-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #244 from woowacourse-teams/refactor/236-Split-Lik…
…edCafe LikedCafe 코드 분리
- Loading branch information
Showing
12 changed files
with
295 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/main/java/com/project/yozmcafe/controller/LikedCafeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
server/src/main/java/com/project/yozmcafe/domain/member/MemberRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
43 changes: 43 additions & 0 deletions
43
server/src/main/java/com/project/yozmcafe/service/LikedCafeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.