Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4기 김영명, 김희빈] Spring Boot JPA로 게시판 구현 미션 제출합니다. #240

Open
wants to merge 23 commits into
base: 희빈,영명-mission-제출
Choose a base branch
from
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
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
gradlew
gradlew.bat
gradle
BOOT-INF

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
77 changes: 77 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.2'
id 'io.spring.dependency-management' version '1.1.2'
id "org.asciidoctor.jvm.convert" version "3.3.2"
}

group = 'com.prgrms'
version = '0.0.1-SNAPSHOT'

java {
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

ext {
snippetsDir = file('build/generated-snippets')
}

bootJar {
dependsOn asciidoctor
copy {
from "${asciidoctor.outputDir}"
into 'BOOT-INF/classes/static/docs'
}
}

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'org.springframework.boot:spring-boot-starter-validation'

implementation 'org.springframework.boot:spring-boot-starter-web'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

runtimeOnly 'com.h2database:h2'

runtimeOnly 'com.mysql:mysql-connector-j'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

testImplementation 'org.springframework.restdocs:spring-restdocs-mockmvc'
}

tasks.named('test') {
useJUnitPlatform()
}

asciidoctor {
dependsOn test
inputs.dir snippetsDir
}

asciidoctor.doFirst {
delete file('src/main/resources/static/docs')
}

tasks.register('copyDocument', Copy) {
dependsOn asciidoctor
from file("build/docs/asciidoc")
into file("src/main/resources/static/docs")
}

build {
dependsOn copyDocument
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'board'
41 changes: 41 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
= Board REST API

== 게시물 생성

=== REQUEST

include::{snippets}/posts/createPost/http-request.adoc[]

=== RESPONSE

include::{snippets}/posts/createPost/http-response.adoc[]

== 게시물 전체 조회

=== REQUEST

include::{snippets}/posts/getPosts/http-request.adoc[]

=== RESPONSE

include::{snippets}/posts/getPosts/http-response.adoc[]

== 게시물 조회

=== REQUEST

include::{snippets}/posts/getPost/http-request.adoc[]

=== RESPONSE

include::{snippets}/posts/getPost/http-response.adoc[]

== 게시물 수정

=== REQUEST

include::{snippets}/posts/updatePost/http-request.adoc[]

=== RESPONSE

include::{snippets}/posts/updatePost/http-response.adoc[]
12 changes: 12 additions & 0 deletions src/main/java/com/prgrms/board/BoardApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.prgrms.board;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BoardApplication {

public static void main(String[] args) {
SpringApplication.run(BoardApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.prgrms.board.domain.post.controller;

import static com.prgrms.board.global.common.SuccessMessage.*;
import static org.springframework.http.HttpStatus.*;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.prgrms.board.domain.post.dto.request.PostCreateRequest;
import com.prgrms.board.domain.post.dto.request.PostUpdateRequest;
import com.prgrms.board.domain.post.dto.response.PostDetailResponse;
import com.prgrms.board.domain.post.dto.response.PostResponse;
import com.prgrms.board.domain.post.entity.Post;
import com.prgrms.board.domain.post.service.PostService;
import com.prgrms.board.global.common.dto.BaseResponse;
import com.prgrms.board.global.common.dto.PageResponse;

import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;

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

private final PostService postService;

@GetMapping
public BaseResponse<PageResponse<Post>> getPosts(
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable
) {
PageResponse<Post> data = postService.getPosts(pageable);
return BaseResponse.ok(GET_POSTS_SUCCESS, data);
}

@GetMapping("/{postId}")
public BaseResponse<PostDetailResponse> getPost(@PathVariable Long postId) {
PostDetailResponse data = postService.getPost(postId);
return BaseResponse.ok(GET_POST_SUCCESS, data);
}

@PostMapping
@ResponseStatus(CREATED)
public BaseResponse<PostResponse> createPost(@Valid @RequestBody PostCreateRequest request) {
PostResponse data = postService.createPost(request);
return BaseResponse.created(CREATE_POST_SUCCESS, data);
}

@PatchMapping("/{postId}")
public BaseResponse<PostResponse> updatePost(@PathVariable Long postId, @RequestBody PostUpdateRequest request) {
PostResponse data = postService.updatePost(postId, request);
return BaseResponse.ok(UPDATE_POST_SUCCESS, data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.prgrms.board.domain.post.dto.request;

import com.prgrms.board.domain.post.entity.Post;
import com.prgrms.board.domain.user.entity.User;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

public record PostCreateRequest(
@NotNull(message = "유저 아이디는 필수입니다.")
Long userId,

@NotBlank(message = "제목은 필수입니다.")
@Size(max = 30, message = "제목은 최대 30자까지 가능합니다.")
String title,

@NotBlank(message = "내용은 필수입니다.")
String content
) {
public Post toEntity(User user) {
return Post.create(user, title, content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.prgrms.board.domain.post.dto.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record PostUpdateRequest(
@NotBlank(message = "제목은 필수입니다.")
@Size(max = 30, message = "제목은 최대 30자까지 가능합니다.")
String title,

@NotBlank(message = "내용은 필수입니다.")
String content
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.prgrms.board.domain.post.dto.response;

import java.time.LocalDateTime;

import com.prgrms.board.domain.post.entity.Post;

import lombok.Builder;

@Builder
public record PostDetailResponse(
Long postId,
Long userId,
String username,
String title,
String content,
LocalDateTime createdAt,
LocalDateTime updatedAt
) {
public static PostDetailResponse from(Post post) {
return PostDetailResponse.builder()
.postId(post.getId())
.userId(post.getUser().getId())
.username(post.getUser().getName())
.title(post.getTitle())
.content(post.getContent())
.createdAt(post.getCreatedAt())
.updatedAt(post.getUpdatedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.prgrms.board.domain.post.dto.response;

import com.prgrms.board.domain.post.entity.Post;

import lombok.Builder;

@Builder
public record PostResponse(
Long postId,
String title,
String content
) {
public static PostResponse from(Post post) {
return PostResponse.builder()
.postId(post.getId())
.title(post.getTitle())
.content(post.getContent())
.build();
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/prgrms/board/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.prgrms.board.domain.post.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.prgrms.board.domain.user.entity.User;
import com.prgrms.board.global.common.BaseEntity;

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.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "post")
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Post extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "post_id")
private Long id;

@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;

@Column(name = "title", length = 30, nullable = false)
private String title;

@Column(name = "content", nullable = false, columnDefinition = "text")
private String content;

public static Post create(User user, String title, String content) {
return Post.builder()
.user(user)
.title(title)
.content(content)
.build();
}

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