Skip to content

Commit

Permalink
Merge pull request #67 from Travel-in-nanaland/refactor/#66-post-like
Browse files Browse the repository at this point in the history
[Refactor] : 게시물 좋아요 토글 반환형에 boolean favorite 추가
  • Loading branch information
heeeeeseok authored Apr 23, 2024
2 parents f47f6ce + 8027377 commit 270bf2c
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS;

import com.jeju.nanaland.domain.experience.service.ExperienceService;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.member.dto.MemberResponse.MemberInfoDto;
import com.jeju.nanaland.global.BaseResponse;
import com.jeju.nanaland.global.jwt.AuthMember;
Expand Down Expand Up @@ -34,9 +35,12 @@ public class ExperienceController {
@ApiResponse(responseCode = "500", description = "서버측 에러", content = @Content)
})
@PostMapping("/like/{id}")
public BaseResponse<String> toggleLikeStatus(@AuthMember MemberInfoDto memberInfoDto,
public BaseResponse<FavoriteResponse.StatusDto> toggleLikeStatus(
@AuthMember MemberInfoDto memberInfoDto,
@PathVariable Long id) {
String result = experienceService.toggleLikeStatus(memberInfoDto.getMember(), id);
return BaseResponse.success(POST_LIKE_TOGGLE_SUCCESS, result);

return BaseResponse.success(
POST_LIKE_TOGGLE_SUCCESS,
experienceService.toggleLikeStatus(memberInfoDto.getMember(), id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.jeju.nanaland.domain.common.data.CategoryContent;
import com.jeju.nanaland.domain.experience.repository.ExperienceRepository;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.service.FavoriteService;
import com.jeju.nanaland.domain.member.entity.Member;
import com.jeju.nanaland.global.exception.BadRequestException;
Expand All @@ -19,10 +20,13 @@ public class ExperienceService {
private final FavoriteService favoriteService;

@Transactional
public String toggleLikeStatus(Member member, Long postId) {
public FavoriteResponse.StatusDto toggleLikeStatus(Member member, Long postId) {
experienceRepository.findById(postId)
.orElseThrow(() -> new BadRequestException("해당 id의 이색체험 게시물이 존재하지 않습니다."));

return favoriteService.toggleLikeStatus(member, CategoryContent.EXPERIENCE, postId);
Boolean status = favoriteService.toggleLikeStatus(member, CategoryContent.EXPERIENCE, postId);
return FavoriteResponse.StatusDto.builder()
.isFavorite(status)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jeju.nanaland.domain.favorite.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Data;

public class FavoriteResponse {

@Data
@Builder
public static class StatusDto {

@Schema(description = "좋아요 상태")
private boolean isFavorite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public List<Long> getMemberFavoritePostIds(Member member, CategoryContent catego
}

@Transactional
public String toggleLikeStatus(Member member, CategoryContent categoryContent, Long postId) {
public Boolean toggleLikeStatus(Member member, CategoryContent categoryContent, Long postId) {

Category category = getCategoryFromCategoryContent(categoryContent);

Expand All @@ -55,7 +55,7 @@ public String toggleLikeStatus(Member member, CategoryContent categoryContent, L

// 좋아요 삭제
favoriteRepository.delete(favorite);
return "좋아요 삭제";
return false;
}
// 좋아요 상태가 아닐 때
else {
Expand All @@ -67,7 +67,7 @@ public String toggleLikeStatus(Member member, CategoryContent categoryContent, L

// 좋아요 추가
favoriteRepository.save(favorite);
return "좋아요 추가";
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS;

import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.festival.service.FestivalService;
import com.jeju.nanaland.domain.member.dto.MemberResponse.MemberInfoDto;
import com.jeju.nanaland.global.BaseResponse;
Expand Down Expand Up @@ -34,9 +35,12 @@ public class FestivalController {
@ApiResponse(responseCode = "500", description = "서버측 에러", content = @Content)
})
@PostMapping("/like/{id}")
public BaseResponse<String> toggleLikeStatus(@AuthMember MemberInfoDto memberInfoDto,
public BaseResponse<FavoriteResponse.StatusDto> toggleLikeStatus(
@AuthMember MemberInfoDto memberInfoDto,
@PathVariable Long id) {
String result = festivalService.toggleLikeStatus(memberInfoDto.getMember(), id);
return BaseResponse.success(POST_LIKE_TOGGLE_SUCCESS, result);

return BaseResponse.success(
POST_LIKE_TOGGLE_SUCCESS,
festivalService.toggleLikeStatus(memberInfoDto.getMember(), id));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jeju.nanaland.domain.festival.service;

import com.jeju.nanaland.domain.common.data.CategoryContent;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.service.FavoriteService;
import com.jeju.nanaland.domain.festival.repository.FestivalRepository;
import com.jeju.nanaland.domain.member.entity.Member;
Expand All @@ -19,10 +20,13 @@ public class FestivalService {
private final FavoriteService favoriteService;

@Transactional
public String toggleLikeStatus(Member member, Long postId) {
public FavoriteResponse.StatusDto toggleLikeStatus(Member member, Long postId) {
festivalRepository.findById(postId)
.orElseThrow(() -> new BadRequestException("해당 id의 축제 게시물이 존재하지 않습니다."));

return favoriteService.toggleLikeStatus(member, CategoryContent.FESTIVAL, postId);
Boolean status = favoriteService.toggleLikeStatus(member, CategoryContent.FESTIVAL, postId);
return FavoriteResponse.StatusDto.builder()
.isFavorite(status)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS;

import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.market.service.MarketService;
import com.jeju.nanaland.domain.member.dto.MemberResponse.MemberInfoDto;
import com.jeju.nanaland.global.BaseResponse;
Expand Down Expand Up @@ -34,9 +35,12 @@ public class MarketController {
@ApiResponse(responseCode = "500", description = "서버측 에러", content = @Content)
})
@PostMapping("/like/{id}")
public BaseResponse<String> toggleLikeStatus(@AuthMember MemberInfoDto memberInfoDto,
public BaseResponse<FavoriteResponse.StatusDto> toggleLikeStatus(
@AuthMember MemberInfoDto memberInfoDto,
@PathVariable Long id) {
String result = marketService.toggleLikeStatus(memberInfoDto.getMember(), id);
return BaseResponse.success(POST_LIKE_TOGGLE_SUCCESS, result);

return BaseResponse.success(
POST_LIKE_TOGGLE_SUCCESS,
marketService.toggleLikeStatus(memberInfoDto.getMember(), id));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.jeju.nanaland.domain.market.service;

import com.jeju.nanaland.domain.common.data.CategoryContent;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse.StatusDto;
import com.jeju.nanaland.domain.favorite.service.FavoriteService;
import com.jeju.nanaland.domain.market.repository.MarketRepository;
import com.jeju.nanaland.domain.member.entity.Member;
Expand All @@ -19,10 +21,13 @@ public class MarketService {
private final FavoriteService favoriteService;

@Transactional
public String toggleLikeStatus(Member member, Long postId) {
public StatusDto toggleLikeStatus(Member member, Long postId) {
marketRepository.findById(postId)
.orElseThrow(() -> new BadRequestException("해당 id의 전통시장 게시물이 존재하지 않습니다."));

return favoriteService.toggleLikeStatus(member, CategoryContent.MARKET, postId);
Boolean status = favoriteService.toggleLikeStatus(member, CategoryContent.MARKET, postId);
return FavoriteResponse.StatusDto.builder()
.isFavorite(status)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS;

import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.member.dto.MemberResponse.MemberInfoDto;
import com.jeju.nanaland.domain.nana.dto.NanaResponse;
import com.jeju.nanaland.domain.nana.dto.NanaResponse.NanaDetailDto;
Expand Down Expand Up @@ -77,9 +78,12 @@ public BaseResponse<NanaDetailDto> nanaDetail(@PathVariable(name = "id") Long id
@ApiResponse(responseCode = "500", description = "서버측 에러", content = @Content)
})
@PostMapping("/like/{id}")
public BaseResponse<String> toggleLikeStatus(@AuthMember MemberInfoDto memberInfoDto,
public BaseResponse<FavoriteResponse.StatusDto> toggleLikeStatus(
@AuthMember MemberInfoDto memberInfoDto,
@PathVariable Long id) {
String result = nanaService.toggleLikeStatus(memberInfoDto.getMember(), id);
return BaseResponse.success(POST_LIKE_TOGGLE_SUCCESS, result);

return BaseResponse.success(
POST_LIKE_TOGGLE_SUCCESS,
nanaService.toggleLikeStatus(memberInfoDto.getMember(), id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.jeju.nanaland.domain.common.data.CategoryContent;
import com.jeju.nanaland.domain.common.entity.Locale;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse.StatusDto;
import com.jeju.nanaland.domain.favorite.service.FavoriteService;
import com.jeju.nanaland.domain.member.entity.Member;
import com.jeju.nanaland.domain.nana.dto.NanaResponse;
Expand Down Expand Up @@ -99,11 +101,14 @@ public NanaResponse.NanaDetailDto getNanaDetail(Long id) {
}

@Transactional
public String toggleLikeStatus(Member member, Long postId) {
public StatusDto toggleLikeStatus(Member member, Long postId) {
nanaRepository.findById(postId)
.orElseThrow(() -> new BadRequestException("해당 id의 나나스픽 게시물이 존재하지 않습니다."));

return favoriteService.toggleLikeStatus(member, CategoryContent.NANA, postId);
Boolean status = favoriteService.toggleLikeStatus(member, CategoryContent.NANA, postId);
return FavoriteResponse.StatusDto.builder()
.isFavorite(status)
.build();
}

// nanaContent의 AdditionalInfo dto로 바꾸기
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static com.jeju.nanaland.global.exception.SuccessCode.POST_LIKE_TOGGLE_SUCCESS;

import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.member.dto.MemberResponse.MemberInfoDto;
import com.jeju.nanaland.domain.nature.service.NatureService;
import com.jeju.nanaland.global.BaseResponse;
Expand Down Expand Up @@ -35,9 +36,12 @@ public class NatureController {
@ApiResponse(responseCode = "500", description = "서버측 에러", content = @Content)
})
@PostMapping("/like/{id}")
public BaseResponse<String> toggleLikeStatus(@AuthMember MemberInfoDto memberInfoDto,
public BaseResponse<FavoriteResponse.StatusDto> toggleLikeStatus(
@AuthMember MemberInfoDto memberInfoDto,
@PathVariable Long id) {
String result = natureService.toggleLikeStatus(memberInfoDto.getMember(), id);
return BaseResponse.success(POST_LIKE_TOGGLE_SUCCESS, result);

return BaseResponse.success(
POST_LIKE_TOGGLE_SUCCESS,
natureService.toggleLikeStatus(memberInfoDto.getMember(), id));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jeju.nanaland.domain.nature.service;

import com.jeju.nanaland.domain.common.data.CategoryContent;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.service.FavoriteService;
import com.jeju.nanaland.domain.member.entity.Member;
import com.jeju.nanaland.domain.nature.repository.NatureRepository;
Expand All @@ -19,10 +20,13 @@ public class NatureService {
private final FavoriteService favoriteService;

@Transactional
public String toggleLikeStatus(Member member, Long postId) {
public FavoriteResponse.StatusDto toggleLikeStatus(Member member, Long postId) {
natureRepository.findById(postId)
.orElseThrow(() -> new BadRequestException("해당 id의 7대자연 게시물이 존재하지 않습니다."));

return favoriteService.toggleLikeStatus(member, CategoryContent.NATURE, postId);
Boolean status = favoriteService.toggleLikeStatus(member, CategoryContent.NATURE, postId);
return FavoriteResponse.StatusDto.builder()
.isFavorite(status)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import com.jeju.nanaland.domain.common.entity.Language;
import com.jeju.nanaland.domain.common.entity.Locale;
import com.jeju.nanaland.domain.experience.entity.Experience;
import com.jeju.nanaland.domain.favorite.entity.Favorite;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.repository.FavoriteRepository;
import com.jeju.nanaland.domain.member.entity.Member;
import com.jeju.nanaland.domain.member.entity.Provider;
import com.jeju.nanaland.global.exception.BadRequestException;
import jakarta.persistence.EntityManager;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -98,22 +97,19 @@ void toggleLikeStatusTest() {
* member2 : toggleLikeStatus 1번 적용
*/
experienceService.toggleLikeStatus(member1, experience.getId());
experienceService.toggleLikeStatus(member1, experience.getId());

experienceService.toggleLikeStatus(member2, experience.getId());
FavoriteResponse.StatusDto result1 = experienceService.toggleLikeStatus(member1,
experience.getId());

Optional<Favorite> favoriteOptional1 =
favoriteRepository.findByMemberAndCategoryAndPostId(member1, category, experience.getId());
Optional<Favorite> favoriteOptional2 =
favoriteRepository.findByMemberAndCategoryAndPostId(member2, category, experience.getId());
FavoriteResponse.StatusDto result2 = experienceService.toggleLikeStatus(member2,
experience.getId());

/**
* THEN
*
* member1 = 좋아요 X, member2 = 좋아요
*/
assertThat(favoriteOptional1.isPresent()).isFalse();
assertThat(favoriteOptional2.isPresent()).isTrue();
assertThat(result1.isFavorite()).isFalse();
assertThat(result2.isFavorite()).isTrue();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@
import com.jeju.nanaland.domain.common.entity.ImageFile;
import com.jeju.nanaland.domain.common.entity.Language;
import com.jeju.nanaland.domain.common.entity.Locale;
import com.jeju.nanaland.domain.favorite.entity.Favorite;
import com.jeju.nanaland.domain.favorite.dto.FavoriteResponse;
import com.jeju.nanaland.domain.favorite.repository.FavoriteRepository;
import com.jeju.nanaland.domain.festival.entity.Festival;
import com.jeju.nanaland.domain.member.entity.Member;
import com.jeju.nanaland.domain.member.entity.Provider;
import com.jeju.nanaland.global.exception.BadRequestException;
import jakarta.persistence.EntityManager;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -98,22 +97,19 @@ void toggleLikeStatusTest() {
* member2 : toggleLikeStatus 1번 적용
*/
festivalService.toggleLikeStatus(member1, festival.getId());
festivalService.toggleLikeStatus(member1, festival.getId());

festivalService.toggleLikeStatus(member2, festival.getId());
FavoriteResponse.StatusDto result1 = festivalService.toggleLikeStatus(member1,
festival.getId());

Optional<Favorite> favoriteOptional1 =
favoriteRepository.findByMemberAndCategoryAndPostId(member1, category, festival.getId());
Optional<Favorite> favoriteOptional2 =
favoriteRepository.findByMemberAndCategoryAndPostId(member2, category, festival.getId());
FavoriteResponse.StatusDto result2 = festivalService.toggleLikeStatus(member2,
festival.getId());

/**
* THEN
*
* member1 = 좋아요 X, member2 = 좋아요
*/
assertThat(favoriteOptional1.isPresent()).isFalse();
assertThat(favoriteOptional2.isPresent()).isTrue();
assertThat(result1.isFavorite()).isFalse();
assertThat(result2.isFavorite()).isTrue();
}

@Test
Expand Down
Loading

0 comments on commit 270bf2c

Please sign in to comment.