Skip to content

Commit

Permalink
Merge pull request #244 from woowacourse-teams/refactor/236-Split-Lik…
Browse files Browse the repository at this point in the history
…edCafe

LikedCafe 코드 분리
  • Loading branch information
donghae-kim authored Aug 1, 2023
2 parents 6ab4cca + 3e0b6c0 commit 1b2475a
Show file tree
Hide file tree
Showing 12 changed files with 295 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.project.yozmcafe.controller.dto.cafe.CafeResponse;
import com.project.yozmcafe.domain.member.Member;
import com.project.yozmcafe.service.CafeService;
import com.project.yozmcafe.service.MemberService;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
Expand All @@ -22,11 +21,9 @@ public class CafeController {
private static final int IGNORED_PAGE = 0;

private final CafeService cafeService;
private final MemberService memberService;

public CafeController(final CafeService cafeService, final MemberService memberService) {
public CafeController(final CafeService cafeService) {
this.cafeService = cafeService;
this.memberService = memberService;
}

@GetMapping
Expand All @@ -43,14 +40,6 @@ public ResponseEntity<List<CafeResponse>> getCafes(@PageableDefault(size = PAGE_
return ResponseEntity.ok(cafeResponses);
}

@PostMapping("/{cafeId}/likes")
public ResponseEntity<Void> updateLikes(final Member member,
@PathVariable("cafeId") final long cafeId,
@RequestParam("isLiked") final boolean isLiked) {
memberService.updateLike(member, cafeId, isLiked);
return ResponseEntity.ok().build();
}

@PostMapping
public ResponseEntity<Void> saveCafe(@RequestBody CafeRequest cafeRequest) {
cafeService.saveCafe(cafeRequest);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.project.yozmcafe.controller;

import com.project.yozmcafe.controller.dto.LikedCafeResponse;
import com.project.yozmcafe.domain.member.Member;
import com.project.yozmcafe.service.LikedCafeService;
import jakarta.websocket.server.PathParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class LikedCafeController {

private static final int PAGE_SIZE = 15;

private final LikedCafeService likedCafeService;

public LikedCafeController(final LikedCafeService likedCafeService) {
this.likedCafeService = likedCafeService;
}

@GetMapping("/members/{memberId}/liked-cafes")
public ResponseEntity<List<LikedCafeResponse>> getLikedCafes(@PathVariable("memberId") final String memberId,
@PathParam("page") final int page) {
final List<LikedCafeResponse> likedCafes = likedCafeService.findLikedCafesById(memberId, page, PAGE_SIZE);
return ResponseEntity.ok(likedCafes);
}

@PostMapping("/cafes/{cafeId}/likes")
public ResponseEntity<Void> updateLikes(final Member member,
@PathVariable("cafeId") final long cafeId,
@RequestParam("isLiked") final boolean isLiked) {
likedCafeService.updateLike(member, cafeId, isLiked);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package com.project.yozmcafe.controller;

import com.project.yozmcafe.controller.dto.LikedCafeResponse;
import com.project.yozmcafe.controller.dto.MemberResponse;
import com.project.yozmcafe.service.MemberService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/members")
public class MemberController {
Expand All @@ -28,11 +23,4 @@ public ResponseEntity<MemberResponse> findById(@PathVariable("memberId") final S
final MemberResponse member = memberService.findById(memberId);
return ResponseEntity.ok(member);
}

@GetMapping("/{memberId}/liked-cafes")
public ResponseEntity<List<LikedCafeResponse>> getLikedCafes(@PathVariable("memberId") final String memberId,
@PageableDefault(size = 15) final Pageable pageable) {
final List<LikedCafeResponse> likedCafes = memberService.findLikedCafesById(memberId, pageable);
return ResponseEntity.ok(likedCafes);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.project.yozmcafe.domain.member;

import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_LIKED_CAFE;
import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_UN_VIEWED_CAFE;

import java.util.ArrayList;
import java.util.List;

import com.project.yozmcafe.domain.cafe.Cafe;
import com.project.yozmcafe.domain.cafe.LikedCafe;
import com.project.yozmcafe.domain.cafe.UnViewedCafe;
import com.project.yozmcafe.exception.BadRequestException;
import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_LIKED_CAFE;
import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_UN_VIEWED_CAFE;

@Entity
public class Member {

Expand Down Expand Up @@ -90,7 +88,6 @@ private void removeLikedCafe(final Cafe cafe) {
cafe.subtractLikeCount();
}


public void addLikedCafe(Cafe cafe) {
likedCafes.add(new LikedCafe(cafe, this));
cafe.addLikeCount();
Expand Down Expand Up @@ -120,4 +117,14 @@ public List<UnViewedCafe> getUnViewedCafes() {
public List<LikedCafe> getLikedCafes() {
return likedCafes;
}

public List<LikedCafe> getLikedCafesByPaging(int pageNum, int pageSize) {
List<LikedCafe> reverseLikedCafes = new ArrayList<>(likedCafes);
Collections.reverse(reverseLikedCafes);

int startIndex = (pageNum - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, reverseLikedCafes.size());

return reverseLikedCafes.subList(startIndex, endIndex);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
package com.project.yozmcafe.domain.member;

import com.project.yozmcafe.domain.cafe.LikedCafe;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface MemberRepository extends JpaRepository<Member, String> {
Optional<Member> findById(String id);

@Query("SELECT likedCafes FROM Member m JOIN m.likedCafes likedCafes WHERE m.id = :memberId")
Slice<LikedCafe> findLikedCafesByMemberId(@Param("memberId") final String memberId, final Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.project.yozmcafe.service;

import com.project.yozmcafe.controller.dto.LikedCafeResponse;
import com.project.yozmcafe.domain.cafe.Cafe;
import com.project.yozmcafe.domain.cafe.CafeRepository;
import com.project.yozmcafe.domain.cafe.LikedCafe;
import com.project.yozmcafe.domain.member.Member;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class LikedCafeService {

private final CafeRepository cafeRepository;
private final MemberService memberService;

public LikedCafeService(final CafeRepository cafeRepository,
final MemberService memberService) {
this.cafeRepository = cafeRepository;
this.memberService = memberService;
}

public List<LikedCafeResponse> findLikedCafesById(final String memberId, final int pageNum, final int pageSize) {
final Member member = memberService.findMemberByIdOrElseThrow(memberId);

final List<LikedCafe> likedCafes = member.getLikedCafesByPaging(pageNum, pageSize);

return likedCafes.stream()
.map(LikedCafeResponse::from)
.toList();
}

@Transactional
public void updateLike(final Member member, final long cafeId, final boolean isLiked) {
final Cafe cafe = cafeRepository.findById(cafeId)
.orElseThrow(() -> new IllegalArgumentException("해당하는 카페가 존재하지 않습니다."));

member.updateLikedCafesBy(cafe, isLiked);
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,22 @@
package com.project.yozmcafe.service;

import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_CAFE;
import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.project.yozmcafe.controller.dto.LikedCafeResponse;
import com.project.yozmcafe.controller.dto.MemberResponse;
import com.project.yozmcafe.domain.cafe.Cafe;
import com.project.yozmcafe.domain.cafe.CafeRepository;
import com.project.yozmcafe.domain.cafe.LikedCafe;
import com.project.yozmcafe.domain.member.Member;
import com.project.yozmcafe.domain.member.MemberRepository;
import com.project.yozmcafe.exception.BadRequestException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.project.yozmcafe.exception.ErrorCode.NOT_EXISTED_MEMBER;

@Transactional(readOnly = true)
@Service
public class MemberService {

private final MemberRepository memberRepository;
private final CafeRepository cafeRepository;

public MemberService(final MemberRepository memberRepository, final CafeRepository cafeRepository) {
public MemberService(final MemberRepository memberRepository) {
this.memberRepository = memberRepository;
this.cafeRepository = cafeRepository;
}

public MemberResponse findById(final String memberId) {
Expand All @@ -37,25 +25,8 @@ public MemberResponse findById(final String memberId) {
return MemberResponse.from(member);
}

public List<LikedCafeResponse> findLikedCafesById(final String memberId, final Pageable pageable) {
final Member member = findMemberByIdOrElseThrow(memberId);

final Slice<LikedCafe> likedCafes = memberRepository.findLikedCafesByMemberId(member.getId(), pageable);

return likedCafes.stream()
.map(LikedCafeResponse::from)
.toList();
}

private Member findMemberByIdOrElseThrow(final String memberId) {
public Member findMemberByIdOrElseThrow(final String memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new BadRequestException(NOT_EXISTED_MEMBER));
}

@Transactional
public void updateLike(final Member member, final long cafeId, final boolean isLiked) {
final Cafe cafe = cafeRepository.findById(cafeId)
.orElseThrow(() -> new BadRequestException(NOT_EXISTED_CAFE));
member.updateLikedCafesBy(cafe, isLiked);
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
package com.project.yozmcafe.controller;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;

import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.jdbc.Sql;

import com.project.yozmcafe.controller.dto.LikedCafeResponse;
import com.project.yozmcafe.controller.dto.MemberResponse;
import com.project.yozmcafe.domain.cafe.Cafe;
Expand All @@ -31,9 +11,27 @@
import com.project.yozmcafe.service.auth.JwtTokenProvider;
import com.project.yozmcafe.service.auth.KakaoOAuthClient;
import com.project.yozmcafe.util.AcceptanceContext;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doReturn;
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql(scripts = {"classpath:truncate.sql"}, executionPhase = AFTER_TEST_METHOD)
Expand Down Expand Up @@ -93,15 +91,14 @@ void getLikedCafes() {
memberRepository.save(member);

//when
context.invokeHttpGet("/members/{memberId}/liked-cafes?size=1&page=1", member.getId());
context.invokeHttpGet("/members/{memberId}/liked-cafes?page=1", member.getId());
Response response = context.response;
context.invokeHttpGet("/members/{memberId}/liked-cafes?size=1&page=2", member.getId());
Response response2 = context.response;


//then
assertAll(
() -> assertThat(response.jsonPath().getLong("[0].cafeId")).isEqualTo(savedCafe1.getId()),
() -> assertThat(response2.jsonPath().getLong("[0].cafeId")).isEqualTo(savedCafe2.getId())
() -> assertThat(response.jsonPath().getLong("[0].cafeId")).isEqualTo(savedCafe2.getId()),
() -> assertThat(response.jsonPath().getLong("[1].cafeId")).isEqualTo(savedCafe1.getId())
);
}

Expand All @@ -113,7 +110,7 @@ void getLikedCafes_empty() {
memberRepository.save(member);

//when
context.invokeHttpGet("/members/{memberId}/liked-cafes?size=1&page=1", member.getId());
context.invokeHttpGet("/members/{memberId}/liked-cafes?page=1", member.getId());
Response response = context.response;

//then
Expand All @@ -135,7 +132,7 @@ void reLoginLikedListTest() {
//when
context.accessToken = "accessToken";
context.invokeHttpPostWithToken("/cafes/" + cafe.getId() + "/likes?isLiked=true");
relogin();
reLogin();
context.invokeHttpGet("/members/{memberId}/liked-cafes?size=1&page=1", member.getId());

final List<LikedCafeResponse> result = context.response.jsonPath().getList("");
Expand All @@ -144,8 +141,8 @@ void reLoginLikedListTest() {
assertThat(result).hasSize(1);
}

private Response relogin() {
return RestAssured.given()
private void reLogin() {
RestAssured.given()
.log().all()
.queryParam("code", "googleCode")
.when()
Expand Down
Loading

0 comments on commit 1b2475a

Please sign in to comment.