From 5ae05daac0094fd6b40bf8eee13af9b47722eed8 Mon Sep 17 00:00:00 2001 From: kyukong <92148749+kyukong@users.noreply.github.com> Date: Mon, 26 Feb 2024 17:06:41 +0900 Subject: [PATCH] =?UTF-8?q?[REFACTOR]=20=ED=8C=9D=EC=97=85=EC=8A=A4?= =?UTF-8?q?=ED=86=A0=EC=96=B4,=20=EC=BB=A4=EB=AE=A4=EB=8B=88=ED=8B=B0=20?= =?UTF-8?q?=EA=B2=8C=EC=8B=9C=EA=B8=80=EC=97=90=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=9E=A9=20=EC=97=AC=EB=B6=80=20=EB=B0=8F=20=EC=9E=91=EC=84=B1?= =?UTF-8?q?=20=EC=97=AC=EB=B6=80=20=EC=B6=94=EA=B0=80=20(#22)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [REFACTOR] 팝업스토어 게시글 상세정보에 작성/스크랩 여부 추가 * [REFACTOR] 커뮤니티 게시글 상세정보에 작성/스크랩 여부 추가 --- .../dto/response/CommunityDetailResponse.java | 11 +++++++++-- .../kr/community/mapper/CommunityMapper.java | 2 +- .../repository/CommunityRepository.java | 11 +++++++++++ .../kr/community/service/CommunityService.java | 9 ++++++++- .../controller/dto/response/PopupResponse.java | 8 ++++++-- .../com/oya/kr/popup/mapper/PopupMapper.java | 3 ++- .../dto/request/PopupDetailMapperRequest.java | 12 ++++++++++++ .../response/PopupDetailMapperResponse.java | 2 ++ .../kr/popup/repository/PopupRepository.java | 3 ++- .../com/oya/kr/popup/service/PopupService.java | 2 +- .../kr/community/mapper/CommunityMapper.xml | 17 +++++++++++++++++ .../com/oya/kr/popup/mapper/PopupMapper.xml | 15 ++++++++++----- src/main/resources/mybatis/mybatis-config.xml | 1 + .../community/mapper/CommunityMapperTest.java | 18 ++++++++++++++++++ .../oya/kr/popup/mapper/PlanMapperTest.java | 5 +++++ .../oya/kr/popup/mapper/PopupMapperTest.java | 5 ++++- 16 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/oya/kr/popup/mapper/dto/request/PopupDetailMapperRequest.java diff --git a/src/main/java/com/oya/kr/community/controller/dto/response/CommunityDetailResponse.java b/src/main/java/com/oya/kr/community/controller/dto/response/CommunityDetailResponse.java index d3ddbf7..6b86a85 100644 --- a/src/main/java/com/oya/kr/community/controller/dto/response/CommunityDetailResponse.java +++ b/src/main/java/com/oya/kr/community/controller/dto/response/CommunityDetailResponse.java @@ -15,6 +15,7 @@ @Getter @RequiredArgsConstructor public class CommunityDetailResponse { + private final long writeId; private final String nickname; private final String email; @@ -33,7 +34,11 @@ public class CommunityDetailResponse { private final List voteResponseList; private final List imageList; - public static CommunityDetailResponse from(List imageList, Community community, int countView, List voteResponseList) { + private final boolean written; + private final boolean collected; + + public static CommunityDetailResponse from(List imageList, Community community, int countView, + List voteResponseList, Long userId, boolean collected) { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd"); String createdDateString = community.getCreatedDate().format(formatter); @@ -54,7 +59,9 @@ public static CommunityDetailResponse from(List imageList, Community com modifiedDateString, countView, voteResponseList, - imageList + imageList, + userId.equals(community.getUser().getId()), + collected ); } } diff --git a/src/main/java/com/oya/kr/community/mapper/CommunityMapper.java b/src/main/java/com/oya/kr/community/mapper/CommunityMapper.java index ea4a56c..44d6d4b 100644 --- a/src/main/java/com/oya/kr/community/mapper/CommunityMapper.java +++ b/src/main/java/com/oya/kr/community/mapper/CommunityMapper.java @@ -5,7 +5,6 @@ import org.apache.ibatis.annotations.Param; -import com.oya.kr.community.mapper.dto.request.ReadCollectionsMapperRequest; import com.oya.kr.community.mapper.dto.request.SaveBasicMapperRequest; import com.oya.kr.community.mapper.dto.request.SaveVoteMapperRequest; import com.oya.kr.community.mapper.dto.response.CommunityBasicMapperResponse; @@ -31,4 +30,5 @@ public interface CommunityMapper { int findSizeByAll(); int findSizeByType(String type); int findSizeByCollection(Long userId); + boolean existCollection(@Param("communityId") long communityId, @Param("userId") Long userId); } diff --git a/src/main/java/com/oya/kr/community/repository/CommunityRepository.java b/src/main/java/com/oya/kr/community/repository/CommunityRepository.java index 1cb7e1a..1531c51 100644 --- a/src/main/java/com/oya/kr/community/repository/CommunityRepository.java +++ b/src/main/java/com/oya/kr/community/repository/CommunityRepository.java @@ -213,4 +213,15 @@ public int findSizeByType(User loginUser, String type) { return communityMapper.findSizeByType(type); } } + + /** + * 커뮤니티 게시글 스크랩 여부 반환 + * + * @parameter long, Long + * @author 김유빈 + * @since 2024.02.26 + */ + public boolean existCollection(long communityId, Long userId) { + return communityMapper.existCollection(communityId, userId); + } } diff --git a/src/main/java/com/oya/kr/community/service/CommunityService.java b/src/main/java/com/oya/kr/community/service/CommunityService.java index 6522fb0..b3efdb2 100644 --- a/src/main/java/com/oya/kr/community/service/CommunityService.java +++ b/src/main/java/com/oya/kr/community/service/CommunityService.java @@ -88,6 +88,11 @@ public String save(String communityType, String email, CommunityRequest communit * @return CommunityDetailResponse * @author 이상민 * @since 2024.02.18 + * + * 커뮤니티 게시글 스크랩 여부 추가 + * + * @author 김유빈 + * @since 2024.02.16 */ public CommunityDetailResponse read(String email, long communityId) { User loginUser = userRepository.findByEmail(email); @@ -98,8 +103,10 @@ public CommunityDetailResponse read(String email, long communityId) { Community community = response.toDomain(writeUser); int countView = response.getCountView(); + boolean collected = communityRepository.existCollection(communityId, loginUser.getId()); + List voteResponseList = getVoteList(community.getCommunityType().getName(), loginUser, community.getId()); - return CommunityDetailResponse.from(imageList, community, countView, voteResponseList); + return CommunityDetailResponse.from(imageList, community, countView, voteResponseList, loginUser.getId(), collected); } /** diff --git a/src/main/java/com/oya/kr/popup/controller/dto/response/PopupResponse.java b/src/main/java/com/oya/kr/popup/controller/dto/response/PopupResponse.java index e9e4a86..2cbd73d 100644 --- a/src/main/java/com/oya/kr/popup/controller/dto/response/PopupResponse.java +++ b/src/main/java/com/oya/kr/popup/controller/dto/response/PopupResponse.java @@ -22,8 +22,10 @@ public class PopupResponse { private final CategoryResponse category; private final LocalDate openDate; private final LocalDate closeDate; + private final boolean written; + private final boolean collected; - public static PopupResponse from(PopupDetailMapperResponse response) { + public static PopupResponse from(PopupDetailMapperResponse response, Long userId) { return new PopupResponse( response.getId(), response.getPlanId(), @@ -32,7 +34,9 @@ public static PopupResponse from(PopupDetailMapperResponse response) { response.getPulledDate(), CategoryResponse.from(Category.from(response.getCategory())), response.getOpenDate(), - response.getCloseDate() + response.getCloseDate(), + userId.equals(response.getUserId()), + response.isCollected() ); } diff --git a/src/main/java/com/oya/kr/popup/mapper/PopupMapper.java b/src/main/java/com/oya/kr/popup/mapper/PopupMapper.java index c354b23..d30c860 100644 --- a/src/main/java/com/oya/kr/popup/mapper/PopupMapper.java +++ b/src/main/java/com/oya/kr/popup/mapper/PopupMapper.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.Optional; +import com.oya.kr.popup.mapper.dto.request.PopupDetailMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSaveMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSearchMapperRequest; import com.oya.kr.popup.mapper.dto.response.PopupDetailMapperResponse; @@ -16,7 +17,7 @@ public interface PopupMapper { Optional findById(Long popupId); - Optional findByIdWithDate(Long popupId); + Optional findByIdWithDate(PopupDetailMapperRequest request); List findAllByPlanId(Long planId); diff --git a/src/main/java/com/oya/kr/popup/mapper/dto/request/PopupDetailMapperRequest.java b/src/main/java/com/oya/kr/popup/mapper/dto/request/PopupDetailMapperRequest.java new file mode 100644 index 0000000..939d452 --- /dev/null +++ b/src/main/java/com/oya/kr/popup/mapper/dto/request/PopupDetailMapperRequest.java @@ -0,0 +1,12 @@ +package com.oya.kr.popup.mapper.dto.request; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public class PopupDetailMapperRequest { + + private final Long popupId; + private final Long userId; +} diff --git a/src/main/java/com/oya/kr/popup/mapper/dto/response/PopupDetailMapperResponse.java b/src/main/java/com/oya/kr/popup/mapper/dto/response/PopupDetailMapperResponse.java index 76db40f..3820122 100644 --- a/src/main/java/com/oya/kr/popup/mapper/dto/response/PopupDetailMapperResponse.java +++ b/src/main/java/com/oya/kr/popup/mapper/dto/response/PopupDetailMapperResponse.java @@ -17,7 +17,9 @@ public class PopupDetailMapperResponse { private final String withdrawalStatus; private final long planId; private final String thumbnail; + private final Long userId; private final String category; private final LocalDate openDate; private final LocalDate closeDate; + private final boolean collected; } diff --git a/src/main/java/com/oya/kr/popup/repository/PopupRepository.java b/src/main/java/com/oya/kr/popup/repository/PopupRepository.java index da3923e..8e521d7 100644 --- a/src/main/java/com/oya/kr/popup/repository/PopupRepository.java +++ b/src/main/java/com/oya/kr/popup/repository/PopupRepository.java @@ -19,6 +19,7 @@ import com.oya.kr.popup.mapper.PopupViewMapper; import com.oya.kr.popup.mapper.dto.request.PopupCollectionMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupCollectionSaveMapperRequest; +import com.oya.kr.popup.mapper.dto.request.PopupDetailMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupImageSaveMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSaveMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSearchMapperRequest; @@ -66,7 +67,7 @@ public Popup findById(Long id, Plan plan) { * @since 2024.02.21 */ public PopupDetailMapperResponse findByIdWithDate(Long id, Long userId) { - PopupDetailMapperResponse response = popupMapper.findByIdWithDate(id) + PopupDetailMapperResponse response = popupMapper.findByIdWithDate(new PopupDetailMapperRequest(id, userId)) .orElseThrow(() -> new ApplicationException(NOT_EXIST_POPUP)); countView(id, userId); return response; diff --git a/src/main/java/com/oya/kr/popup/service/PopupService.java b/src/main/java/com/oya/kr/popup/service/PopupService.java index d987e68..7e69fd5 100644 --- a/src/main/java/com/oya/kr/popup/service/PopupService.java +++ b/src/main/java/com/oya/kr/popup/service/PopupService.java @@ -53,7 +53,7 @@ public class PopupService { public PopupResponse findById(String email, Long popupId) { User savedUser = userRepository.findByEmail(email); PopupDetailMapperResponse popupMapperResponse = popupRepository.findByIdWithDate(popupId, savedUser.getId()); - return PopupResponse.from(popupMapperResponse); + return PopupResponse.from(popupMapperResponse, savedUser.getId()); } /** diff --git a/src/main/resources/com/oya/kr/community/mapper/CommunityMapper.xml b/src/main/resources/com/oya/kr/community/mapper/CommunityMapper.xml index ab3ab64..f00cec8 100644 --- a/src/main/resources/com/oya/kr/community/mapper/CommunityMapper.xml +++ b/src/main/resources/com/oya/kr/community/mapper/CommunityMapper.xml @@ -119,6 +119,23 @@ AND CC.DELETED = 0 + + + SELECT c.ID, diff --git a/src/main/resources/com/oya/kr/popup/mapper/PopupMapper.xml b/src/main/resources/com/oya/kr/popup/mapper/PopupMapper.xml index b21bdcf..90cdeb7 100644 --- a/src/main/resources/com/oya/kr/popup/mapper/PopupMapper.xml +++ b/src/main/resources/com/oya/kr/popup/mapper/PopupMapper.xml @@ -9,12 +9,17 @@ AND DELETED = 0 - + SELECT P.ID, TITLE, DESCRIPTION, PULLED_DATE, WITHDRAWAL_STATUS, PLAN_ID, null, PN.USER_ID, + PN.CATEGORY, OPEN_DATE, CLOSE_DATE, + NVL2(PC.ID, 1, 0) COLLECTED FROM POPUP P JOIN PLAN PN ON P.PLAN_ID = PN.ID + LEFT JOIN POPUP_COLLECTION PC + ON P.ID = PC.POPUP_ID + AND PC.USER_ID = #{userId} + AND PC.DELETED = 0 WHERE P.ID = #{popupId} AND P.DELETED = 0 @@ -94,8 +99,8 @@ SELECT P.ID, TITLE, DESCRIPTION, PULLED_DATE, WITHDRAWAL_STATUS, PLAN_ID, - PI.URL AS thumbnail, PN.CATEGORY, - OPEN_DATE, CLOSE_DATE + PI.URL thumbnail, PN.USER_ID userId, PN.CATEGORY, + OPEN_DATE, CLOSE_DATE, 1 FROM POPUP P LEFT JOIN POPUP_IMAGE PI ON P.ID = PI.POPUP_ID diff --git a/src/main/resources/mybatis/mybatis-config.xml b/src/main/resources/mybatis/mybatis-config.xml index bc57a30..9eef45c 100644 --- a/src/main/resources/mybatis/mybatis-config.xml +++ b/src/main/resources/mybatis/mybatis-config.xml @@ -50,6 +50,7 @@ + diff --git a/src/test/java/com/oya/kr/community/mapper/CommunityMapperTest.java b/src/test/java/com/oya/kr/community/mapper/CommunityMapperTest.java index 6b4496d..0f5f026 100644 --- a/src/test/java/com/oya/kr/community/mapper/CommunityMapperTest.java +++ b/src/test/java/com/oya/kr/community/mapper/CommunityMapperTest.java @@ -1,5 +1,6 @@ package com.oya.kr.community.mapper; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatCode; import static com.oya.kr.community.exception.CommunityErrorCodeList.*; import static org.junit.jupiter.api.Assertions.*; @@ -209,6 +210,23 @@ void statistics(){ assertEquals(count1+1, count2); } + /** + * @author 김유빈 + * @since 2024.02.26 + */ + @DisplayName("existCollection() : 스크랩 여부를 확인할 수 있다.") + @Test + void existCollection() { + // given + saveBasicCommunity(); + + // when + boolean collected = communityMapper.existCollection(communityId, user.getId()); + + // then + assertThat(collected).isFalse(); + } + private int staticCount(){ List response = communityMapper.statistics(); Map categoryCounts = response.stream() diff --git a/src/test/java/com/oya/kr/popup/mapper/PlanMapperTest.java b/src/test/java/com/oya/kr/popup/mapper/PlanMapperTest.java index 3b51fd6..0604e45 100644 --- a/src/test/java/com/oya/kr/popup/mapper/PlanMapperTest.java +++ b/src/test/java/com/oya/kr/popup/mapper/PlanMapperTest.java @@ -34,6 +34,9 @@ public class PlanMapperTest extends SpringApplicationTest { @Autowired private PlanMapper planMapper; + @Autowired + private PopupMapper popupMapper; + @Autowired private UserMapper userMapper; @@ -52,6 +55,7 @@ public class PlanMapperTest extends SpringApplicationTest { @BeforeEach void setUp() { businessMapper.deleteAll(); + popupMapper.deleteAll(); planMapper.deleteAll(); userMapper.deleteAll(); } @@ -65,6 +69,7 @@ void setUp() { @AfterEach void init() { businessMapper.deleteAll(); + popupMapper.deleteAll(); planMapper.deleteAll(); userMapper.deleteAll(); } diff --git a/src/test/java/com/oya/kr/popup/mapper/PopupMapperTest.java b/src/test/java/com/oya/kr/popup/mapper/PopupMapperTest.java index bf84f39..4293947 100644 --- a/src/test/java/com/oya/kr/popup/mapper/PopupMapperTest.java +++ b/src/test/java/com/oya/kr/popup/mapper/PopupMapperTest.java @@ -23,6 +23,7 @@ import com.oya.kr.popup.domain.enums.WithdrawalStatus; import com.oya.kr.popup.mapper.dto.request.PlanSaveMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupCollectionSaveMapperRequest; +import com.oya.kr.popup.mapper.dto.request.PopupDetailMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSaveMapperRequest; import com.oya.kr.popup.mapper.dto.request.PopupSearchMapperRequest; import com.oya.kr.popup.mapper.dto.response.PopupDetailMapperResponse; @@ -137,7 +138,9 @@ void findByIdWithDate() { popupMapper.save(request); // when - Optional mapperResponse = popupMapper.findByIdWithDate(request.getPopupId()); + Optional mapperResponse = popupMapper.findByIdWithDate( + new PopupDetailMapperRequest(request.getPopupId(), savedUser.getId()) + ); // then assertThat(mapperResponse).isPresent();