mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-05-11 13:17:29 +08:00
feat: add paginated tag loading
This commit is contained in:
@@ -80,18 +80,15 @@ public class TagController {
|
||||
@ApiResponse(responseCode = "200", description = "List of tags",
|
||||
content = @Content(array = @ArraySchema(schema = @Schema(implementation = TagDto.class))))
|
||||
public List<TagDto> list(@RequestParam(value = "keyword", required = false) String keyword,
|
||||
@RequestParam(value = "limit", required = false) Integer limit) {
|
||||
List<Tag> tags = tagService.searchTags(keyword);
|
||||
@RequestParam(value = "page", required = false) Integer page,
|
||||
@RequestParam(value = "pageSize", required = false) Integer pageSize) {
|
||||
List<Tag> tags = tagService.searchTags(keyword, page, pageSize);
|
||||
List<Long> tagIds = tags.stream().map(Tag::getId).toList();
|
||||
Map<Long, Long> postCntByTagIds = postService.countPostsByTagIds(tagIds);
|
||||
List<TagDto> dtos = tags.stream()
|
||||
return tags.stream()
|
||||
.map(t -> tagMapper.toDto(t, postCntByTagIds.getOrDefault(t.getId(), 0L)))
|
||||
.sorted((a, b) -> Long.compare(b.getCount(), a.getCount()))
|
||||
.collect(Collectors.toList());
|
||||
if (limit != null && limit > 0 && dtos.size() > limit) {
|
||||
return dtos.subList(0, limit);
|
||||
}
|
||||
return dtos;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.openisle.model.Tag;
|
||||
import com.openisle.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -13,6 +14,8 @@ public interface TagRepository extends JpaRepository<Tag, Long> {
|
||||
List<Tag> findByApproved(boolean approved);
|
||||
List<Tag> findByApprovedTrue();
|
||||
List<Tag> findByNameContainingIgnoreCaseAndApprovedTrue(String keyword);
|
||||
Page<Tag> findByApprovedTrue(Pageable pageable);
|
||||
Page<Tag> findByNameContainingIgnoreCaseAndApprovedTrue(String keyword, Pageable pageable);
|
||||
|
||||
List<Tag> findByCreatorOrderByCreatedAtDesc(User creator, Pageable pageable);
|
||||
List<Tag> findByCreator(User creator);
|
||||
|
||||
@@ -108,6 +108,17 @@ public class TagService {
|
||||
return tagRepository.findByNameContainingIgnoreCaseAndApprovedTrue(keyword);
|
||||
}
|
||||
|
||||
public List<Tag> searchTags(String keyword, Integer page, Integer pageSize) {
|
||||
if (page == null || pageSize == null) {
|
||||
return searchTags(keyword);
|
||||
}
|
||||
Pageable pageable = PageRequest.of(page, pageSize);
|
||||
if (keyword == null || keyword.isBlank()) {
|
||||
return tagRepository.findByApprovedTrue(pageable).getContent();
|
||||
}
|
||||
return tagRepository.findByNameContainingIgnoreCaseAndApprovedTrue(keyword, pageable).getContent();
|
||||
}
|
||||
|
||||
public List<Tag> getRecentTagsByUser(String username, int limit) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new com.openisle.exception.NotFoundException("User not found"));
|
||||
|
||||
@@ -75,9 +75,9 @@ class TagControllerTest {
|
||||
t.setDescription("d2");
|
||||
t.setIcon("i2");
|
||||
t.setSmallIcon("s2");
|
||||
Mockito.when(tagService.searchTags(null)).thenReturn(List.of(t));
|
||||
Mockito.when(tagService.searchTags(null, 0, 10)).thenReturn(List.of(t));
|
||||
|
||||
mockMvc.perform(get("/api/tags"))
|
||||
mockMvc.perform(get("/api/tags?page=0&pageSize=10"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$[0].name").value("spring"))
|
||||
|
||||
Reference in New Issue
Block a user