From e8e91abc44e9a957d4d01ad54acd254e354b30df Mon Sep 17 00:00:00 2001 From: acceptor-gyu Date: Wed, 14 Jun 2023 11:02:33 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B4=80=EC=8B=AC=20=EC=83=81=ED=92=88?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80(#34)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/secondhand/post/PostController.java | 10 ---------- .../com/secondhand/post/entity/Interest.java | 7 +++++++ .../com/secondhand/user/UserController.java | 16 +++++++++++++++ .../java/com/secondhand/user/UserService.java | 20 +++++++++++++++++++ 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/be/src/main/java/com/secondhand/post/PostController.java b/be/src/main/java/com/secondhand/post/PostController.java index 39d5cf86d..8a0ad7d6f 100644 --- a/be/src/main/java/com/secondhand/post/PostController.java +++ b/be/src/main/java/com/secondhand/post/PostController.java @@ -115,16 +115,6 @@ public ResponseEntity deletePost(@PathVariable Long postId, @Req "판매글 삭제 성공")); } - @PostMapping("/{postId}") - public ResponseEntity changeInterestPost(@PathVariable Long postId) { - return ResponseEntity - .ok() - .body(new CustomResponse( - "success", - 200, - "관심상품 추가 / 삭제 성공")); - } - @PatchMapping("/{postId}") public ResponseEntity changePostStatus(@PathVariable Long postId, @RequestBody UpdatePostStateDto stateDto, @RequestHeader("Authorization") String token) { diff --git a/be/src/main/java/com/secondhand/post/entity/Interest.java b/be/src/main/java/com/secondhand/post/entity/Interest.java index 2f3dbb71b..9cbcb9a12 100644 --- a/be/src/main/java/com/secondhand/post/entity/Interest.java +++ b/be/src/main/java/com/secondhand/post/entity/Interest.java @@ -2,11 +2,13 @@ import com.secondhand.user.entity.User; import lombok.Getter; +import lombok.NoArgsConstructor; import javax.persistence.*; @Entity @Getter +@NoArgsConstructor public class Interest { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -19,4 +21,9 @@ public class Interest { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_meta_id") private PostMeta postMeta; + + public Interest(User user, PostMeta postMeta) { + this.user = user; + this.postMeta = postMeta; + } } diff --git a/be/src/main/java/com/secondhand/user/UserController.java b/be/src/main/java/com/secondhand/user/UserController.java index d301bda55..f33b2c091 100644 --- a/be/src/main/java/com/secondhand/user/UserController.java +++ b/be/src/main/java/com/secondhand/user/UserController.java @@ -2,6 +2,7 @@ import com.secondhand.region.dto.PostMyRegionDto; import com.secondhand.user.login.JwtUtil; +import com.secondhand.user.login.dto.LoggedInUser; import com.secondhand.util.CustomResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -32,4 +33,19 @@ public ResponseEntity updateMyRegion(@RequestBody PostMyRegionDt "지역 등록 성공" )); } + + @PostMapping("/{postId}") + public ResponseEntity addInterestPost(@PathVariable Long postId, @RequestHeader("Authorization") String token) { + + LoggedInUser loggedInUser = jwtUtil.extractedUserFromToken(token); + + userService.addInterestPost(postId, loggedInUser); + + return ResponseEntity + .ok() + .body(new CustomResponse( + "success", + 200, + "관심상품 추가 / 삭제 성공")); + } } diff --git a/be/src/main/java/com/secondhand/user/UserService.java b/be/src/main/java/com/secondhand/user/UserService.java index fa9bafaa0..36d740b9c 100644 --- a/be/src/main/java/com/secondhand/user/UserService.java +++ b/be/src/main/java/com/secondhand/user/UserService.java @@ -1,17 +1,27 @@ package com.secondhand.user; +import com.secondhand.post.entity.Interest; +import com.secondhand.post.entity.PostMeta; +import com.secondhand.post.repository.InterestRepository; +import com.secondhand.post.repository.PostMetaRepository; import com.secondhand.region.dto.PostMyRegionDto; import com.secondhand.user.entity.User; +import com.secondhand.user.login.dto.LoggedInUser; import com.secondhand.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import javax.transaction.Transactional; + @Service @RequiredArgsConstructor public class UserService { private final UserRepository userRepository; + private final PostMetaRepository postMetaRepository; + private final InterestRepository interestRepository; + @Transactional public void updateMyRegion(long userId, PostMyRegionDto postMyRegionDto) { User loggedInUser = userRepository.findById(userId) @@ -21,4 +31,14 @@ public void updateMyRegion(long userId, PostMyRegionDto postMyRegionDto) { userRepository.save(modifiedUser); } + + @Transactional + public void addInterestPost(long postId, LoggedInUser loggedInUser) { + + User user = userRepository.findById(loggedInUser.getId()).orElseThrow(); + PostMeta postMeta = postMetaRepository.findById(postId).orElseThrow(); + + Interest interest = new Interest(user, postMeta); + interestRepository.save(interest); + } }