rest controller practice
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -72,6 +72,14 @@ public class User {
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<BoardBookmark> boardBookmarkList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<UserInterest> userList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "interestUser")
|
||||
List<UserInterest> userInterestList = new ArrayList<>();
|
||||
|
||||
@PrePersist
|
||||
public void prePersist() {
|
||||
status = status == null ? UserStatus.USING : status;
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
@@ -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<UserInterest, Long> {
|
||||
|
||||
Long countByUserAndInterestUser(User user, User interestUser);
|
||||
}
|
||||
@@ -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<UserLogCount> getUserLogCount();
|
||||
|
||||
List<UserLogCount> getUserLikeBest();
|
||||
|
||||
ServiceResult addInterestUser(Long id, String email);
|
||||
}
|
||||
|
||||
@@ -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<UserLogCount> getUserLikeBest() {
|
||||
return userCustomRepository.findUserLikeBest();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult addInterestUser(Long id, String email) {
|
||||
Optional<User> optionalUser = userRepository.findByEmail(email);
|
||||
if (!optionalUser.isPresent()) {
|
||||
return ServiceResult.fail("회원 정보가 없습니다.");
|
||||
}
|
||||
User userEntity = optionalUser.get();
|
||||
|
||||
Optional<User> 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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user