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
25 changes: 25 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Todo List
- 게시글 구현
- [X] 게시글 entity 작성(제목,내용,작성일,수정일,삭제여부)
- [X] 게시글 entity 데이터베이스 연동(docker로 구현한 postgresSql과의 연결)
- [X] 게시글 작성 API 구현 (POST) / 제목,내용 필수항목, 작성일자 자동기록
- [X] 게시글 수정 API 구현 (PUT) / 제목,내용 수정가능, 수정일자 자동기록
- [X] 수정하고자 하는 게시글이 존재하는지 확인
- [X] 존재하지 않는경우 예외처리
- [X] 존재하는경우 수정할 게시글의 내용 업데이트
- [X] 게시글 단건 조회 API 구현 (GET) / 게시글에 포함된 모든 댓글 목록 조회, 삭제데이터 조회불가능
- [X] 삭제된 게시글을 조회하는경우 예외처리
- [X] 게시글 조회 시 함께 조회되는 댓글목록 페이징 처리 (10 고정 10/30/50 순)
- [X] 게시글 삭제 API 구현 (DELETE) / 삭제시 게시글의 댓글로 삭제처리, 복구불가능, softDelete 표현
- [X] 게시글 삭제 시 연결된 댓글도 전부 삭제되도록 구현.

- 댓글 구현
- [X] 댓글 entity 구현
- [X] 댓글 작성 API 구현 / 댓글내용 필수항목 등록일자 자동기록
- [X] 게시글 번호에 맞추어 댓글 작성진행
- [X] 게시글이 존재하는지 확인, 존재하지않는경우 예외처리
- [X] 댓글 수정 API 구현
- [X] 댓글이 존재하는지 확인
- [X] 댓글 조회 API 구현
- [X] 댓글 삭제 API 구현
- [X] 삭제 후 조회,수정,다시삭제 불가능하도록 설정
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
Expand Down
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,8 +2,10 @@

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

@SpringBootApplication
@EnableJpaAuditing
public class BoardApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.sparta.board.application.dto.request;

import io.sparta.board.domain.model.Comment;
import io.sparta.board.domain.model.Post;

public record CommentRequestDto(
String content,
Long postId
) {
public Comment createComment(Post post) {
return Comment.builder()
.content(content)
.post(post)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.sparta.board.application.dto.request;

public record CommentUpdateRequestDto(
String content
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.sparta.board.application.dto.request;

import io.sparta.board.domain.model.Post;

public record PostRequestDto(
String title,
String content
) {

public Post createPost() {
return Post.builder()
.title(title)
.content(content)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.sparta.board.application.dto.response;

import io.sparta.board.domain.model.Comment;
import lombok.Builder;

import java.util.Date;

@Builder
public record CommentResponseDto(
Long id,
String content,
Long postId,
Date createdAt,
Date updatedAt
) {

public static CommentResponseDto from(Comment comment) {
return CommentResponseDto.builder()
.id(comment.getId())
.content(comment.getContent())
.postId(comment.getPost().getId())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.sparta.board.application.dto.response;

import io.sparta.board.domain.model.Comment;
import lombok.Builder;

import java.util.Date;

@Builder
public record CommentUpdateResponseDto(
Long id,
String content,
Long postId,
Date updatedAt
) {

public static CommentUpdateResponseDto from(Comment comment) {
return CommentUpdateResponseDto.builder()
.id(comment.getId())
.content(comment.getContent())
.postId(comment.getPost().getId())
.updatedAt(comment.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.sparta.board.application.dto.response;

import io.sparta.board.domain.model.Post;
import lombok.Builder;
import org.springframework.data.domain.Page;

import java.util.Date;

@Builder
public record PostResponseDto(
Long id,
String title,
String content,
Date createdAt,
Date updatedAt,
Page<CommentResponseDto> comments
) {
public static PostResponseDto from(Post post, Page<CommentResponseDto> commentPage) {
return PostResponseDto.builder()
.id(post.getId())
.title(post.getTitle())
.content(post.getContent())
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.comments(commentPage)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.sparta.board.application.dto.response;

import io.sparta.board.domain.model.Post;
import lombok.Builder;

import java.util.Date;

@Builder
public record PostUpdateResponseDto(
Long id,
String title,
String content,
Date updatedAt
) {

public static PostUpdateResponseDto from(Post post) {
return PostUpdateResponseDto.builder()
.id(post.getId())
.title(post.getTitle())
.content(post.getContent())
.updatedAt(post.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.sparta.board.application.service;

import io.sparta.board.application.dto.request.CommentRequestDto;
import io.sparta.board.application.dto.request.CommentUpdateRequestDto;
import io.sparta.board.application.dto.response.CommentResponseDto;
import io.sparta.board.application.dto.response.CommentUpdateResponseDto;
import io.sparta.board.domain.model.Comment;
import io.sparta.board.domain.model.Post;
import io.sparta.board.infastructure.JpaCommentRepository;
import io.sparta.board.infastructure.JpaPostRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class CommentService {

private final JpaCommentRepository commentRepository;
private final JpaPostRepository postRepository;

public Long createComment(CommentRequestDto requestDto) {
Post post = findPostById(requestDto.postId());
Comment comment = commentRepository.save(requestDto.createComment(post));
return comment.getId();
}

public CommentUpdateResponseDto modifyComment(Long id, CommentUpdateRequestDto requestDto) {
Comment comment = findCommentById(id);
comment.updateComment(requestDto);
return CommentUpdateResponseDto.from(comment);
}

public CommentResponseDto getComment(Long id) {
return CommentResponseDto.from(findCommentById(id));
}

public void deleteComment(Long id) {
commentRepository.deleteById(id);
}


public Comment findCommentById(Long id) {
return commentRepository.findById(id).orElseThrow(
()-> new NullPointerException("존재하지않는 게시글입니다."));
}

public Post findPostById(Long id) {
return postRepository.findById(id).orElseThrow(()-> new NullPointerException("존재하지 않는 게시글입니다."));
}

}
56 changes: 56 additions & 0 deletions src/main/java/io/sparta/board/application/service/PostService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.sparta.board.application.service;

import io.sparta.board.application.dto.request.PostRequestDto;
import io.sparta.board.application.dto.response.CommentResponseDto;
import io.sparta.board.application.dto.response.PostResponseDto;
import io.sparta.board.application.dto.response.PostUpdateResponseDto;
import io.sparta.board.domain.model.Comment;
import io.sparta.board.domain.model.Post;
import io.sparta.board.infastructure.JpaCommentRepository;
import io.sparta.board.infastructure.JpaPostRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class PostService {

private final JpaPostRepository postRepository;
private final JpaCommentRepository commentRepository;


public Long createPost(PostRequestDto requestDto) {
Post post = postRepository.save(requestDto.createPost());
return post.getId();
}

public PostUpdateResponseDto modifyPost(Long id, PostRequestDto requestDto) {
Post post = findPostById(id);
post.updatePost(requestDto);
return PostUpdateResponseDto.from(post);
}

public PostResponseDto getPost(Long id, int page, int size) {
Post post= findPostById(id);
Pageable pageable = PageRequest.of(page, size);
Page<Comment> commentPage = commentRepository.findAllByPost(post, pageable);

return PostResponseDto.from(post,commentPage.map(CommentResponseDto::from));
}

public void deletePost(Long id) {
postRepository.deleteById(id);
}


public Post findPostById(Long id) {
return postRepository.findById(id).orElseThrow(()-> new IllegalArgumentException("존재하지 않는 게시글입니다."));
}


}
37 changes: 37 additions & 0 deletions src/main/java/io/sparta/board/domain/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.sparta.board.domain.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import io.sparta.board.application.dto.request.CommentUpdateRequestDto;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "p_comments")
@NoArgsConstructor
public class Comment extends Default{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comment_id", nullable = false)
Long Id;

@Column(nullable = false)
String content;

@JsonBackReference
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", referencedColumnName = "post_id", insertable = true)
Post post;

@Builder
public Comment(String content, Post post){
this.content = content;
this.post = post;
}
public void updateComment(CommentUpdateRequestDto requestDto) {
this.content = requestDto.content();
}

}
30 changes: 30 additions & 0 deletions src/main/java/io/sparta/board/domain/model/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.sparta.board.domain.model;

import jakarta.persistence.*;
import lombok.Getter;
import org.hibernate.annotations.SoftDelete;
import org.hibernate.type.TrueFalseConverter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Date;
@Getter
@SoftDelete(converter = TrueFalseConverter.class, columnName = "is_deleted")
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class Default {

@CreatedDate
@Column(name = "created_at", updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date createdAt;

@LastModifiedDate
@Column(name = "updated_at")
@Temporal(TemporalType.TIMESTAMP)
private Date updatedAt;

@Column(name = "is_deleted", insertable = false, updatable = false)
private boolean isDeleted;
}
Loading