From 45a7d627f762781fd98b21d88f4610ca66ee3bf2 Mon Sep 17 00:00:00 2001 From: squareCaaat <140076739+squareCaaat@users.noreply.github.com> Date: Mon, 25 Nov 2024 17:40:40 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=97=B0?= =?UTF-8?q?=EA=B4=80=20=EA=B4=80=EA=B3=84=20=EB=A7=A4=ED=95=91=20=EB=B0=8F?= =?UTF-8?q?=20=EC=A2=8B=EC=95=84=EC=9A=94=ED=95=9C=20=EB=A9=94=EB=89=B4=20?= =?UTF-8?q?=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cano_be/controller/LikeController.java | 41 +++++++++++++++++++ .../cano_be/controller/MenuController.java | 16 -------- .../java/com/wap/cano_be/domain/Like.java | 2 + .../java/com/wap/cano_be/domain/Member.java | 5 +++ .../wap/cano_be/service/impl/LikeService.java | 12 ++++++ 5 files changed, 60 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/wap/cano_be/controller/LikeController.java diff --git a/src/main/java/com/wap/cano_be/controller/LikeController.java b/src/main/java/com/wap/cano_be/controller/LikeController.java new file mode 100644 index 0000000..02f3ad9 --- /dev/null +++ b/src/main/java/com/wap/cano_be/controller/LikeController.java @@ -0,0 +1,41 @@ +package com.wap.cano_be.controller; + +import com.wap.cano_be.domain.PrincipalDetail; +import com.wap.cano_be.service.impl.LikeService; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api") +public class LikeController { + private final LikeService likeService; + + public LikeController(LikeService likeService) { + this.likeService = likeService; + } + + // 좋아요 등록 + @PostMapping("/menus/{menu_id}/like") + public ResponseEntity like( + @PathVariable("menu_id") long menuId, + @AuthenticationPrincipal PrincipalDetail principalDetail){ + return likeService.insert(principalDetail.getMember().getId(), menuId); + } + + // [TEST] 좋아요 한 메뉴 조회 + @GetMapping("/likes/me") + public ResponseEntity getLikes( + @AuthenticationPrincipal PrincipalDetail principalDetail + ) { + return likeService.getLikedMenus(principalDetail.getMember().getId()); + } + + // 좋아요 취소 + @DeleteMapping("/menus/{menu_id}/like") + public ResponseEntity unlike( + @PathVariable("menu_id") long menuId, + @AuthenticationPrincipal PrincipalDetail principalDetail){ + return likeService.delete(principalDetail.getMember().getId(), menuId); + } +} diff --git a/src/main/java/com/wap/cano_be/controller/MenuController.java b/src/main/java/com/wap/cano_be/controller/MenuController.java index f8cdb50..070285f 100644 --- a/src/main/java/com/wap/cano_be/controller/MenuController.java +++ b/src/main/java/com/wap/cano_be/controller/MenuController.java @@ -128,20 +128,4 @@ public ResponseEntity reportMenu(@RequestBody MenuReportDto menuReportDto){ log.info("menuReportDto: {}", menuReportDto); return getSuccessResponse(); } - - // 좋아요 등록 - @PostMapping("/{menu_id}/like") - public ResponseEntity like( - @PathVariable("menu_id") long menuId, - @AuthenticationPrincipal PrincipalDetail principalDetail){ - return likeService.insert(principalDetail.getMember().getId(), menuId); - } - - // 좋아요 취소 - @DeleteMapping("/{menu_id}/like") - public ResponseEntity unlike( - @PathVariable("menu_id") long menuId, - @AuthenticationPrincipal PrincipalDetail principalDetail){ - return likeService.delete(principalDetail.getMember().getId(), menuId); - } } diff --git a/src/main/java/com/wap/cano_be/domain/Like.java b/src/main/java/com/wap/cano_be/domain/Like.java index efac382..ee66a64 100644 --- a/src/main/java/com/wap/cano_be/domain/Like.java +++ b/src/main/java/com/wap/cano_be/domain/Like.java @@ -2,8 +2,10 @@ import jakarta.persistence.*; import lombok.Builder; +import lombok.Getter; @Entity +@Getter @Table(name = "menu_like") public class Like { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/com/wap/cano_be/domain/Member.java b/src/main/java/com/wap/cano_be/domain/Member.java index 850b24a..f6e02f5 100644 --- a/src/main/java/com/wap/cano_be/domain/Member.java +++ b/src/main/java/com/wap/cano_be/domain/Member.java @@ -6,6 +6,9 @@ import jakarta.persistence.*; import lombok.*; +import java.util.ArrayList; +import java.util.List; + @Entity @Getter @Setter @@ -32,4 +35,6 @@ public class Member { private MemberRole role; @Enumerated(EnumType.STRING) private Gender gender; + @OneToMany(mappedBy = "member", cascade = CascadeType.REMOVE, orphanRemoval = true) + private List likes = new ArrayList<>(); } diff --git a/src/main/java/com/wap/cano_be/service/impl/LikeService.java b/src/main/java/com/wap/cano_be/service/impl/LikeService.java index 81ba477..d2565a7 100644 --- a/src/main/java/com/wap/cano_be/service/impl/LikeService.java +++ b/src/main/java/com/wap/cano_be/service/impl/LikeService.java @@ -12,6 +12,9 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.stream.Collectors; + @Service @Transactional public class LikeService { @@ -58,4 +61,13 @@ public ResponseEntity delete(long memberId, long menuId){ return ResponseEntity.status(HttpStatus.CONFLICT).build(); } + // [TEST] 좋아요한 메뉴 찾기 + public ResponseEntity> getLikedMenus(long memberId){ + Member member = memberRepository.findById(memberId).orElseThrow(()->new IllegalArgumentException("Member not found")); + return ResponseEntity.ok().body( + member.getLikes().stream() + .map(Like::getMenu) + .collect(Collectors.toList())); + } + }