diff --git a/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java b/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java index ed8f114..20d8ad6 100644 --- a/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java +++ b/src/main/java/com/beotkkot/qtudy/controller/post/PostApiController.java @@ -22,72 +22,120 @@ public class PostApiController { private final PostsService postsService; private final AuthService authService; - @GetMapping("/posts") - public ResponseEntity getAllPost() { - ResponseEntity response = postsService.getAllPost(); + @GetMapping("/posts/all") + public ResponseEntity getAllPost(@RequestParam("page")int page) { + ResponseEntity response = postsService.getAllPost(page); return response; } @PostMapping("/posts") public ResponseEntity save(@RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } ResponseEntity response = postsService.savePost(kakao_uid, requestDto); return response; } - @GetMapping("/posts/{postId}") - public ResponseEntity getPost(@PathVariable Long postId) { + @GetMapping("/posts") + public ResponseEntity getPost(@RequestParam("postId") Long postId) { ResponseEntity response = postsService.getPost(postId); return response; } - @PatchMapping("/posts/{postId}") - public ResponseEntity patchPost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @PatchMapping("/posts") + public ResponseEntity patchPost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token, @RequestBody PostsRequestDto requestDto) { + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } ResponseEntity response = postsService.patchPost(postId, kakao_uid, requestDto); return response; } - @DeleteMapping("/posts/{postId}") - public ResponseEntity deletePost(@PathVariable Long postId, @RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @DeleteMapping("/posts") + public ResponseEntity deletePost(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) { + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } ResponseEntity response = postsService.deletePost(postId, kakao_uid); return response; } @GetMapping("/posts/my-post-list") - public ResponseEntity getMyPost(@RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); - ResponseEntity response = postsService.getMyPost(kakao_uid); + public ResponseEntity getMyPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) { + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } + ResponseEntity response = postsService.getMyPost(kakao_uid, page); return response; } - @GetMapping("/posts/search-list/{searchWord}") - public ResponseEntity getSearchPost(@PathVariable String searchWord) { - ResponseEntity response = postsService.getSearchPost(searchWord); + @GetMapping("/posts/search-list") + public ResponseEntity getSearchPost(@RequestParam("searchWord") String searchWord, @RequestParam("page")int page) { + ResponseEntity response = postsService.getSearchPost(searchWord, page); return response; } @GetMapping("/posts/category-list") - public ResponseEntity searchPostsByCategory(@RequestParam("category") List categories) { - ResponseEntity response = postsService.getCategorySearchPost(categories); + public ResponseEntity searchPostsByCategory(@RequestParam("categoryId") List categories, @RequestParam("page")int page) { + ResponseEntity response = postsService.getCategorySearchPost(categories, page); return response; } // 스크랩 - @PutMapping("/posts/{postId}/scrap") - public ResponseEntity putScrap(@PathVariable("postId") Long postId, @RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); + @PutMapping("/posts/scrap") + public ResponseEntity putScrap(@RequestParam("postId") Long postId, @RequestHeader(value="Authorization") String token) { + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } ResponseEntity response = postsService.putScrap(postId, kakao_uid); return response; } @GetMapping("/posts/scrap-list") - public ResponseEntity getAllScrapPost(@RequestHeader(value="Authorization") String token) { - Long kakao_uid = authService.getKakaoUserInfo(token).getId(); - ResponseEntity response = postsService.getAllScrapPost(kakao_uid); + public ResponseEntity getAllScrapPost(@RequestHeader(value="Authorization") String token, @RequestParam("page")int page) { + Long kakao_uid; + try { + kakao_uid = authService.getKakaoUserInfo(token).getId(); + if (kakao_uid == null) + return PostsResponseDto.noAuthentication(); + } catch (Exception exception) { + log.info(exception.getMessage()); + return PostsResponseDto.databaseError(); + } + ResponseEntity response = postsService.getAllScrapPost(kakao_uid, page); return response; } } diff --git a/src/main/java/com/beotkkot/qtudy/controller/post/SummarizationController.java b/src/main/java/com/beotkkot/qtudy/controller/post/SummarizationController.java index e0b17fc..7f5f2b1 100644 --- a/src/main/java/com/beotkkot/qtudy/controller/post/SummarizationController.java +++ b/src/main/java/com/beotkkot/qtudy/controller/post/SummarizationController.java @@ -7,6 +7,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @Slf4j @@ -19,8 +20,8 @@ public class SummarizationController { /** * 사용자가 작성한 글에 대해 AI가 요약한 내용을 반환한다. */ - @GetMapping("summary/{postId}") - public ResponseEntity summary(@PathVariable("postId") Long postId) { + @GetMapping("/summary") + public ResponseEntity summary(@RequestParam("postId") Long postId) { // 포스트 아이디로부터 포스트 얻어오기 ResponseEntity response = postsService.getSummary(postId); return response; diff --git a/src/main/java/com/beotkkot/qtudy/controller/quiz/QuizController.java b/src/main/java/com/beotkkot/qtudy/controller/quiz/QuizController.java index 1a9ab05..6f9ba9e 100644 --- a/src/main/java/com/beotkkot/qtudy/controller/quiz/QuizController.java +++ b/src/main/java/com/beotkkot/qtudy/controller/quiz/QuizController.java @@ -30,22 +30,24 @@ public ResponseEntity generateQuiz(@RequestBody GenerateQuizRequestDto dto) { } // 태그별 퀴즈 출력 - @GetMapping("/quiz/tag-quiz/{tagName}") - public ResponseEntity getTagQuiz(@PathVariable String tagName) { + @GetMapping("/quiz/tag-quiz") + public ResponseEntity getTagQuiz(@RequestParam String tagName) { ResponseEntity response = quizService.getTagQuiz(tagName); return response; } // 게시글 별 퀴즈 출력 - @GetMapping("/quiz/post-quiz/{postId}") - public ResponseEntity getPostQuiz(@PathVariable Long postId) { + @GetMapping("/quiz/post-quiz") + public ResponseEntity getPostQuiz(@RequestParam Long postId) { ResponseEntity response = quizService.getPostQuiz(postId); return response; } + // 정답 채점 @PostMapping("/quiz/grade") - public ResponseEntity gradeQuiz(@RequestBody GradeQuizRequestDto dto) { + public ResponseEntity gradeQuiz(@RequestHeader(value="Authorization") String token, @RequestBody GradeQuizRequestDto dto) { + // uuid 받아서 사용자 조회 필요 -> 오답노트 엔티티에 저장해야함 ResponseEntity response = quizService.gradeQuiz(dto); return response; } diff --git a/src/main/java/com/beotkkot/qtudy/controller/tag/TagApiController.java b/src/main/java/com/beotkkot/qtudy/controller/tag/TagApiController.java new file mode 100644 index 0000000..5bb0fe9 --- /dev/null +++ b/src/main/java/com/beotkkot/qtudy/controller/tag/TagApiController.java @@ -0,0 +1,20 @@ +package com.beotkkot.qtudy.controller.tag; + +import com.beotkkot.qtudy.dto.response.tags.GetTop3TagsDto; +import com.beotkkot.qtudy.service.tag.TagService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RequiredArgsConstructor +@RestController +public class TagApiController { + private final TagService tagService; + + @GetMapping("/tag/top3") + public ResponseEntity getTop3Tags() { + ResponseEntity response = tagService.getTop3Tags(); + return response; + } +} diff --git a/src/main/java/com/beotkkot/qtudy/domain/posts/Posts.java b/src/main/java/com/beotkkot/qtudy/domain/posts/Posts.java index a938cf5..733d969 100644 --- a/src/main/java/com/beotkkot/qtudy/domain/posts/Posts.java +++ b/src/main/java/com/beotkkot/qtudy/domain/posts/Posts.java @@ -20,7 +20,7 @@ public class Posts { private Long postId; @Column(nullable = false) - private Long userUid; + private Long kakaoId; @Column(nullable = false) private String title; @@ -34,7 +34,7 @@ public class Posts { // AI 요약본 @Column(columnDefinition = "TEXT") - private String aiScript; + private String summary; private int commentCount; diff --git a/src/main/java/com/beotkkot/qtudy/dto/object/TagListItem.java b/src/main/java/com/beotkkot/qtudy/dto/object/TagListItem.java new file mode 100644 index 0000000..94c54eb --- /dev/null +++ b/src/main/java/com/beotkkot/qtudy/dto/object/TagListItem.java @@ -0,0 +1,24 @@ +package com.beotkkot.qtudy.dto.object; + +import com.beotkkot.qtudy.domain.tags.Tags; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Builder +@Getter +@AllArgsConstructor +@NoArgsConstructor +public class TagListItem { + private String name; + private int count; + + public static TagListItem of(Tags tag) { + + return TagListItem.builder() + .name(tag.getName()) + .count(tag.getCount()) + .build(); + } +} diff --git a/src/main/java/com/beotkkot/qtudy/dto/request/posts/PostsRequestDto.java b/src/main/java/com/beotkkot/qtudy/dto/request/posts/PostsRequestDto.java index 5e55efa..c691e2d 100644 --- a/src/main/java/com/beotkkot/qtudy/dto/request/posts/PostsRequestDto.java +++ b/src/main/java/com/beotkkot/qtudy/dto/request/posts/PostsRequestDto.java @@ -25,10 +25,10 @@ public Posts toEntity(Long uid) { String tagString = String.join(",", tag); return Posts.builder() - .userUid(uid) + .kakaoId(uid) .title(title) .content(content) - .aiScript(summary) + .summary(summary) .tag(tagString) .createdAt(writeDatetime) .commentCount(0) diff --git a/src/main/java/com/beotkkot/qtudy/dto/response/posts/GetPostsAllResponseDto.java b/src/main/java/com/beotkkot/qtudy/dto/response/posts/GetPostsAllResponseDto.java index 1a6a976..f54661e 100644 --- a/src/main/java/com/beotkkot/qtudy/dto/response/posts/GetPostsAllResponseDto.java +++ b/src/main/java/com/beotkkot/qtudy/dto/response/posts/GetPostsAllResponseDto.java @@ -13,15 +13,17 @@ @Getter public class GetPostsAllResponseDto extends ResponseDto { + private int page; private List postList; - public GetPostsAllResponseDto(List PostListItem) { + public GetPostsAllResponseDto(List PostListItem, int page) { super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); + this.page = page; this.postList = PostListItem; } - public static ResponseEntity success(List PostListItem) { - GetPostsAllResponseDto result = new GetPostsAllResponseDto(PostListItem); + public static ResponseEntity success(List PostListItem, int page) { + GetPostsAllResponseDto result = new GetPostsAllResponseDto(PostListItem, page); return ResponseEntity.status(HttpStatus.OK).body(result); } diff --git a/src/main/java/com/beotkkot/qtudy/dto/response/tags/GetTop3TagsDto.java b/src/main/java/com/beotkkot/qtudy/dto/response/tags/GetTop3TagsDto.java new file mode 100644 index 0000000..b52d838 --- /dev/null +++ b/src/main/java/com/beotkkot/qtudy/dto/response/tags/GetTop3TagsDto.java @@ -0,0 +1,31 @@ +package com.beotkkot.qtudy.dto.response.tags; + +import com.beotkkot.qtudy.common.ResponseCode; +import com.beotkkot.qtudy.common.ResponseMessage; +import com.beotkkot.qtudy.dto.object.TagListItem; +import com.beotkkot.qtudy.dto.response.ResponseDto; +import lombok.Getter; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.List; + +@Getter +public class GetTop3TagsDto extends ResponseDto { + private List top3List; + + public GetTop3TagsDto(List TagListItem) { + super(ResponseCode.SUCCESS, ResponseMessage.SUCCESS); + this.top3List = TagListItem; + } + + public static ResponseEntity success(List top3List) { + GetTop3TagsDto result = new GetTop3TagsDto(top3List); + return ResponseEntity.status(HttpStatus.OK).body(result); + } + + public static ResponseEntity notExistedPost(){ + ResponseDto result = new ResponseDto(ResponseCode.NOT_EXISTED_POST, ResponseMessage.NOT_EXISTED_POST); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); + } +} diff --git a/src/main/java/com/beotkkot/qtudy/repository/posts/PostsRepository.java b/src/main/java/com/beotkkot/qtudy/repository/posts/PostsRepository.java index 368b7df..4ba46bd 100644 --- a/src/main/java/com/beotkkot/qtudy/repository/posts/PostsRepository.java +++ b/src/main/java/com/beotkkot/qtudy/repository/posts/PostsRepository.java @@ -1,6 +1,7 @@ package com.beotkkot.qtudy.repository.posts; import com.beotkkot.qtudy.domain.posts.Posts; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -10,12 +11,12 @@ @Repository public interface PostsRepository extends JpaRepository { - List findAllByUserUid(Long uuid); + List findAllByKakaoId(Long kakaoId, PageRequest pageRequest); @Query("SELECT p FROM Posts p WHERE p.title LIKE %:searchWord% OR p.content LIKE %:searchWord% OR p.tag LIKE %:searchWord%") - List findBySearchWord(String searchWord); + List findBySearchWord(String searchWord, PageRequest pageRequest); - List findByCategoryId(Long categoryId); + List findByCategoryId(Long categoryId, PageRequest pageRequest); Posts findByPostId(Long postId); diff --git a/src/main/java/com/beotkkot/qtudy/repository/scrap/ScrapRepository.java b/src/main/java/com/beotkkot/qtudy/repository/scrap/ScrapRepository.java index f6b5279..ed90874 100644 --- a/src/main/java/com/beotkkot/qtudy/repository/scrap/ScrapRepository.java +++ b/src/main/java/com/beotkkot/qtudy/repository/scrap/ScrapRepository.java @@ -2,6 +2,7 @@ import com.beotkkot.qtudy.domain.scrap.Scrap; import com.beotkkot.qtudy.domain.primaryKey.ScrapPk; +import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -15,7 +16,7 @@ public interface ScrapRepository extends JpaRepository { Scrap findByPostIdAndUserId(Long postId, Long uid); @Query("SELECT s.postId FROM Scrap s WHERE s.userId = :userId") - List findPostIdsByUserId(@Param("userId") Long userId); + List findPostIdsByUserId(@Param("userId") Long userId, PageRequest pageRequest); @Transactional void deleteByPostId(Long postId); diff --git a/src/main/java/com/beotkkot/qtudy/service/posts/PostsService.java b/src/main/java/com/beotkkot/qtudy/service/posts/PostsService.java index a8a73d4..001eb19 100644 --- a/src/main/java/com/beotkkot/qtudy/service/posts/PostsService.java +++ b/src/main/java/com/beotkkot/qtudy/service/posts/PostsService.java @@ -14,14 +14,13 @@ import com.beotkkot.qtudy.repository.user.UserRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; +import java.util.*; @Slf4j @RequiredArgsConstructor @@ -32,7 +31,7 @@ public class PostsService { private final TagsRepository tagRepo; private final ScrapRepository scrapRepo; private final SummaryService summaryService; -// private final CommentsRepository commentsRepo; (댓글 기능 구현 시 주석 제거) + private final CommentsRepository commentsRepo; @Transactional public ResponseEntity savePost(Long kakao_uid, PostsRequestDto dto) { @@ -75,7 +74,7 @@ public ResponseEntity savePost(Long kakao_uid, PostsRe */ String summary = summaryService.summary(dto.getContent()); post.setContent(dto.getContent()); - post.setAiScript(summary); + post.setSummary(summary); // 포스트 저장 postsRepo.save(post); @@ -97,7 +96,7 @@ public ResponseEntity getPost(Long postId) { try { if (postsRepo.existsById(postId)) { post = postsRepo.findById(postId).get(); - user = userRepo.findByKakaoId(post.getUserUid()); + user = userRepo.findByKakaoId(post.getKakaoId()); } else { return GetPostsResponseDto.noExistPost(); } @@ -116,7 +115,7 @@ public ResponseEntity getSummary(Long postId) { try { if (postsRepo.existsById(postId)) { post = postsRepo.findByPostId(postId); - summary = post.getAiScript(); + summary = post.getSummary(); } else { return GetSummaryResponseDto.noExistPost(); } @@ -129,10 +128,11 @@ public ResponseEntity getSummary(Long postId) { } @Transactional - public ResponseEntity getAllPost() { + public ResponseEntity getAllPost(int page) { List postListItems = new ArrayList<>(); + PageRequest pageRequest = PageRequest.of(page, 12, Sort.by("createdAt").descending()); try { - List posts = postsRepo.findAll(); + List posts = postsRepo.findAll(pageRequest).getContent(); for (Posts post : posts) postListItems.add(PostListItem.of(post)); } catch (Exception exception) { @@ -140,8 +140,8 @@ public ResponseEntity getAllPost() { return ResponseDto.databaseError(); } - GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems); - return responseDto.success(postListItems); + GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems, page); + return responseDto.success(postListItems, page); } @Transactional @@ -153,7 +153,7 @@ public ResponseEntity patchPost(Long postId, Long kaka Posts post = postOptional.get(); if (userRepo.findByKakaoId(kakao_uid) == null) return PostsResponseDto.notExistUser(); - Long writerId = post.getUserUid(); + Long writerId = post.getKakaoId(); if (!writerId.equals(kakao_uid)) return PostsResponseDto.noPermission(); // 업데이트되기 이전의 태그 목록 @@ -206,13 +206,12 @@ public ResponseEntity deletePost(Long postId, Long kak if (!postsRepo.existsById(postId)) return PostsResponseDto.notExistedPost(); if (userRepo.findByKakaoId(kakao_uid) == null) return PostsResponseDto.notExistUser(); - Long writerId = post.getUserUid(); + Long writerId = post.getKakaoId(); boolean isWriter = writerId.equals(kakao_uid); if (!isWriter) return PostsResponseDto.noPermission(); - // ********* comment, scrap 함께 삭제 (댓글 기능 구현 시 수정 바람) scrapRepo.deleteByPostId(postId); - // commentRepo.deleteByPostId(postId); + commentRepo.deleteByPostId(postId); // 관련된 hash tag -1 List tagNameList = Arrays.asList(post.getTag().split("\\s*,\\s*")); @@ -231,10 +230,11 @@ public ResponseEntity deletePost(Long postId, Long kak } @Transactional - public ResponseEntity getMyPost(Long kakao_uid) { - List postListItems = new ArrayList<>();; + public ResponseEntity getMyPost(Long kakao_uid, int page) { + List postListItems = new ArrayList<>(); try { - List posts = postsRepo.findAllByUserUid(kakao_uid); + PageRequest pageRequest = PageRequest.of(page, 12, Sort.by("createdAt").descending()); + List posts = postsRepo.findAllByKakaoId(kakao_uid, pageRequest); for (Posts post : posts) postListItems.add(PostListItem.of(post)); } catch (Exception exception) { @@ -242,15 +242,16 @@ public ResponseEntity getMyPost(Long kakao_uid) return ResponseDto.databaseError(); } - GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems); - return responseDto.success(postListItems); + GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems, page); + return responseDto.success(postListItems, page); } @Transactional - public ResponseEntity getSearchPost(String searchWord) { + public ResponseEntity getSearchPost(String searchWord, int page) { List postListItems = new ArrayList<>(); try { - List posts = postsRepo.findBySearchWord(searchWord); + PageRequest pageRequest = PageRequest.of(page, 12, Sort.by("createdAt").descending()); + List posts = postsRepo.findBySearchWord(searchWord, pageRequest); for (Posts post : posts) postListItems.add(PostListItem.of(post)); } catch (Exception exception) { @@ -258,16 +259,17 @@ public ResponseEntity getSearchPost(String searc return ResponseDto.databaseError(); } - GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems); - return responseDto.success(postListItems); + GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems, page); + return responseDto.success(postListItems, page); } @Transactional - public ResponseEntity getCategorySearchPost(List categories) { + public ResponseEntity getCategorySearchPost(List categories, int page) { List postListItems = new ArrayList<>(); try { for (Long category : categories) { - List posts = postsRepo.findByCategoryId(category); + PageRequest pageRequest = PageRequest.of(page, 12, Sort.by("createdAt").descending()); + List posts = postsRepo.findByCategoryId(category, pageRequest); for (Posts post : posts) { postListItems.add(PostListItem.of(post)); @@ -278,8 +280,8 @@ public ResponseEntity getCategorySearchPost(List return ResponseDto.databaseError(); } - GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems); - return responseDto.success(postListItems); + GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems, page); + return responseDto.success(postListItems, page); } // 스크랩 @@ -313,12 +315,13 @@ public ResponseEntity putScrap(Long postId, Long ka } @Transactional - public ResponseEntity getAllScrapPost(Long kakao_uid) { + public ResponseEntity getAllScrapPost(Long kakao_uid, int page) { List postListItems = new ArrayList<>(); try { if (userRepo.findByKakaoId(kakao_uid) == null) return PutScrapResponseDto.notExistUser(); - List postIdList = scrapRepo.findPostIdsByUserId(kakao_uid); + PageRequest pageRequest = PageRequest.of(page, 12, Sort.by("createdAt").descending()); + List postIdList = scrapRepo.findPostIdsByUserId(kakao_uid, pageRequest); List posts = postsRepo.findByPostIds(postIdList); if (posts == null) return PutScrapResponseDto.notExistedPost(); @@ -329,7 +332,7 @@ public ResponseEntity getAllScrapPost(Long kakao return ResponseDto.databaseError(); } - GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems); - return responseDto.success(postListItems); + GetPostsAllResponseDto responseDto = new GetPostsAllResponseDto(postListItems, page); + return responseDto.success(postListItems, page); } } diff --git a/src/main/java/com/beotkkot/qtudy/service/tag/TagService.java b/src/main/java/com/beotkkot/qtudy/service/tag/TagService.java new file mode 100644 index 0000000..3f26cbb --- /dev/null +++ b/src/main/java/com/beotkkot/qtudy/service/tag/TagService.java @@ -0,0 +1,43 @@ +package com.beotkkot.qtudy.service.tag; + +import com.beotkkot.qtudy.domain.tags.Tags; +import com.beotkkot.qtudy.dto.object.TagListItem; +import com.beotkkot.qtudy.dto.response.ResponseDto; +import com.beotkkot.qtudy.dto.response.tags.GetTop3TagsDto; +import com.beotkkot.qtudy.repository.tags.TagsRepository; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@RequiredArgsConstructor +@Service +public class TagService { + + private final TagsRepository tagRepo; + + @Transactional + public ResponseEntity getTop3Tags() { + List top3List = new ArrayList<>(); + try { + List tags = tagRepo.findTop3ByOrderByCountDesc(); + for (Tags tag : tags) { + top3List.add(TagListItem.of(tag)); + } + log.info(String.valueOf(top3List)); + } catch (Exception exception) { + exception.printStackTrace(); + return ResponseDto.databaseError(); + } + + GetTop3TagsDto responseDto = new GetTop3TagsDto(top3List); + return responseDto.success(top3List); + } + +}