package com.rongyichuang.judge.api;
|
|
import com.rongyichuang.judge.dto.response.TagResponse;
|
import com.rongyichuang.judge.entity.Tag;
|
import com.rongyichuang.judge.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;
|
}
|
}
|