Skip to content

Commit

Permalink
[REFACTOR] 팝업스토어, 커뮤니티 게시글에 스크랩 여부 및 작성 여부 추가 (#22)
Browse files Browse the repository at this point in the history
* [REFACTOR] 팝업스토어 게시글 상세정보에 작성/스크랩 여부 추가

* [REFACTOR] 커뮤니티 게시글 상세정보에 작성/스크랩 여부 추가
  • Loading branch information
kyukong authored Feb 26, 2024
1 parent e571803 commit 5ae05da
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
@Getter
@RequiredArgsConstructor
public class CommunityDetailResponse {

private final long writeId;
private final String nickname;
private final String email;
Expand All @@ -33,7 +34,11 @@ public class CommunityDetailResponse {
private final List<VoteResponse> voteResponseList;
private final List<String> imageList;

public static CommunityDetailResponse from(List<String> imageList, Community community, int countView, List<VoteResponse> voteResponseList) {
private final boolean written;
private final boolean collected;

public static CommunityDetailResponse from(List<String> imageList, Community community, int countView,
List<VoteResponse> voteResponseList, Long userId, boolean collected) {

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
String createdDateString = community.getCreatedDate().format(formatter);
Expand All @@ -54,7 +59,9 @@ public static CommunityDetailResponse from(List<String> imageList, Community com
modifiedDateString,
countView,
voteResponseList,
imageList
imageList,
userId.equals(community.getUser().getId()),
collected
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<VoteResponse> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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()
);
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/oya/kr/popup/mapper/PopupMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,7 @@ public interface PopupMapper {

Optional<PopupMapperResponse> findById(Long popupId);

Optional<PopupDetailMapperResponse> findByIdWithDate(Long popupId);
Optional<PopupDetailMapperResponse> findByIdWithDate(PopupDetailMapperRequest request);

List<PopupMapperResponse> findAllByPlanId(Long planId);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/oya/kr/popup/service/PopupService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/main/resources/com/oya/kr/community/mapper/CommunityMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,23 @@
AND CC.DELETED = 0
</select>

<!--
스크랩 여부 반환 추가
@author 김유빈
@since 2024.02.26
-->
<select id="existCollection" resultType="boolean">
SELECT NVL2(CC.ID, 1, 0) COLLECTED
FROM COMMUNITY C
LEFT JOIN COMMUNITY_COLLECTION CC
ON C.ID = CC.COMMUNITY_ID
AND USER_ID = #{userId}
AND CC.DELETED = 0
WHERE C.ID = #{communityId}
AND C.DELETED = 0
</select>

<sql id="selecCommunity">
SELECT
c.ID,
Expand Down
15 changes: 10 additions & 5 deletions src/main/resources/com/oya/kr/popup/mapper/PopupMapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
AND DELETED = 0
</select>

<select id="findByIdWithDate" parameterType="Long" resultType="popupDetailMapperResponse">
SELECT P.ID, TITLE, DESCRIPTION, PULLED_DATE, WITHDRAWAL_STATUS, PLAN_ID, null,
PN.CATEGORY, OPEN_DATE, CLOSE_DATE
<select id="findByIdWithDate" parameterType="popupDetailMapperRequest" resultType="popupDetailMapperResponse">
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
</select>
Expand Down Expand Up @@ -94,8 +99,8 @@

<sql id="selectPopupWithPlanAndPopupImage">
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
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/mybatis/mybatis-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<typeAlias type="com.oya.kr.popup.mapper.dto.request.PopupViewCreateOrUpdateMapperRequest" alias="popupViewCreateOrUpdateMapperRequest"/>
<typeAlias type="com.oya.kr.popup.mapper.dto.request.PopupCollectionMapperRequest" alias="popupCollectionMapperRequest"/>
<typeAlias type="com.oya.kr.popup.mapper.dto.request.PopupCollectionSaveMapperRequest" alias="popupCollectionSaveMapperRequest"/>
<typeAlias type="com.oya.kr.popup.mapper.dto.request.PopupDetailMapperRequest" alias="popupDetailMapperRequest"/>

<!-- user -->
<typeAlias type="com.oya.kr.user.mapper.dto.request.SignupAdministratorMapperRequest" alias="SignupUserRequest"/>
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/com/oya/kr/community/mapper/CommunityMapperTest.java
Original file line number Diff line number Diff line change
@@ -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.*;
Expand Down Expand Up @@ -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<StatisticsResponseMapper> response = communityMapper.statistics();
Map<String, Integer> categoryCounts = response.stream()
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/oya/kr/popup/mapper/PlanMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class PlanMapperTest extends SpringApplicationTest {
@Autowired
private PlanMapper planMapper;

@Autowired
private PopupMapper popupMapper;

@Autowired
private UserMapper userMapper;

Expand All @@ -52,6 +55,7 @@ public class PlanMapperTest extends SpringApplicationTest {
@BeforeEach
void setUp() {
businessMapper.deleteAll();
popupMapper.deleteAll();
planMapper.deleteAll();
userMapper.deleteAll();
}
Expand All @@ -65,6 +69,7 @@ void setUp() {
@AfterEach
void init() {
businessMapper.deleteAll();
popupMapper.deleteAll();
planMapper.deleteAll();
userMapper.deleteAll();
}
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/com/oya/kr/popup/mapper/PopupMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,7 +138,9 @@ void findByIdWithDate() {
popupMapper.save(request);

// when
Optional<PopupDetailMapperResponse> mapperResponse = popupMapper.findByIdWithDate(request.getPopupId());
Optional<PopupDetailMapperResponse> mapperResponse = popupMapper.findByIdWithDate(
new PopupDetailMapperRequest(request.getPopupId(), savedUser.getId())
);

// then
assertThat(mapperResponse).isPresent();
Expand Down

0 comments on commit 5ae05da

Please sign in to comment.