rest controller practice
This commit is contained in:
@@ -6,6 +6,7 @@ import com.auth0.jwt.exceptions.JWTVerificationException;
|
||||
import com.auth0.jwt.exceptions.SignatureVerificationException;
|
||||
import com.example.restcontroller.board.entity.Board;
|
||||
import com.example.restcontroller.board.entity.BoardComment;
|
||||
import com.example.restcontroller.board.model.ServiceResult;
|
||||
import com.example.restcontroller.board.service.BoardService;
|
||||
import com.example.restcontroller.common.model.ResponseResult;
|
||||
import com.example.restcontroller.notice.entity.Notice;
|
||||
@@ -21,6 +22,7 @@ import com.example.restcontroller.user.exception.PasswordNotMatchException;
|
||||
import com.example.restcontroller.user.exception.UserNotFoundException;
|
||||
import com.example.restcontroller.user.model.*;
|
||||
import com.example.restcontroller.user.repository.UserRepository;
|
||||
import com.example.restcontroller.user.service.UserPointService;
|
||||
import com.example.restcontroller.util.JWTUtils;
|
||||
import com.example.restcontroller.util.PasswordUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -48,6 +50,7 @@ public class ApiUserController {
|
||||
private final NoticeLikeRepository noticeLikeRepository;
|
||||
|
||||
private final BoardService boardService;
|
||||
private final UserPointService userPointService;
|
||||
|
||||
@PostMapping("/api/user")
|
||||
public ResponseEntity<?> chapter2_1(@RequestBody @Valid UserInput userInput, BindingResult bindingResult) {
|
||||
@@ -386,4 +389,18 @@ public class ApiUserController {
|
||||
|
||||
return ResponseResult.success(list);
|
||||
}
|
||||
|
||||
@PostMapping("/api/user/point")
|
||||
public ResponseEntity<?> chapter3_22(@RequestHeader("TOKEN") String token,
|
||||
@RequestBody UserPointInput userPointInput) {
|
||||
String email = "";
|
||||
try {
|
||||
email = JWTUtils.getIssuer(token);
|
||||
} catch (JWTVerificationException e) {
|
||||
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
|
||||
}
|
||||
ServiceResult result = userPointService.addPoint(email, userPointInput);
|
||||
|
||||
return ResponseResult.result(result);
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,9 @@ public class User {
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<UserInterest> userList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "user")
|
||||
List<UserPoint> userPointList = new ArrayList<>();
|
||||
|
||||
@JsonIgnore
|
||||
@OneToMany(mappedBy = "interestUser")
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.restcontroller.user.entity;
|
||||
|
||||
import com.example.restcontroller.user.model.UserPointType;
|
||||
import lombok.*;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
@Getter @Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
@Entity
|
||||
public class UserPoint {
|
||||
|
||||
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(foreignKey = @ForeignKey(name = "FK_USER_POINT_USER_ID"))
|
||||
private User user;
|
||||
|
||||
private int point;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private UserPointType userPointType;
|
||||
|
||||
private LocalDateTime regDate;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.restcontroller.user.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Builder
|
||||
public class UserPointInput {
|
||||
|
||||
private UserPointType userPointType;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.example.restcontroller.user.model;
|
||||
|
||||
public enum UserPointType {
|
||||
|
||||
NONE(0),
|
||||
USER_REGISTER(100),
|
||||
ADD_POST(200),
|
||||
ADD_COMMENT(150),
|
||||
ADD_LIKE(50);
|
||||
|
||||
int value;
|
||||
|
||||
UserPointType(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.restcontroller.user.repository;
|
||||
|
||||
import com.example.restcontroller.user.entity.UserPoint;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface UserPointRepository extends JpaRepository<UserPoint, Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.restcontroller.user.service;
|
||||
|
||||
import com.example.restcontroller.board.model.ServiceResult;
|
||||
import com.example.restcontroller.user.model.UserPointInput;
|
||||
import com.example.restcontroller.user.model.UserPointType;
|
||||
|
||||
public interface UserPointService {
|
||||
|
||||
ServiceResult addPoint(String email, UserPointInput userPointInput);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.example.restcontroller.user.service;
|
||||
|
||||
|
||||
import com.example.restcontroller.board.model.ServiceResult;
|
||||
import com.example.restcontroller.common.exception.BizException;
|
||||
import com.example.restcontroller.user.entity.User;
|
||||
import com.example.restcontroller.user.entity.UserInterest;
|
||||
import com.example.restcontroller.user.entity.UserPoint;
|
||||
import com.example.restcontroller.user.entity.UserStatus;
|
||||
import com.example.restcontroller.user.model.*;
|
||||
import com.example.restcontroller.user.repository.UserCustomRepository;
|
||||
import com.example.restcontroller.user.repository.UserInterestRepository;
|
||||
import com.example.restcontroller.user.repository.UserPointRepository;
|
||||
import com.example.restcontroller.user.repository.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
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
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class UserPointServiceImpl implements UserPointService {
|
||||
|
||||
private final UserPointRepository userPointRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public ServiceResult addPoint(String email, UserPointInput userPointInput) {
|
||||
User userEntity = userRepository.findByEmail(email)
|
||||
.orElseThrow(() -> new BizException("회원 정보가 존재하지 않습니다."));
|
||||
|
||||
userPointRepository.save(UserPoint.builder()
|
||||
.user(userEntity)
|
||||
.userPointType(userPointInput.getUserPointType())
|
||||
.point(userPointInput.getUserPointType().getValue())
|
||||
.build());
|
||||
return ServiceResult.success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user