Skip to content

Commit

Permalink
feat: 좋아요 연관 관계 매핑 및 좋아요한 메뉴 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
squareCaaat committed Nov 25, 2024
1 parent 6b44d3d commit 2225160
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 16 deletions.
41 changes: 41 additions & 0 deletions src/main/java/com/wap/cano_be/controller/LikeController.java
Original file line number Diff line number Diff line change
@@ -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("/{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("/{menu_id}/like")
public ResponseEntity<?> unlike(
@PathVariable("menu_id") long menuId,
@AuthenticationPrincipal PrincipalDetail principalDetail){
return likeService.delete(principalDetail.getMember().getId(), menuId);
}
}
16 changes: 0 additions & 16 deletions src/main/java/com/wap/cano_be/controller/MenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/wap/cano_be/domain/Like.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/wap/cano_be/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import jakarta.persistence.*;
import lombok.*;

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

@Entity
@Getter
@Setter
Expand All @@ -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<Like> likes = new ArrayList<>();
}
12 changes: 12 additions & 0 deletions src/main/java/com/wap/cano_be/service/impl/LikeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -58,4 +61,13 @@ public ResponseEntity<?> delete(long memberId, long menuId){
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}

// [TEST] 좋아요한 메뉴 찾기
public ResponseEntity<List<Menu>> 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()));
}

}

0 comments on commit 2225160

Please sign in to comment.