Skip to content
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ dependencies {
// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

//aop
implementation 'org.springframework.boot:spring-boot-starter-aop'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.blog.domain.comment.controller.dto;
package com.blog.domain.comment.controller;


import com.blog.domain.comment.controller.dto.request.CommentRequest;
import com.blog.domain.comment.controller.dto.request.CommentUpdatedRequest;
import com.blog.domain.comment.controller.request.CommentRequest;
import com.blog.domain.comment.controller.request.CommentUpdatedRequest;
import com.blog.domain.comment.service.CommentService;
import com.blog.domain.post.service.PostService;
import com.blog.global.response.ApiResponse;
import com.blog.global.security.aop.GetUserId;
import jakarta.validation.Valid;
Expand All @@ -23,22 +22,22 @@ public CommentController(CommentService commentService) {

// 댓글 등록
@PostMapping
public ApiResponse<String> registerComment(@GetUserId long userId, @Valid @RequestBody CommentRequest request) {
public ApiResponse<String> registerComment(@GetUserId Long userId, @Valid @RequestBody CommentRequest request) {
commentService.registerComment(request, userId);
return ApiResponse.ok("정상적으로 등록되었습니다.");
}

//댓글 수정
@PutMapping("/{commentId}")
public ApiResponse<String> updateComment(@GetUserId long userId, @PathVariable long commentId, @Valid @RequestBody CommentUpdatedRequest request) {
public ApiResponse<String> updateComment(@GetUserId Long userId, @PathVariable long commentId, @Valid @RequestBody CommentUpdatedRequest request) {
commentService.updateComment(userId, commentId, request);
return ApiResponse.ok("정상적으로 수정되었습니다.");

}

//댓글 삭제
@DeleteMapping("/{commentId}")
public ApiResponse<String> deleteComment(@GetUserId long userId, @PathVariable long commentId) {
public ApiResponse<String> deleteComment(@GetUserId Long userId, @PathVariable long commentId) {
commentService.deleteComment(userId, commentId);
return ApiResponse.ok("정상적으로 삭제되었습니다.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blog.domain.comment.controller.dto.request;
package com.blog.domain.comment.controller.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.blog.domain.comment.controller.dto.request;
package com.blog.domain.comment.controller.request;

import jakarta.validation.constraints.NotBlank;

public record CommentUpdatedRequest(
@NotBlank(message = "댓글 내용은 비어 있을 수 없습니다.")
String content)
{}

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blog.domain.comment.controller.dto.response;
package com.blog.domain.comment.controller.response;

import com.blog.domain.comment.domain.Comment;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/blog/domain/comment/domain/Comment.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.blog.domain.comment.domain;

import com.blog.domain.comment.controller.dto.request.CommentRequest;
import com.blog.domain.comment.controller.request.CommentRequest;
import com.blog.global.common.BaseDomain;

import java.time.LocalDateTime;
Expand Down
32 changes: 12 additions & 20 deletions src/main/java/com/blog/domain/comment/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.blog.domain.comment.service;

import com.blog.domain.comment.controller.dto.request.CommentRequest;
import com.blog.domain.comment.controller.dto.request.CommentUpdatedRequest;
import com.blog.domain.comment.controller.dto.response.CommentResponse;
import com.blog.domain.comment.controller.request.CommentRequest;
import com.blog.domain.comment.controller.request.CommentUpdatedRequest;
import com.blog.domain.comment.controller.response.CommentResponse;
import com.blog.domain.comment.domain.Comment;
import com.blog.domain.comment.repository.CommentRepository;
import com.blog.domain.post.domain.Post;
import com.blog.domain.post.repository.PostRepository;
import com.blog.domain.user.domain.User;
import com.blog.domain.user.repository.UserRepository;
Expand Down Expand Up @@ -36,39 +35,31 @@ public CommentService(CommentRepository commentRepository, UserRepository userRe
// 댓글 등록
@Transactional
public void registerComment(@Valid CommentRequest request, Long userId) {
Post post = postRepository.findById(request.postId())
postRepository.findById(request.postId())
.orElseThrow(() -> new CustomException(POST_NOT_FOUND));

// 댓글 생성 및 저장
Comment comment = Comment.of(userId, request);
commentRepository.save(comment);

postRepository.incrementCommentCount(request.postId());
System.out.println(post.getCommentCount());
}

// 댓글 수정
@Transactional
public void updateComment(Long userId, Long commentId, CommentUpdatedRequest request) {
Comment comment = getCommentsByCommentId(commentId);
validateCommentOwner(comment, userId);
Comment comment = getAuthorizedComment(userId, commentId);
comment.updateContent(request.content());
commentRepository.update(comment);
}

// 댓글 삭제
@Transactional
public void deleteComment(Long userId, Long commentId) {
Comment comment = getCommentsByCommentId(commentId);
validateCommentOwner(comment, userId);

int count = commentRepository.deleteByCommentId(commentId); // 이름 수정
Long postId = comment.getPostId(); // postId 가져오기

postRepository.decreaseCommentCount(postId, count);
Comment comment = getAuthorizedComment(userId, commentId);
int count = commentRepository.deleteByCommentId(commentId);
postRepository.decreaseCommentCount(comment.getPostId(), count);
}


// 모든 댓글 삭제
@Transactional
public void deleteAllCommentsByPostId(Long postId) {
Expand All @@ -92,12 +83,13 @@ public List<CommentResponse> getAllCommentsByPostId(Long postId) {
.toList();
}

private void validateCommentOwner(Comment comment, Long userId) {
// 댓글 조회 + 권한 검증 메서드 추가
private Comment getAuthorizedComment(Long userId, Long commentId) {
Comment comment = getCommentsByCommentId(commentId);
if (!comment.getUserId().equals(userId)) {
throw new CustomException(ACCESS_DENY);
}
return comment;
}



}
16 changes: 4 additions & 12 deletions src/main/java/com/blog/domain/post/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.blog.domain.post.controller;

import com.blog.domain.post.controller.dto.request.PostRequest;
import com.blog.domain.post.controller.dto.response.PostListResponse;
import com.blog.domain.post.controller.dto.response.PostResponse;
import com.blog.domain.post.controller.request.PostRequest;
import com.blog.domain.post.controller.response.PostListResponse;
import com.blog.domain.post.controller.response.PostResponse;
import com.blog.domain.post.service.PostService;
import com.blog.global.exception.CustomException;
import com.blog.global.exception.ErrorCode;
Expand All @@ -18,7 +18,6 @@
public class PostController {

private final PostService postService;
// private final TokenService tokenService;

public PostController(PostService postService) {
this.postService = postService;
Expand All @@ -27,13 +26,7 @@ public PostController(PostService postService) {
// 글 등록
@PostMapping
public ApiResponse<String> createPost(@GetUserId Long userId, @Valid @RequestBody PostRequest postRequest) {

// 로그인이 되어 있지 않으면 로그인 페이지 URL 반환
if (userId == null) {
return ApiResponse.fail(new CustomException(ErrorCode.USER_NOT_FOUND));
}

postService.savePost(userId, postRequest);
postService.createPost(userId, postRequest);
return ApiResponse.ok("정상적으로 등록되었습니다.");
}

Expand All @@ -42,7 +35,6 @@ public ApiResponse<String> createPost(@GetUserId Long userId, @Valid @RequestBod
public ApiResponse<List<PostListResponse>> getPostList(@GetUserId Long userId) {
List<PostListResponse> posts = postService.getPostList(userId);
return ApiResponse.ok(posts);

}

// 글 상세 조회
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blog.domain.post.controller.dto.request;
package com.blog.domain.post.controller.request;

import com.blog.domain.post.domain.ContentType;
import com.blog.domain.post.domain.PostContent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blog.domain.post.controller.dto.request;
package com.blog.domain.post.controller.request;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.blog.domain.post.controller.dto.response;
package com.blog.domain.post.controller.response;

import com.blog.domain.user.domain.User;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.blog.domain.post.controller.dto.response;
package com.blog.domain.post.controller.response;

import com.blog.domain.post.controller.dto.request.PostContentDto;
import com.blog.domain.post.domain.ContentType;
import com.blog.domain.post.controller.request.PostContentDto;
import com.blog.domain.post.domain.Post;
import com.blog.domain.post.domain.PostContent;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

public record PostListResponse(
long postId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.blog.domain.post.controller.dto.response;
package com.blog.domain.post.controller.response;

import com.blog.domain.comment.controller.dto.response.CommentResponse;
import com.blog.domain.post.controller.dto.request.PostContentDto;
import com.blog.domain.comment.controller.response.CommentResponse;
import com.blog.domain.post.controller.request.PostContentDto;
import com.blog.domain.post.domain.Post;
import com.blog.domain.post.domain.PostContent;
import com.blog.domain.user.domain.User;

import java.time.LocalDateTime;
import java.util.List;

import static com.blog.domain.post.controller.dto.response.AuthorSummary.toAuthorSummary;
import static com.blog.domain.post.controller.response.AuthorSummary.toAuthorSummary;

public record PostResponse(
long postId,
Expand Down
14 changes: 3 additions & 11 deletions src/main/java/com/blog/domain/post/domain/Post.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.blog.domain.post.domain;

import com.blog.domain.post.controller.dto.request.PostRequest;
import com.blog.domain.post.controller.request.PostRequest;
import com.blog.global.common.BaseDomain;
import jakarta.validation.constraints.NotBlank;
import java.time.LocalDateTime;

public class Post extends BaseDomain {

private Long id;
private Long userId;
private long userId;
private String title;
private int commentCount = 0;

Expand Down Expand Up @@ -36,12 +36,10 @@ public Post(Long id, Long userId, String title,int commentCount, LocalDateTime c
this.commentCount = commentCount;
}


public static Post of(Long userId, PostRequest postRequest) {
return new Post(userId, postRequest.title());
}


// Getter
public Long getId() {
return id;
Expand All @@ -61,16 +59,10 @@ public int getCommentCount() {

// 도메인 메서드
public void updateTitle(@NotBlank String title) {
if (title == null || title.isBlank()) {
throw new IllegalArgumentException("제목은 비어있을 수 없습니다.");
}
this.title = title;
}

public void updateUpdatedAt(LocalDateTime now) {
if (now == null) {
throw new IllegalArgumentException("업데이트 시간은 비어있을 수 없습니다.");
}
public void updateUpdatedAt(@NotBlank LocalDateTime now) {
this.updatedAt = now;
}

Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/blog/domain/post/domain/PostContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class PostContent {

private Long id;
private Long postId;
private long postId;
private ContentType type; // TEXT, IMAGE
private String content;
private int sequence; // 순서
Expand All @@ -22,6 +22,15 @@ public PostContent(Long postId, ContentType type, String content, int sequence)
this.sequence = sequence;
}

public static PostContent text(Long postId, String text, int sequence) {
return new PostContent(postId,ContentType.TEXT,text,sequence);
}

public static PostContent image(Long postId, String imageUrl, int sequence) {
return new PostContent(postId, ContentType.IMAGE, imageUrl, sequence);
}

// getter
public int getSequence() {
return sequence;
}
Expand All @@ -42,12 +51,5 @@ public Long getId() {
return id;
}

public static PostContent text(Long postId, String text, int sequence) {
return new PostContent(postId,ContentType.TEXT,text,sequence);
}

public static PostContent image(Long postId, String imageUrl, int sequence) {
return new PostContent(postId, ContentType.IMAGE, imageUrl, sequence);
}
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.blog.domain.post.repository;

import com.blog.domain.post.domain.ContentType;
import com.blog.domain.post.domain.Post;
import com.blog.domain.post.domain.PostContent;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
Expand Down Expand Up @@ -56,6 +55,7 @@ public void update(List<PostContent> postContents) {
}
}

// post삭제
public void deleteByPostId(Long postId) {
String sql = "DELETE FROM post_content WHERE post_id = ?";
jdbcTemplate.update(sql, postId);
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/blog/domain/post/service/PostContentMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.blog.domain.post.service;

import com.blog.domain.post.controller.request.PostContentDto;
import com.blog.domain.post.domain.PostContent;
import com.blog.global.exception.CustomException;
import com.blog.global.exception.ErrorCode;

import java.util.List;
import java.util.stream.Collectors;

public class PostContentMapper {

public static PostContent mapFromDto(Long postId, PostContentDto postContentDto) {
return switch (postContentDto.type()) {
case TEXT -> PostContent.text(postId, postContentDto.data(), 0);
case IMAGE -> PostContent.image(postId, postContentDto.data(), 0);
default -> throw new CustomException(ErrorCode.POST_TYPE_NOT_FOUND);
};
}

public static List<PostContent> mapFromDtos(Long postId, List<PostContentDto> postContentDtos) {
return postContentDtos.stream()
.map(dto -> mapFromDto(postId, dto))
.collect(Collectors.toList());
}
}
Loading