Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.log4u.domain.comment.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto;
import com.example.log4u.domain.comment.dto.response.CommentCreateResponseDto;
import com.example.log4u.domain.comment.service.CommentService;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

@Tag(name = "댓글 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/comments")
public class CommentController {

private final CommentService commentService;

@PostMapping
public ResponseEntity<CommentCreateResponseDto> addComment(@RequestBody @Valid CommentCreateRequestDto requestDto) {
Long userId = 1L; //임시 처리

CommentCreateResponseDto response = commentService.addComment(userId, requestDto);
return ResponseEntity.ok(response);
}

@DeleteMapping("/{commentId}")
public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
Long userId = 1L; //임시 처리

commentService.deleteComment(userId, commentId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.log4u.domain.comment.dto.request;

import com.example.log4u.domain.comment.entity.Comment;

import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public record CommentCreateRequestDto(

@NotNull
Long diaryId,

@NotBlank
@Size(max = 1000)
String content
) {
public Comment toEntity(Long userId) {
return Comment.builder()
.userId(userId)
.diaryId(diaryId)
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.log4u.domain.comment.dto.response;

import com.example.log4u.domain.comment.entity.Comment;

public record CommentCreateResponseDto(
Long commentId,
String content
) {
public static CommentCreateResponseDto of(Comment comment) {
return new CommentCreateResponseDto(
comment.getCommentId(),
comment.getContent());
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/example/log4u/domain/comment/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.log4u.domain.comment.entity;

import com.example.log4u.common.entity.BaseEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class Comment extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long commentId;

@Column(nullable = false)
private Long userId;

@Column(nullable = false)
private Long diaryId;

private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
@RequiredArgsConstructor
public enum CommentErrorCode implements ErrorCode {

NOT_FOUND_COMMENT(HttpStatus.NOT_FOUND, "댓글을 찾을 수 없습니다.");
NOT_FOUND_COMMENT(HttpStatus.NOT_FOUND, "댓글을 찾을 수 없습니다."),
UNAUTHORIZED_COMMENT_ACCESS(HttpStatus.FORBIDDEN, "댓글에 대한 삭제 권한이 없습니다.");
;

private final HttpStatus httpStatus;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.log4u.domain.comment.exception;

public class UnauthorizedAccessException extends CommentException {
public UnauthorizedAccessException() {
super(CommentErrorCode.UNAUTHORIZED_COMMENT_ACCESS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.log4u.domain.comment.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.log4u.domain.comment.entity.Comment;

public interface CommentRepository extends JpaRepository<Comment, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.log4u.domain.comment.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.log4u.domain.comment.dto.request.CommentCreateRequestDto;
import com.example.log4u.domain.comment.dto.response.CommentCreateResponseDto;
import com.example.log4u.domain.comment.entity.Comment;
import com.example.log4u.domain.comment.exception.NotFoundCommentException;
import com.example.log4u.domain.comment.exception.UnauthorizedAccessException;
import com.example.log4u.domain.comment.repository.CommentRepository;
import com.example.log4u.domain.diary.service.DiaryService;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CommentService {

private final CommentRepository commentRepository;
private final DiaryService diaryService;

@Transactional
public CommentCreateResponseDto addComment(Long userId, CommentCreateRequestDto requestDto) {
checkDiaryExists(requestDto);
Comment comment = requestDto.toEntity(userId);
commentRepository.save(comment);
return CommentCreateResponseDto.of(comment);
}

@Transactional
public void deleteComment(Long userId, Long commentId) {
Comment comment = getComment(commentId);
validateCommentOwner(userId, comment);
commentRepository.delete(comment);
}

private void checkDiaryExists(CommentCreateRequestDto requestDto) {
diaryService.checkDiaryExists(requestDto.diaryId());
}

private void validateCommentOwner(Long userId, Comment comment) {
if (!comment.getUserId().equals(userId)) {
throw new UnauthorizedAccessException();
}
}

private Comment getComment(Long commentId) {
return commentRepository.findById(commentId)
.orElseThrow(NotFoundCommentException::new);
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ public Long getLikeCount(Long diaryId) {
Diary diary = getDiary(diaryId);
return diary.getLikeCount();
}

public void checkDiaryExists(Long diaryId) {
if (!diaryRepository.existsById(diaryId)) {
throw new NotFoundDiaryException();
}
}

}
Loading
Loading