Skip to content

Commit

Permalink
Merge pull request #61 from IxxP-Girls/comment
Browse files Browse the repository at this point in the history
#21 #22 Feat: 댓글 수정, 삭제 기능 구현
  • Loading branch information
diddnwjd authored Feb 20, 2024
2 parents 504554b + 1937307 commit da37d64
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/ixxp/culpop/controller/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,27 @@ public ResponseEntity<StatusResponse> createComment(@AuthenticationPrincipal Use
StatusResponse statusResponse = new StatusResponse(HttpStatus.CREATED.value(), "comment 등록 완료");
return ResponseEntity.status(HttpStatus.CREATED).body(statusResponse);
}

// 댓글 조회

// 댓글 수정
@PatchMapping("/{postId}/comments/{commentId}")
public ResponseEntity<StatusResponse> updateComment(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable int postId,
@PathVariable int commentId,
@RequestBody CommentRequest commentRequest) {
commentService.updateComment(userDetails.getUser(), postId, commentId, commentRequest);
StatusResponse statusResponse = new StatusResponse(HttpStatus.OK.value(), "comment 수정 완료");
return ResponseEntity.ok(statusResponse);
}

// 댓글 삭제
@DeleteMapping("/{postId}/comments/{commentId}")
public ResponseEntity<StatusResponse> deleteComment(@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable int postId,
@PathVariable int commentId) {
commentService.deleteComment(userDetails.getUser(), postId, commentId);
StatusResponse statusResponse = new StatusResponse(HttpStatus.OK.value(), "comment 삭제 완료");
return ResponseEntity.ok(statusResponse);
}
}
1 change: 0 additions & 1 deletion src/main/java/com/ixxp/culpop/dto/post/CommentRequest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ixxp.culpop.dto.post;

import lombok.Getter;
import org.springframework.lang.Nullable;

@Getter
public class CommentRequest {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/ixxp/culpop/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public Comment(User user, Post post, String content, boolean secret, int parentI
this.secret = secret;
this.parentId = parentId;
}

public void updateComment(User user, Post post, String content, boolean secret, int parentId) {
this.user = user;
this.post = post;
this.content = content;
this.secret = secret;
this.parentId = parentId;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/ixxp/culpop/mapper/CommentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
@Mapper
public interface CommentMapper {
void insertComment(Comment comment);
Comment selectCommentDetail(int commentId);
void updateComment(Comment comment);
void deleteComment(int commentId);
}
48 changes: 48 additions & 0 deletions src/main/java/com/ixxp/culpop/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,52 @@ public void createComment(User user, int postId, CommentRequest commentRequest)
Comment comment = new Comment(user, post, commentRequest.getContent(), commentRequest.isSecret(), commentRequest.getParentId());
commentMapper.insertComment(comment);
}

// 댓글 조회

// 댓글 수정
@Transactional
public void updateComment(User user, int postId, int commentId, CommentRequest commentRequest) {
Post post = postMapper.selectPostDetail(postId);
if (post == null) {
throw new IllegalArgumentException("post가 존재하지 않습니다.");
}

Comment comment = commentMapper.selectCommentDetail(commentId);
if (comment == null) {
throw new IllegalArgumentException("comment가 존재하지 않습니다.");
}

if (comment.getUser().getId() != user.getId()) {
throw new IllegalArgumentException("작성자만 수정 가능합니다.");
}

comment.updateComment(user, post, commentRequest.getContent(), commentRequest.isSecret(), commentRequest.getParentId());
commentMapper.updateComment(comment);
}


// 댓글 삭제
@Transactional
public void deleteComment(User user, int postId, int commentId) {
Post post = postMapper.selectPostDetail(postId);
if (post == null) {
throw new IllegalArgumentException("post가 존재하지 않습니다.");
}

Comment comment = commentMapper.selectCommentDetail(commentId);
if (comment == null) {
throw new IllegalArgumentException("comment가 존재하지 않습니다.");
}

if (comment.getUser().getId() != user.getId()) {
throw new IllegalArgumentException("작성자만 수정 가능합니다.");
}

if (comment.getPost().getId() != postId) {
throw new IllegalArgumentException("post를 잘못 입력하였습니다.");
}

commentMapper.deleteComment(commentId);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring.profiles.include=dev

# aws rds
spring.datasource.url=jdbc:mysql://culpop.c7w6eoa627sa.ap-northeast-2.rds.amazonaws.com:3306/culpop_backend?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.datasource.url=jdbc:mysql://culpop.c7w6eoa627sa.ap-northeast-2.rds.amazonaws.com:3306/culpop_backend?useSSL=false&characterEncoding=UTF-8&serverTimezone=UTC
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# mybatis
mybatis.type-aliases-package=com.ixxp.culpop.entity
Expand Down
31 changes: 31 additions & 0 deletions src/main/resources/mapper/comment-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,38 @@
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ixxp.culpop.mapper.CommentMapper">
<resultMap id="commentResult" type="comment">
<id property="id" column="id"/>
<result property="content" column="content"/>
<result property="secret" column="secret"/>
<result property="parentId" column="parentId"/>
<result property="createdAt" column="createdAt"/>
<result property="modifiedAt" column="modifiedAt"/>
<collection property="user" javaType="User">
<id property="id" column="userId" />
<result property="username" column="username" />
</collection>
<collection property="post" javaType="Post">
<id property="id" column="postId" />
</collection>
</resultMap>
<insert id="insertComment" useGeneratedKeys="true" keyProperty="id">
INSERT INTO Comment (userId, postId, content, secret, parentId) VALUES (#{user.id}, #{post.id}, #{content}, #{secret}, #{parentId})
</insert>
<select id="selectCommentDetail" parameterType="int" resultMap="commentResult">
SELECT c.id, c.content, c.secret, c.parentId, c.createdAt, u.id AS userId, u.username, p.id AS postId
FROM Comment c
LEFT JOIN User u ON c.userId = u.id
LEFT JOIN Post p on c.postId = p.id
WHERE c.id=#{commentId}
GROUP BY c.id, c.content, c.secret, c.parentId, c.createdAt, u.id, u.username, p.id
ORDER BY c.createdAt;
</select>
<update id="updateComment" parameterType="Comment">
UPDATE Comment SET content=#{content}, secret=#{secret}, parentId=#{parentId}
WHERE id=#{id}
</update>
<delete id="deleteComment" parameterType="Comment">
DELETE FROM Comment WHERE id=#{id}
</delete>
</mapper>

0 comments on commit da37d64

Please sign in to comment.