Skip to content
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.projectlombok:lombok:1.18.30'
implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.mysql:mysql-connector-j'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.URI;
import java.util.List;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -45,7 +46,7 @@ public ResponseEntity<ArticleResponse> getArticle(

@PostMapping("/articles")
public ResponseEntity<ArticleResponse> crateArticle(
@RequestBody ArticleCreateRequest request
@Valid @RequestBody ArticleCreateRequest request
) {
ArticleResponse response = articleService.create(request);
return ResponseEntity.created(URI.create("/articles/" + response.id())).body(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -39,7 +40,7 @@ public BoardResponse getBoard(

@PostMapping("/boards")
public BoardResponse createBoard(
@RequestBody BoardCreateRequest request
@Valid @RequestBody BoardCreateRequest request
) {
return boardService.createBoard(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -41,7 +42,7 @@ public ResponseEntity<MemberResponse> getMember(

@PostMapping("/members")
public ResponseEntity<MemberResponse> create(
@RequestBody MemberCreateRequest request
@Valid @RequestBody MemberCreateRequest request
) {
MemberResponse response = memberService.create(request);
return ResponseEntity.ok(response);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.demo.controller.dto.request;

import jakarta.annotation.Nonnull;
import jakarta.validation.constraints.NotNull;

public record ArticleCreateRequest(
Long authorId,
Long boardId,
String title,
String description
@NotNull(message = "멤버ID를 입력해주세요.") Long authorId,
@NotNull(message = "게시판ID를 입력해주세요.") Long boardId,
@NotNull(message = "제목을 입력해주세요.") String title,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NotNull 어노테이션 잘 사용하셨네요!

@NotNull(message = "내용을 입력해주세요.") String content
) {

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.demo.controller.dto.request;

import jakarta.validation.constraints.NotNull;

public record BoardCreateRequest(
String name
@NotNull(message = "이름을 입력해주세요.") String name
) {

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.example.demo.controller.dto.request;

import jakarta.validation.constraints.NotNull;

public record MemberCreateRequest(
String name,
String email,
String password
@NotNull(message = "이름을 입력해주세요.") String name,
@NotNull(message = "이메일을 입력해주세요.") String email,
@NotNull(message = "비밀번호를 입력해주세요") String password
) {

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.example.demo.controller.dto.request;

import jakarta.validation.constraints.NotNull;

public record MemberUpdateRequest(
String name,
String email
@NotNull(message = "멤버의 이름을 적어주세요.") String name,
@NotNull(message = "멤버의 이메일을 적어주세요") String email
) {

}
86 changes: 73 additions & 13 deletions src/main/java/com/example/demo/domain/Article.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package com.example.demo.domain;

import jakarta.persistence.*;

import java.time.LocalDateTime;

@Entity
public class Article {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "article_id")
private Long id;
private Long authorId;
private Long boardId;
// private Long authorId;
// private Long boardId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "author_id")
private Member member;

@ManyToOne
@JoinColumn(name = "board_id")
private Board board;

private String title;
private String content;
@Column(name = "created_date")
private LocalDateTime createdAt;
@Column(name = "modified_date")
private LocalDateTime modifiedAt;

public Article(
/* public Article(
Long id,
Long authorId,
Long boardId,
Expand All @@ -37,16 +54,33 @@ public Article(Long authorId, Long boardId, String title, String content) {
this.title = title;
this.content = content;
this.createdAt = LocalDateTime.now();
this.modifiedAt = LocalDateTime.now();
}*/

public Article() {

}

public void update(Long boardId, String title, String description) {
this.boardId = boardId;
public void update(Board board, String title, String description) {
this.board = board;
this.title = title;
this.content = description;
this.modifiedAt = LocalDateTime.now();
}

public void setMember(Member member) {
this.member = member;
member.getArticles().add(this);
}

public void setBoard(Board board) {
this.board = board;
board.getArticles().add(this);
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public void setId(Long id) {
this.id = id;
}
Expand All @@ -59,13 +93,13 @@ public Long getId() {
return id;
}

public Long getAuthorId() {
return authorId;
}

public Long getBoardId() {
return boardId;
}
// public Long getAuthorId() {
// return authorId;
// }
//
// public Long getBoardId() {
// return boardId;
// }

public String getTitle() {
return title;
Expand All @@ -82,4 +116,30 @@ public LocalDateTime getCreatedAt() {
public LocalDateTime getModifiedAt() {
return modifiedAt;
}

public Member getMember() {
return member;
}

public Board getBoard() {
return board;
}

private void setTitle(String title) {
this.title = title;
}

private void setContent(String content) {
this.content = content;
}

public static Article createArticle(String title, String content, Member member, Board board) {
Article article = new Article();
article.setTitle(title);
article.setContent(content);
article.setMember(member);
article.setBoard(board);
article.setCreatedAt(LocalDateTime.now());
return article;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/example/demo/domain/Board.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package com.example.demo.domain;

import jakarta.persistence.*;

import java.util.List;

import static jakarta.persistence.CascadeType.ALL;

@Entity
public class Board {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_id")
private Long id;
private String name;

@OneToMany(mappedBy = "board", orphanRemoval = true, cascade = ALL)
private List<Article> articles;

public Board(Long id, String name) {
this.id = id;
this.name = name;
Expand All @@ -14,6 +27,10 @@ public Board(String name) {
this.name = name;
}

public Board() {

}

public Long getId() {
return id;
}
Expand All @@ -29,4 +46,8 @@ public String getName() {
public void update(String name) {
this.name = name;
}

public List<Article> getArticles() {
return articles;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/example/demo/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.example.demo.domain;

import jakarta.persistence.*;

import java.util.List;

import static jakarta.persistence.CascadeType.ALL;

@Entity
public class Member {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_id")
private Long id;
private String name;
private String email;
private String password;
@OneToMany(mappedBy = "member", orphanRemoval = true, cascade = ALL)
private List<Article> articles;

public Member(Long id, String name, String email, String password) {
this.id = id;
Expand All @@ -20,6 +32,11 @@ public Member(String name, String email, String password) {
this.password = password;
}

public Member() {

}


public void update(String name, String email) {
this.name = name;
this.email = email;
Expand All @@ -44,4 +61,8 @@ public String getEmail() {
public String getPassword() {
return password;
}

public List<Article> getArticles() {
return articles;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.demo.exception.errorcode;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum CommonErrorCode implements ErrorCode {
EMAIL_ALREADY_EXIST(HttpStatus.CONFLICT, "이미 존재하는 이메일입니다."),
GET_ARTICLE_NOT_EXIST(HttpStatus.NOT_FOUND, "입력한 ID에 해당하는 게시물이 존재하지 않습니다."),
GET_MEMBER_NOT_EXIST(HttpStatus.NOT_FOUND, "입력한 ID에 해당하는 멤버가 존재하지 않습니다."),
GET_BOARD_NOT_EXIST(HttpStatus.NOT_FOUND, "입력한 ID에 해당하는 게시판이 존재하지 않습니다."),
POST_ARTICLE_NOT_EXIST(HttpStatus.BAD_REQUEST, "입력한 ID에 해당하는 게시물이 존재하지 않습니다."),
POST_MEMBER_NOT_EXIST(HttpStatus.BAD_REQUEST, "입력한 ID에 해당하는 멤버가 존재하지 않습니다."),
POST_BOARD_NOT_EXIST(HttpStatus.BAD_REQUEST, "입력한 ID에 해당하는 게시판이 존재하지 않습니다."),
UPDATE_BOARD_NOT_EXIST(HttpStatus.BAD_REQUEST,"입력한 ID에 해당하는 게시판이 존재하지 않습니다." ),
Comment on lines +16 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동일한 상태코드와 반환 메세지라면 하나 통일해서 써도 될 것 같아요

UPDATE_NAME_NULL(HttpStatus.BAD_REQUEST, "변경할 이름을 입력해주세요"),
UPDATE_EMAIL_NULL(HttpStatus.BAD_REQUEST, "변경할 이메일을 입력해주세요"),
DELETE_ARTICLE_EXIST_IN_MEMBER(HttpStatus.BAD_REQUEST, "해당 멤버가 작성한 게시물이 존재합니다."),
DELETE_ARTICLE_EXIST_IN_BOARD(HttpStatus.BAD_REQUEST, "해당 게시판에 게시물이 존재합니다.");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.demo.exception.errorcode;

import org.springframework.http.HttpStatus;

public interface ErrorCode {
String name();
HttpStatus getHttpStatus();
String getMessage();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.demo.exception.exception;

import com.example.demo.exception.errorcode.ErrorCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class RestApiException extends RuntimeException{
private final ErrorCode errorCode;
}
Loading