Skip to content
Open
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
27 changes: 27 additions & 0 deletions http/Board.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@postId = 3c275155-6199-417a-8773-a2311b85a183

### CREATE POST
POST http://localhost:8080/posts
Content-Type: application/json

{
"title" : "test",
"content" : "test"
}

### UPDATE POST
PUT http://localhost:8080/posts/{{postId}}
Content-Type: application/json

{
"title" : "test 수정",
"content" : "test"
}

### DELETE POST
DELETE http://localhost:8080/posts/{{postId}}
Content-Type: application/json

### GET POST DETAILS
GET http://localhost:8080/posts/{{postId}}?page=1&size=15
Content-Type: application/json
22 changes: 22 additions & 0 deletions http/Comment.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@postId = 3cc2939c-c475-4c15-9b8a-b3f1880693e3
@commentId = ba24ae4b-ac3a-485f-9142-27c19af550ac

### Create Comment
POST localhost:8080/posts/{{postId}}/comments
Content-Type: application/json

{
"content": "게시물에 댓글 달기2"
}

### Update Comment
PUT localhost:8080/comments/{{commentId}}
Content-Type: application/json

{
"content": "게시물에 댓글 수정하기"
}

### DELETE Comment
DELETE localhost:8080/comments/{{commentId}}
Content-Type: application/json
2 changes: 2 additions & 0 deletions src/main/java/io/sparta/board/BoardApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@EnableJpaAuditing
@SpringBootApplication
public class BoardApplication {

Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/sparta/board/controller/CommentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.sparta.board.controller;

import java.util.UUID;

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.RestController;

import io.sparta.board.dto.CommentRequestDto;
import io.sparta.board.dto.CommentResponseDto;
import io.sparta.board.service.CommentService;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
public class CommentController {

private final CommentService commentService;

//댓글 추가
@PostMapping("/posts/{postId}/comments")
public CommentResponseDto createComment(@PathVariable("postId") UUID postId, @RequestBody CommentRequestDto requestDto) {
return commentService.createComment(postId, requestDto);
}

//댓글 수정
@PutMapping("/comments/{commentId}")
public CommentResponseDto updateComment(@PathVariable("commentId") UUID commentId, @RequestBody CommentRequestDto requestDto) {
return commentService.updateComment(commentId, requestDto);
}

//댓글 삭제
@DeleteMapping("/comments/{commentId}")
public CommentResponseDto deleteComment(@PathVariable("commentId") UUID commentId) {
return commentService.deleteComment(commentId);
}
}
60 changes: 60 additions & 0 deletions src/main/java/io/sparta/board/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package io.sparta.board.controller;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.sparta.board.dto.PostRequestDto;
import io.sparta.board.dto.PostResponseDto;
import io.sparta.board.service.PostService;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/posts")
public class PostController {

private final PostService postService;

//게시글 작성
@PostMapping
public PostResponseDto createPost(@RequestBody PostRequestDto requestDto) {
return postService.createPost(requestDto);
}

//게시글 수정
@PutMapping("/{postId}")
public PostResponseDto updatePost(@PathVariable("postId") UUID postId, @RequestBody PostRequestDto requestDto) {
return postService.updatePost(postId, requestDto);
}

//게시글 삭제
@DeleteMapping("/{postId}")
public PostResponseDto deletePost(@PathVariable("postId") UUID postId) {
return postService.deletePost(postId);
}

//게시글 단건 조회
@GetMapping("/{postId}")
public PostResponseDto getPost(
@PathVariable("postId") UUID postId,
@RequestParam("page") int page,
@RequestParam(value = "size", defaultValue = "10") int size
) {
// Page Size가 10/30/50이 아닌 경우 10으로 고정
List<Integer> allowedSize = Arrays.asList(10, 30, 50);
int requestSize = allowedSize.contains(size) ? size : 10;

return postService.getPost(postId, page-1, requestSize);
}
}
9 changes: 9 additions & 0 deletions src/main/java/io/sparta/board/dto/CommentRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.sparta.board.dto;

import lombok.Getter;

@Getter
public class CommentRequestDto {

private String content;
}
34 changes: 34 additions & 0 deletions src/main/java/io/sparta/board/dto/CommentResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.sparta.board.dto;

import java.util.UUID;

import io.sparta.board.entity.Comment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@RequiredArgsConstructor
public class CommentResponseDto {

private UUID commentId;
private UUID postId;
private String content;

public CommentResponseDto(Comment comment) {
this.commentId = comment.getCommentId();
this.postId = comment.getPost().getPostId();
this.content = comment.getContent();
}

public static CommentResponseDto from (UUID commentId, UUID postId, String content) {
return CommentResponseDto.builder()
.commentId(commentId)
.postId(postId)
.content(content)
.build();
}
}
10 changes: 10 additions & 0 deletions src/main/java/io/sparta/board/dto/PostRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.sparta.board.dto;

import lombok.Getter;

@Getter
public class PostRequestDto {

private String title;
private String content;
}
40 changes: 40 additions & 0 deletions src/main/java/io/sparta/board/dto/PostResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.sparta.board.dto;

import java.time.LocalDateTime;
import java.util.UUID;

import org.springframework.data.domain.Page;

import io.sparta.board.entity.Post;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor
public class PostResponseDto {

private UUID postId;
private String title;
private String content;
private LocalDateTime createdAt;
private Page<CommentResponseDto> commentList;

public PostResponseDto(Post post, Page<CommentResponseDto> commentList) {
this.postId = post.getPostId();
this.title = post.getTitle();
this.content = post.getContent();
this.createdAt = post.getCreatedAt();
this.commentList = commentList;
}

public static PostResponseDto from(UUID postId, String title, String content, LocalDateTime createdAt) {
return PostResponseDto.builder()
.postId(postId)
.title(title)
.content(content)
.createdAt(createdAt)
.build();
}
}
52 changes: 52 additions & 0 deletions src/main/java/io/sparta/board/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.sparta.board.entity;

import java.util.UUID;

import io.sparta.board.dto.CommentRequestDto;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;

@Entity
@Getter
@Table(name = "p_comment")
public class Comment extends Timestamped {

@Id
@GeneratedValue
private UUID commentId;

@Column(nullable = false)
private String content;

@Column(nullable = false)
private boolean deleted;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = false)
private Post post;

public static Comment createComment(Post post, CommentRequestDto requestDto) {

Comment comment = new Comment();
comment.post = post;
comment.content = requestDto.getContent();
comment.deleted = false;

return comment;
}

public void update(String content) {
this.content = content;
}

public void delete(boolean b) {
this.deleted = b;
}
}
54 changes: 54 additions & 0 deletions src/main/java/io/sparta/board/entity/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.sparta.board.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
@Table(name = "p_post")
public class Post extends Timestamped{

@Id
@GeneratedValue
private UUID postId;

@Column(nullable = false, length = 100)
private String title;

@Column(nullable = false)
private String content;

@Column(nullable = false)
private boolean deleted;

@OneToMany(mappedBy = "post")
private List<Comment> commentList = new ArrayList<>();

public static Post createPost(String title, String content) {
Post post = new Post();
post.title = title;
post.content = content;
post.deleted = false;
return post;
}

public void update(String title, String content) {
this.title = title;
this.content = content;
}

public void delete(boolean b) {
this.deleted = b;
}
}
30 changes: 30 additions & 0 deletions src/main/java/io/sparta/board/entity/Timestamped.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.sparta.board.entity;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import lombok.Getter;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Timestamped {

@CreatedDate
@Column(updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime createdAt;

@LastModifiedDate
@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime modifiedAt;
}
Loading