Skip to content

Commit

Permalink
Merge pull request #69 from second-hand-team06/be-feat/#34-add-interest
Browse files Browse the repository at this point in the history
[BE] feat: 관심 상품 추가(#34)
  • Loading branch information
acceptor-gyu authored Jun 14, 2023
2 parents 8c491ad + e8e91ab commit bb1200e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
10 changes: 0 additions & 10 deletions be/src/main/java/com/secondhand/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,6 @@ public ResponseEntity<CustomResponse> deletePost(@PathVariable Long postId, @Req
"판매글 삭제 성공"));
}

@PostMapping("/{postId}")
public ResponseEntity<CustomResponse> changeInterestPost(@PathVariable Long postId) {
return ResponseEntity
.ok()
.body(new CustomResponse(
"success",
200,
"관심상품 추가 / 삭제 성공"));
}

@PatchMapping("/{postId}")
public ResponseEntity<CustomResponse> changePostStatus(@PathVariable Long postId, @RequestBody UpdatePostStateDto stateDto, @RequestHeader("Authorization") String token) {

Expand Down
7 changes: 7 additions & 0 deletions be/src/main/java/com/secondhand/post/entity/Interest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
16 changes: 16 additions & 0 deletions be/src/main/java/com/secondhand/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,4 +33,19 @@ public ResponseEntity<CustomResponse> updateMyRegion(@RequestBody PostMyRegionDt
"지역 등록 성공"
));
}

@PostMapping("/{postId}")
public ResponseEntity<CustomResponse> 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,
"관심상품 추가 / 삭제 성공"));
}
}
20 changes: 20 additions & 0 deletions be/src/main/java/com/secondhand/user/UserService.java
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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);
}
}

0 comments on commit bb1200e

Please sign in to comment.