
@Service
@RequiredArgsConstructor
public class CommentService {
private final CommentRepository commentRepository;
private final ScheduleRepository scheduleRepository;
public CommentResponseDto createComment(Long scheduleId, CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails) {
Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(
NotFoundScheduleException::new
);
Comment comment = new Comment(commentRequestDto.getComment(), schedule, userDetails.getUser());
commentRepository.save(comment);
return new CommentResponseDto(comment);
}
@Transactional
public CommentResponseDto updateComment(Long commentId, CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails) {
Comment comment = commentRepository.findById(commentId).orElseThrow(
NotFoundCommentException::new
);
if (!(comment.getUser().getUserId() == userDetails.getUser().getUserId())) {
throw new UnauthorizedOperationException();
}
comment.update(commentRequestDto.getComment());
return new CommentResponseDto(comment);
}
public void deleteComment(Long commentId, UserDetailsImpl userDetails) {
Comment comment = commentRepository.findById(commentId).orElseThrow(
NotFoundCommentException::new
);
if (!(comment.getUser().getUserId() == userDetails.getUser().getUserId())) {
throw new UnauthorizedOperationException();
}
commentRepository.delete(comment);
}
}
public interface CommentService {
/**
* 댓글 생성
*
* @param scheduleId 댓글을 생성할 게시글 ID
* @param commentRequestDto 댓글 생성 내용
* @param userDetails 댓글 생성자 구분
* @return 게시글 생성 결과
*/
CommentResponseDto createComment(Long scheduleId, CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails);
/**
* 댓글 수성
*
* @param commentId 수정할 댓글 ID
* @param commentRequestDto 수정할 댓글 내용
* @param userDetails 댓글 수정자 구분
* @return 게시글 수정 결과
*/
CommentResponseDto updateComment(Long commentId, CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails);
/**
* 댓글 삭제
*
* @param commentId 삭제할 댓글 ID
* @param userDetails 댓글 삭제자 구분
*/
void deleteComment(Long commentId, UserDetailsImpl userDetails);
}
---
@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements CommentService {
private final CommentRepository commentRepository;
private final ScheduleRepository scheduleRepository;
@Override
public CommentResponseDto createComment(
Long scheduleId,
CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails
) {
Schedule schedule = findScheduleById(scheduleId);
Comment comment = new Comment(commentRequestDto.getComment(), schedule,
userDetails.getUser());
commentRepository.save(comment);
return new CommentResponseDto(comment);
}
@Override
@Transactional
public CommentResponseDto updateComment(
Long commentId,
CommentRequestDto commentRequestDto,
UserDetailsImpl userDetails
) {
Comment comment = findCommentById(commentId, userDetails);
comment.update(commentRequestDto.getComment());
return new CommentResponseDto(comment);
}
@Override
public void deleteComment(Long commentId, UserDetailsImpl userDetails) {
Comment comment = findCommentById(commentId, userDetails);
commentRepository.delete(comment);
}
private Schedule findScheduleById(Long scheduleId) {
return scheduleRepository.findById(scheduleId)
.orElseThrow(NotFoundScheduleException::new);
}
private Comment findCommentById(Long commentId, UserDetailsImpl userDetails) {
Comment comment = commentRepository.findById(commentId).orElseThrow(
NotFoundCommentException::new
);
if ((Objects.equals(comment.getUser().getUserId(), userDetails.getUser().getUserId()))) {
return comment;
}
throw new UnauthorizedOperationException();
}
}
장점
단점
상속의 문제
관계 문제
@JoinColumn, @MappedBy)탐색 문제
@FetchType, fetchJoin())@embedded)@Id,@GeneratedValue )@Cacheable 적용 후 설정 추가sharedCache.mode 설정generationType.IDENTITY 로 설정 되어있는 경우 생성쿼리는 쓰기지연이 발생하지 못한다.