lrj
6 天以前 4fa9591629721797386fc11836e3a9deb69cd58c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.rongyichuang.tag.api;
 
import com.rongyichuang.tag.dto.response.TagResponse;
import com.rongyichuang.tag.entity.Tag;
import com.rongyichuang.tag.repository.TagRepository;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
 
import java.util.List;
import java.util.stream.Collectors;
 
@Controller
public class TagGraphqlApi {
 
    private final TagRepository tagRepository;
 
    public TagGraphqlApi(TagRepository tagRepository) {
        this.tagRepository = tagRepository;
    }
 
    @QueryMapping
    public List<TagResponse> tags() {
        return tagRepository.findAll().stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }
 
    @QueryMapping
    public List<TagResponse> tagsByCategory(@Argument String category) {
        return tagRepository.findByCategory(category).stream()
                .map(this::convertToResponse)
                .collect(Collectors.toList());
    }
    
    private TagResponse convertToResponse(Tag tag) {
        TagResponse response = new TagResponse();
        response.setId(tag.getId());
        response.setName(tag.getName());
        response.setCode(tag.getCode());
        return response;
    }
}