From d2e9b95f78ec08c0d29f3345c47cc19d13097339 Mon Sep 17 00:00:00 2001 From: changha Date: Tue, 30 Apr 2024 15:00:47 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=20API=EC=B6=94=EA=B0=80=20(=EC=B9=B4=ED=85=8C?= =?UTF-8?q?=EA=B3=A0=EB=A6=AC=20api=EC=95=88=EB=90=98=EA=B3=A0=EC=9E=88?= =?UTF-8?q?=EC=9D=8C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sanEseo/like/adapter/LikeAdapter.java | 5 +++ .../sanEseo/like/adapter/LikeController.java | 6 ++++ .../sanEseo/like/adapter/LikeRepository.java | 1 + .../like/application/port/LikePort.java | 1 + .../like/application/service/LikeService.java | 4 +++ .../adapter/PostDistrictAdapter.java | 9 +++++ .../adapter/PostDistrictController.java | 16 ++++++--- .../adapter/PostDistrictRepository.java | 6 ++++ .../application/port/PostDistrictPort.java | 4 +++ .../service/PostDistrictService.java | 36 ++++++++++++++++--- 10 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeAdapter.java b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeAdapter.java index e8dcf49..cbf9410 100644 --- a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeAdapter.java +++ b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeAdapter.java @@ -34,5 +34,10 @@ public int countByPostId(Long postId) { return likeRepository.countByPostId(postId); } + @Override + public boolean existsByPostIdAndMemberId(Long postId, Long memberId) { + return likeRepository.existsByPostIdAndMemberId(postId, memberId); + } + } diff --git a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeController.java b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeController.java index 590418b..f948c4d 100644 --- a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeController.java +++ b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeController.java @@ -35,4 +35,10 @@ public ResponseEntity getLikeCount(@PathVariable Long postId) { return ApiResponse.ok("좋아요 수 조회 성공", new GetLikeResponse(postId, likeCount)); } + @GetMapping("/posts/{postId}/members/{memberId}/likes") + public ResponseEntity hasMemberLikedPost(@PathVariable Long postId, @PathVariable Long memberId) { + boolean hasLiked = likeService.hasMemberLikedPost(memberId, postId); + return ApiResponse.ok("좋아요 여부 조회 성공", hasLiked); + } + } diff --git a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeRepository.java b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeRepository.java index 9b032ad..16a38d8 100644 --- a/src/main/java/com/seoultech/sanEseo/like/adapter/LikeRepository.java +++ b/src/main/java/com/seoultech/sanEseo/like/adapter/LikeRepository.java @@ -14,4 +14,5 @@ public interface LikeRepository extends JpaRepository { int countByPostId(Long postId); void deleteByPostAndMember(Post post, Member member); + boolean existsByPostIdAndMemberId(Long postId, Long memberId); } diff --git a/src/main/java/com/seoultech/sanEseo/like/application/port/LikePort.java b/src/main/java/com/seoultech/sanEseo/like/application/port/LikePort.java index 07b90ad..e117bab 100644 --- a/src/main/java/com/seoultech/sanEseo/like/application/port/LikePort.java +++ b/src/main/java/com/seoultech/sanEseo/like/application/port/LikePort.java @@ -14,4 +14,5 @@ public interface LikePort { int countByPostId(Long postId); + boolean existsByPostIdAndMemberId(Long postId, Long memberId); } diff --git a/src/main/java/com/seoultech/sanEseo/like/application/service/LikeService.java b/src/main/java/com/seoultech/sanEseo/like/application/service/LikeService.java index 1481e6b..159380f 100644 --- a/src/main/java/com/seoultech/sanEseo/like/application/service/LikeService.java +++ b/src/main/java/com/seoultech/sanEseo/like/application/service/LikeService.java @@ -54,4 +54,8 @@ public int getLikeCount(Long postId) { return likePort.countByPostId(postId); } + public boolean hasMemberLikedPost(Long memberId, Long postId) { + return likePort.existsByPostIdAndMemberId(memberId, postId); + } + } diff --git a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictAdapter.java b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictAdapter.java index 986885f..1905bd3 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictAdapter.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictAdapter.java @@ -4,6 +4,9 @@ import com.seoultech.sanEseo.post.domain.Category; import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort; import com.seoultech.sanEseo.post_district.domain.PostDistrict; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.stereotype.Component; import java.util.List; @@ -47,4 +50,10 @@ public List findAll() { public List findByPostCategory(Category category) { return postDistrictRepository.findByPostCategory(category); } + +// @Override +// public Slice findByCategory(Category category, Pageable pageable) { +// return postDistrictRepository.findByCategory(category, pageable); +// } + } diff --git a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictController.java b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictController.java index edc13a1..5dd8a11 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictController.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictController.java @@ -6,6 +6,9 @@ import com.seoultech.sanEseo.post_district.application.service.GetPostDistrictResponse; import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort; import com.seoultech.sanEseo.post_district.application.service.PostDistrictService; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -29,11 +32,14 @@ public ResponseEntity getPostDistrict(@PathVariable Long districtId) { } //전체 게시글 조회 - @GetMapping("/posts/{category}") - public ResponseEntity getAllPostDistrictByCategory(@PathVariable int category) { - List responses = postDistrictService.getAllPostDistrict(category); - return ApiResponse.ok("전체 게시글 목록 조회 성공", responses); - } +// @GetMapping("/posts") +// public ResponseEntity getAllPostDistrictByCategory(@RequestParam(value = "page", defaultValue = "0") int page, +// @RequestParam(value = "size", defaultValue = "10") int size, +// @RequestParam(value = "category", required = true) int category) { +// Pageable pageable = PageRequest.of(page, size); +// Slice responses = (Slice) postDistrictService.getAllPostDistrict(pageable, category); +// return ApiResponse.ok("전체 게시글 목록 조회 성공", responses); +// } //좋아요 순으로 정렬된 게시글 조회 @GetMapping("/posts/{category}/sorted-by-likes") diff --git a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictRepository.java b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictRepository.java index c0a4bed..c8016c0 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictRepository.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/adapter/PostDistrictRepository.java @@ -1,7 +1,10 @@ package com.seoultech.sanEseo.post_district.adapter; import com.seoultech.sanEseo.post.domain.Category; +import com.seoultech.sanEseo.post.domain.Post; import com.seoultech.sanEseo.post_district.domain.PostDistrict; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -13,4 +16,7 @@ public interface PostDistrictRepository extends JpaRepository findByPostCategory(Category category); +// Slice findByCategory(Category category, Pageable pageable); + + } diff --git a/src/main/java/com/seoultech/sanEseo/post_district/application/port/PostDistrictPort.java b/src/main/java/com/seoultech/sanEseo/post_district/application/port/PostDistrictPort.java index f24cdd2..0591b2d 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/application/port/PostDistrictPort.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/application/port/PostDistrictPort.java @@ -2,6 +2,8 @@ import com.seoultech.sanEseo.post.domain.Category; import com.seoultech.sanEseo.post_district.domain.PostDistrict; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import java.util.List; @@ -19,4 +21,6 @@ public interface PostDistrictPort { List findAll(); List findByPostCategory(Category category); + +// Slice findByCategory(Category category, Pageable pageable); } diff --git a/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java b/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java index 1cae8fd..76932b7 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java @@ -10,6 +10,8 @@ import com.seoultech.sanEseo.post.domain.Post; import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort; import com.seoultech.sanEseo.post_district.domain.PostDistrict; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Slice; import org.springframework.stereotype.Service; import java.util.List; @@ -45,11 +47,13 @@ public List getPostDistrict(Long districtId) { return getPostDistrictResponses(postDistricts); } - public List getAllPostDistrict(int category) { - Category categoryEnum = Category.from(category); - List postDistricts = postDistrictPort.findByPostCategory(categoryEnum); - return getPostDistrictResponses(postDistricts); - } +// public List getAllPostDistrict(Pageable pageable, int category) { +// Category categoryEnum = Category.from(category); +// Slice postDistricts = postDistrictPort.findByCategory(categoryEnum, pageable); +// +// return getPostDistrictResponses(postDistricts); +// } + public List getPostByLikesSortedDesc(int category) { Category categoryEnum = Category.from(category); @@ -88,4 +92,26 @@ private List getPostDistrictResponses(List getPostDistrictResponses(Slice postDistricts) { + List responses = postDistricts.stream().map(postDistrict -> { + Post post = postDistrict.getPost(); + List images = imageService.getPostImages(post.getId()); + List imageResponses = images.stream().map(image -> new GetImageResponse(image.getImageUrl())).collect(Collectors.toList()); + int likeCount = likeService.getLikeCount(post.getId()); + return new GetPostDistrictResponse( + post.getId(), + imageResponses, + post.getTitle(), + post.getSubTitle(), + post.getTime(), + likeCount, // 가정: Post 엔티티에 좋아요 수를 반환하는 getLikes() 메소드가 있음 + post.getDistance(), + post.getLevel(), + postDistrict.getDistrict().getName() + ); + }).collect(Collectors.toList()); + return responses; + } + }