-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 댓글 기능 추가
- Loading branch information
Showing
17 changed files
with
297 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/unit/daybook/domain/comment/controller/CommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.unit.daybook.domain.comment.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
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.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.unit.daybook.domain.comment.dto.request.CommentCreateRequest; | ||
import com.unit.daybook.domain.comment.dto.request.CommentModifyRequest; | ||
import com.unit.daybook.domain.comment.dto.response.CommentCreateResponse; | ||
import com.unit.daybook.domain.comment.dto.response.CommentModifyResponse; | ||
import com.unit.daybook.domain.comment.service.CommentService; | ||
import com.unit.daybook.domain.common.annotation.LoginUsers; | ||
import com.unit.daybook.global.config.security.CustomUserDetails; | ||
|
||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequestMapping("/comments") | ||
@RequiredArgsConstructor | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping | ||
public ResponseEntity<CommentCreateResponse> commentCreate( | ||
@Valid @RequestBody CommentCreateRequest request, | ||
@LoginUsers CustomUserDetails userDetails | ||
) { | ||
CommentCreateResponse comment = commentService.createComment(request, userDetails.getMemberId()); | ||
return ResponseEntity.status(HttpStatus.CREATED).body(comment); | ||
} | ||
|
||
@DeleteMapping("/{commentId}") | ||
public void commentDelete( | ||
@PathVariable Long commentId | ||
) { | ||
commentService.deleteComment(commentId); | ||
} | ||
|
||
@PutMapping("/{commentId}") | ||
public CommentModifyResponse commentModify( | ||
@Valid @RequestBody CommentModifyRequest request, | ||
@PathVariable Long commentId | ||
) { | ||
return commentService.updateComment(request, commentId); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/unit/daybook/domain/comment/dto/request/CommentCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.unit.daybook.domain.comment.dto.request; | ||
|
||
public record CommentCreateRequest ( | ||
String content, | ||
Long boardId | ||
) { | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/unit/daybook/domain/comment/dto/request/CommentModifyRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.unit.daybook.domain.comment.dto.request; | ||
|
||
public record CommentModifyRequest( | ||
String content | ||
) { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/unit/daybook/domain/comment/dto/response/CommentCreateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.unit.daybook.domain.comment.dto.response; | ||
|
||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
public record CommentCreateResponse( | ||
Long commentId, | ||
String content, | ||
Long boardId, | ||
Long memberId | ||
) { | ||
public static CommentCreateResponse from(Comment comment) { | ||
return new CommentCreateResponse( | ||
comment.getCommentId(), | ||
comment.getContent(), | ||
comment.getBoard().getBoardId(), | ||
comment.getMember().getId() | ||
); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/unit/daybook/domain/comment/dto/response/CommentModifyResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.unit.daybook.domain.comment.dto.response; | ||
|
||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
public record CommentModifyResponse( | ||
String content, | ||
Long commentId | ||
) { | ||
public static CommentModifyResponse from(Comment comment) { | ||
return new CommentModifyResponse(comment.getContent(), comment.getCommentId()); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/unit/daybook/domain/comment/dto/response/FindOneCommentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.unit.daybook.domain.comment.dto.response; | ||
|
||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
public record FindOneCommentResponse( | ||
Long commentId, | ||
String content | ||
) { | ||
public static FindOneCommentResponse from(Comment comment) { | ||
return new FindOneCommentResponse(comment.getCommentId(), comment.getContent()); | ||
} | ||
} |
73 changes: 57 additions & 16 deletions
73
src/main/java/com/unit/daybook/domain/comment/entity/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,63 @@ | ||
package com.unit.daybook.domain.comment.entity; | ||
|
||
import jakarta.persistence.*; | ||
import com.unit.daybook.domain.board.entity.Board; | ||
import com.unit.daybook.domain.common.model.BaseTimeEntity; | ||
import com.unit.daybook.domain.member.domain.Member; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
//@Getter | ||
//@Entity | ||
//@Table(name = "comment") | ||
//@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
//public class Comment { | ||
// | ||
// @Id | ||
// @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
// @Column(name = "comment_id") | ||
// private Long commentId; | ||
// | ||
// @Column | ||
// private String content; | ||
// | ||
//} | ||
@Getter | ||
@Entity | ||
@Table(name = "comment") | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Comment extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "comment_id") | ||
private Long commentId; | ||
|
||
@Column(nullable = false, length = 25) | ||
private String content; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "board_id") | ||
private Board board; | ||
|
||
@Builder(access = AccessLevel.PRIVATE) | ||
private Comment(String content, Member member, Board board) { | ||
this.content = content; | ||
this.member = member; | ||
this.board = board; | ||
} | ||
|
||
public static Comment createComment( | ||
String content, Member member, Board board | ||
) { | ||
return Comment.builder() | ||
.content(content) | ||
.member(member) | ||
.board(board) | ||
.build(); | ||
} | ||
|
||
public void updateContent(String content) { | ||
this.content = content; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/unit/daybook/domain/comment/repository/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.unit.daybook.domain.comment.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long>, CommentRepositoryCustom { | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.unit.daybook.domain.comment.repository; | ||
|
||
import java.util.List; | ||
|
||
import com.unit.daybook.domain.comment.dto.response.FindOneCommentResponse; | ||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
public interface CommentRepositoryCustom { | ||
List<Comment> findCommentByBoard(Long boardId); | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/unit/daybook/domain/comment/repository/CommentRepositoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.unit.daybook.domain.comment.repository; | ||
|
||
import static com.unit.daybook.domain.board.entity.QBoard.*; | ||
import static com.unit.daybook.domain.comment.entity.QComment.*; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.unit.daybook.domain.comment.entity.Comment; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class CommentRepositoryImpl implements CommentRepositoryCustom{ | ||
|
||
private final JPAQueryFactory jpaQueryFactory; | ||
|
||
@Override | ||
public List<Comment> findCommentByBoard(Long boardId) { | ||
return jpaQueryFactory.selectFrom(comment) | ||
.leftJoin(comment.board, board) | ||
.fetchJoin() | ||
.where(board.boardId.eq(boardId)) | ||
.orderBy(comment.createdAt.desc()) | ||
.fetch(); | ||
} | ||
} |
Oops, something went wrong.