diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/controller/ApiUserInterestController.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/controller/ApiUserInterestController.java new file mode 100644 index 00000000..019e47c0 --- /dev/null +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/controller/ApiUserInterestController.java @@ -0,0 +1,33 @@ +package com.example.restcontroller.user.controller; + +import com.auth0.jwt.exceptions.JWTVerificationException; +import com.example.restcontroller.board.model.ServiceResult; +import com.example.restcontroller.common.model.ResponseResult; +import com.example.restcontroller.user.service.UserService; +import com.example.restcontroller.util.JWTUtils; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequiredArgsConstructor +public class ApiUserInterestController { + + private final UserService userService; + + @PutMapping("/api/user/{id}/interest") + public ResponseEntity chapter3_18(@PathVariable Long id, + @RequestHeader("TOKEN") String token) { + String email = ""; + try { + email = JWTUtils.getIssuer(token); + } catch (JWTVerificationException e) { + return ResponseResult.fail("토큰 정보가 정확하지 않습니다."); + } + ServiceResult result = userService.addInterestUser(id, email); + return ResponseResult.result(result); + } +} diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/User.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/User.java index a10eaa39..c8e0f656 100644 --- a/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/User.java +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/User.java @@ -72,6 +72,14 @@ public class User { @OneToMany(mappedBy = "user") List boardBookmarkList = new ArrayList<>(); + @JsonIgnore + @OneToMany(mappedBy = "user") + List userList = new ArrayList<>(); + + @JsonIgnore + @OneToMany(mappedBy = "interestUser") + List userInterestList = new ArrayList<>(); + @PrePersist public void prePersist() { status = status == null ? UserStatus.USING : status; diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/UserInterest.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/UserInterest.java new file mode 100644 index 00000000..6af9633b --- /dev/null +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/entity/UserInterest.java @@ -0,0 +1,29 @@ +package com.example.restcontroller.user.entity; + +import lombok.*; + +import javax.persistence.*; +import java.time.LocalDateTime; + + +@Getter @Setter +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +public class UserInterest { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @ManyToOne + @JoinColumn(foreignKey = @ForeignKey(name = "FK_USER_INTEREST_USER_ID")) + private User user; + + @ManyToOne + @JoinColumn(foreignKey = @ForeignKey(name = "FK_USER_INTEREST_INTEREST_USER_ID")) + private User interestUser; + + private LocalDateTime regDate; + +} diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/repository/UserInterestRepository.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/repository/UserInterestRepository.java new file mode 100644 index 00000000..d7fcccdb --- /dev/null +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/repository/UserInterestRepository.java @@ -0,0 +1,12 @@ +package com.example.restcontroller.user.repository; + +import com.example.restcontroller.user.entity.User; +import com.example.restcontroller.user.entity.UserInterest; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserInterestRepository extends JpaRepository { + + Long countByUserAndInterestUser(User user, User interestUser); +} diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserService.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserService.java index b719d5bc..cb32f8be 100644 --- a/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserService.java +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserService.java @@ -1,5 +1,6 @@ package com.example.restcontroller.user.service; +import com.example.restcontroller.board.model.ServiceResult; import com.example.restcontroller.user.model.UserNoticeCount; import com.example.restcontroller.user.model.UserLogCount; import com.example.restcontroller.user.model.UserResponse; @@ -18,4 +19,6 @@ public interface UserService { List getUserLogCount(); List getUserLikeBest(); + + ServiceResult addInterestUser(Long id, String email); } diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserServiceImpl.java b/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserServiceImpl.java index c9d56320..513a4d4d 100644 --- a/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserServiceImpl.java +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/user/service/UserServiceImpl.java @@ -1,12 +1,16 @@ package com.example.restcontroller.user.service; +import com.example.restcontroller.board.model.ServiceResult; +import com.example.restcontroller.user.entity.User; +import com.example.restcontroller.user.entity.UserInterest; import com.example.restcontroller.user.entity.UserStatus; import com.example.restcontroller.user.model.UserNoticeCount; import com.example.restcontroller.user.model.UserLogCount; import com.example.restcontroller.user.model.UserResponse; import com.example.restcontroller.user.model.UserSummary; import com.example.restcontroller.user.repository.UserCustomRepository; +import com.example.restcontroller.user.repository.UserInterestRepository; import com.example.restcontroller.user.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -16,6 +20,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Service @@ -25,6 +30,7 @@ public class UserServiceImpl implements UserService { private final UserRepository userRepository; private final UserCustomRepository userCustomRepository; + private final UserInterestRepository userInterestRepository; @Override public UserSummary getUserStatusCount() { @@ -62,4 +68,37 @@ public class UserServiceImpl implements UserService { public List getUserLikeBest() { return userCustomRepository.findUserLikeBest(); } + + @Transactional + @Override + public ServiceResult addInterestUser(Long id, String email) { + Optional optionalUser = userRepository.findByEmail(email); + if (!optionalUser.isPresent()) { + return ServiceResult.fail("회원 정보가 없습니다."); + } + User userEntity = optionalUser.get(); + + Optional optionalInterestUser = userRepository.findById(id); + if (!optionalInterestUser.isPresent()) { + return ServiceResult.fail("관심 사용자에 추가할 회원 정보가 없습니다."); + } + User interestUserEntity = optionalInterestUser.get(); + + if (userEntity.getId() == interestUserEntity.getId()) { + return ServiceResult.fail("자기 자신은 추가할 수 없습니다."); + } + + if (userInterestRepository.countByUserAndInterestUser(userEntity, interestUserEntity) > 0) { + return ServiceResult.fail("이미 관심사용자 목록에 추가된 사용자 입니다."); + } + + UserInterest userInterest = UserInterest.builder() + .user(userEntity) + .interestUser(interestUserEntity) + .regDate(LocalDateTime.now()) + .build(); + userInterestRepository.save(userInterest); + + return ServiceResult.success(); + } } diff --git a/rest-controller-practice/src/main/java/com/example/restcontroller/util/JWTUtils.java b/rest-controller-practice/src/main/java/com/example/restcontroller/util/JWTUtils.java index d992ba56..df8f1554 100644 --- a/rest-controller-practice/src/main/java/com/example/restcontroller/util/JWTUtils.java +++ b/rest-controller-practice/src/main/java/com/example/restcontroller/util/JWTUtils.java @@ -2,7 +2,9 @@ package com.example.restcontroller.util; import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.exceptions.JWTVerificationException; import com.auth0.jwt.exceptions.SignatureVerificationException; +import com.example.restcontroller.common.model.ResponseResult; import com.example.restcontroller.user.exception.PasswordNotMatchException; import lombok.experimental.UtilityClass; import org.springframework.security.crypto.bcrypt.BCrypt;