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
Expand Up @@ -9,27 +9,27 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto;
import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto;
import zoo.insightnote.domain.comment.dto.res.CommentIdResDto;
import zoo.insightnote.domain.comment.dto.res.CommentListResDto;
import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest;
import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest;
import zoo.insightnote.domain.comment.dto.request.CommentIdResponse;
import zoo.insightnote.domain.comment.dto.request.CommentListResponse;


@Tag(name = "Comment API", description = "댓글 관련 API")
public interface CommentController {

@Operation(summary = "댓글 작성", description = "사용자가 특정 인사이트에 댓글을 작성합니다.")
ResponseEntity<CommentIdResDto> writeComment(
ResponseEntity<CommentIdResponse> writeComment(
@Parameter(description = "인사이트 ID") @PathVariable Long insightId,
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails,
@RequestBody CommentCreateReqDto request);
@RequestBody CommentCreateRequest request);

@Operation(summary = "댓글 수정", description = "작성자가 본인의 댓글을 수정합니다.")
ResponseEntity<CommentIdResDto> updateComment(
ResponseEntity<CommentIdResponse> updateComment(
@Parameter(description = "인사이트 ID") @PathVariable Long insightId,
@Parameter(description = "댓글 ID") @PathVariable Long commentId,
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails,
@RequestBody CommentUpdateReqDto request);
@RequestBody CommentUpdateRequest request);

@Operation(summary = "댓글 삭제", description = "작성자가 본인의 댓글을 삭제합니다.")
ResponseEntity<Void> deleteComment(
Expand All @@ -38,6 +38,6 @@ ResponseEntity<Void> deleteComment(
@Parameter(hidden = true) @AuthenticationPrincipal UserDetails userDetails);

@Operation(summary = "댓글 목록 조회", description = "특정 인사이트의 댓글 목록을 조회합니다.")
ResponseEntity<List<CommentListResDto>> getListComments(
ResponseEntity<List<CommentListResponse>> getListComments(
@Parameter(description = "인사이트 ID") @PathVariable Long insightId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package zoo.insightnote.domain.comment.controller;

import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -15,11 +13,11 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zoo.insightnote.domain.comment.dto.res.CommentListResDto;
import zoo.insightnote.domain.comment.dto.request.CommentListResponse;

import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto;
import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto;
import zoo.insightnote.domain.comment.dto.res.CommentIdResDto;
import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest;
import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest;
import zoo.insightnote.domain.comment.dto.request.CommentIdResponse;

import zoo.insightnote.domain.comment.service.CommentService;

Expand All @@ -32,28 +30,24 @@ public class CommentControllerImpl implements CommentController {

@Override
@PostMapping("/{insightId}/comments")
public ResponseEntity<CommentIdResDto> writeComment(
public ResponseEntity<CommentIdResponse> writeComment(
@PathVariable Long insightId,
@AuthenticationPrincipal UserDetails userDetails,
@RequestBody CommentCreateReqDto request)
@RequestBody CommentCreateRequest request)
{
CommentIdResDto commentId = commentService.createComment(insightId, userDetails.getUsername(), request);
CommentIdResponse commentId = commentService.createComment(insightId, userDetails.getUsername(), request);
return ResponseEntity.ok().body(commentId);
}

// @Override
// @GetMapping("/{insightId}/comments")
// public ResponseEntity<List<CommentResponse>> listComments(@PathVariable Long insightId) {
// return ResponseEntity.ok().body(commentService.findCommentsByInsightId(insightId));
// }

@Override
@PutMapping("/{insightId}/comments/{commentId}")
public ResponseEntity<CommentIdResDto> updateComment(@PathVariable Long insightId,
@PathVariable Long commentId,
@AuthenticationPrincipal UserDetails userDetails,
@RequestBody CommentUpdateReqDto request) {
CommentIdResDto response = commentService.updateComment(insightId, userDetails.getUsername(), commentId, request);
public ResponseEntity<CommentIdResponse> updateComment(
@PathVariable Long insightId,
@PathVariable Long commentId,
@AuthenticationPrincipal UserDetails userDetails,
@RequestBody CommentUpdateRequest request
) {
CommentIdResponse response = commentService.updateComment(insightId, userDetails.getUsername(), commentId, request);
return ResponseEntity.ok().body(response);
}

Expand All @@ -68,20 +62,10 @@ public ResponseEntity<Void> deleteComment(
return ResponseEntity.noContent().build();
}

// 임시 로직
private UserDetails validateUser(UserDetails userDetails) {
if (userDetails != null) {
return userDetails;
}
return new User("001", "password", Collections.emptyList());
}


// 댓글 리스트 출력
@Override
@GetMapping("/comments/{insightId}")
public ResponseEntity<List<CommentListResDto>> getListComments(@PathVariable Long insightId) {
List<CommentListResDto> comments = commentService.getCommentsByInsight(insightId);
public ResponseEntity<List<CommentListResponse>> getListComments(@PathVariable Long insightId) {
List<CommentListResponse> comments = commentService.getCommentsByInsight(insightId);
return ResponseEntity.ok(comments);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zoo.insightnote.domain.comment.dto.request;
import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.AccessLevel;

public record CommentDefaultResponse (
Long commentId,
String content,
LocalDateTime createAt,
String author
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package zoo.insightnote.domain.comment.dto.request;


public record CommentIdResponse(Long id) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zoo.insightnote.domain.comment.dto.res;
package zoo.insightnote.domain.comment.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand All @@ -10,7 +10,7 @@

@Getter
@AllArgsConstructor
public class CommentListResDto {
public class CommentListResponse {
private Long id;
private String name;
private String createdAt;
Expand All @@ -19,7 +19,7 @@ public class CommentListResDto {
private String content;
private boolean isSpeaker;

public CommentListResDto(Comment comment) {
public CommentListResponse(Comment comment) {
this.id = comment.getId();
this.name = comment.getUser().getName(); // User 엔티티에 name 필드가 있다고 가정
this.createdAt = formatDate(comment.getCreateAt());
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package zoo.insightnote.domain.comment.dto.req;
package zoo.insightnote.domain.comment.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;

public record CommentCreateReqDto(
public record CommentCreateRequest(
@Schema(description = "댓글 내용", example = "작성하신 노트 잘보았습니다!")
@NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.")
String content
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package zoo.insightnote.domain.comment.dto.req;
package zoo.insightnote.domain.comment.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;

public record CommentUpdateReqDto(
public record CommentUpdateRequest(
@Schema(description = "댓글 내용", example = "작성하신 노트에 잘못된 부분이 있는 것 같습니다!")
@NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.")
String content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ public class Comment extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

//TODO : user Entity 개발 완료시 nullable = false로 수정
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = true)
@JoinColumn(name = "user_id", nullable = false)
private User user;

//TODO : insight Entity 개발 완료시 nullable = false로 수정
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "insight_id", nullable = true)
@JoinColumn(name = "insight_id", nullable = false)
private Insight insight;

private String content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package zoo.insightnote.domain.comment.mapper;

import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto;
import zoo.insightnote.domain.comment.dto.res.CommentDefaultResDto;
import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest;
import zoo.insightnote.domain.comment.dto.request.CommentDefaultResponse;

import zoo.insightnote.domain.comment.entity.Comment;
import zoo.insightnote.domain.insight.entity.Insight;
import zoo.insightnote.domain.user.entity.User;

public class CommentMapper {

public static Comment toEntity(Insight insight, User user, CommentCreateReqDto request) {
public static Comment toEntity(Insight insight, User user, CommentCreateRequest request) {
return Comment.builder()
.insight(insight)
.user(user)
.content(request.content())
.build();
}

public static CommentDefaultResDto toResponse(Comment comment) {
return new CommentDefaultResDto (
public static CommentDefaultResponse toResponse(Comment comment) {
return new CommentDefaultResponse(
comment.getId(),
comment.getContent(),
comment.getCreateAt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zoo.insightnote.domain.comment.dto.res.CommentIdResDto;
import zoo.insightnote.domain.comment.dto.res.CommentListResDto;
import zoo.insightnote.domain.comment.dto.request.CommentIdResponse;
import zoo.insightnote.domain.comment.dto.request.CommentListResponse;

import zoo.insightnote.domain.comment.dto.req.CommentCreateReqDto;
import zoo.insightnote.domain.comment.dto.req.CommentUpdateReqDto;
import zoo.insightnote.domain.comment.dto.res.CommentDefaultResDto;
import zoo.insightnote.domain.comment.dto.response.CommentCreateRequest;
import zoo.insightnote.domain.comment.dto.response.CommentUpdateRequest;

import zoo.insightnote.domain.comment.entity.Comment;
import zoo.insightnote.domain.comment.mapper.CommentMapper;
import zoo.insightnote.domain.comment.repository.CommentRepository;
import zoo.insightnote.domain.insight.entity.Insight;
import zoo.insightnote.domain.insight.repository.InsightRepository;
import zoo.insightnote.domain.user.entity.User;
import zoo.insightnote.domain.user.repository.UserRepository;
import zoo.insightnote.domain.user.service.UserService;
import zoo.insightnote.global.exception.CustomException;
import zoo.insightnote.global.exception.ErrorCode;
Expand All @@ -32,34 +30,22 @@ public class CommentService {
private final UserService userService;
private final InsightRepository insightRepository;

public CommentIdResDto createComment(Long insightId, String userName, CommentCreateReqDto request) {
public CommentIdResponse createComment(Long insightId, String userName, CommentCreateRequest request) {

Insight insight = insightRepository.findById(insightId)
.orElseThrow(() -> new CustomException(null, "인사이트 노트를 찾을 수 없음"));
.orElseThrow(() -> new CustomException(ErrorCode.INSIGHT_NOT_FOUND));

User user = userService.findByUsername(userName);

Comment comment = CommentMapper.toEntity(insight, user, request);

commentRepository.save(comment);

return new CommentIdResDto(comment.getId());
return new CommentIdResponse(comment.getId());
}

// public List<CommentResponse> findCommentsByInsightId(Long insightId) {
//
// List<Comment> comments = commentRepository.findAllByInsightId(insightId);
//
// List<CommentResponse> responses = new ArrayList<>();
// for (Comment comment : comments) {
// responses.add(CommentMapper.toResponse(comment));
// }
//
// return responses;
// }

@Transactional
public CommentIdResDto updateComment(Long insightId, String userName, Long commentId, CommentUpdateReqDto request) {
public CommentIdResponse updateComment(Long insightId, String userName, Long commentId, CommentUpdateRequest request) {

Comment comment = findCommentById(commentId);
User user = userService.findByUsername(userName);
Expand All @@ -68,7 +54,7 @@ public CommentIdResDto updateComment(Long insightId, String userName, Long comme

comment.update(request.content());

return new CommentIdResDto(comment.getId());
return new CommentIdResponse(comment.getId());
}

public void deleteComment(Long insightId, String userName, Long commentId) {
Expand All @@ -82,14 +68,11 @@ public void deleteComment(Long insightId, String userName, Long commentId) {

}

public List<CommentListResDto> getCommentsByInsight(Long insightId) {

// 예외처리 로직 필요함

public List<CommentListResponse> getCommentsByInsight(Long insightId) {
List<Comment> comments = commentRepository.findByInsightIdWithUser(insightId);

return comments.stream()
.map(CommentListResDto::new)
.map(CommentListResponse::new)
.collect(Collectors.toList());
}

Expand Down