Skip to content

Commit 2a0a972

Browse files
committed
Bugfix board
1 parent 189e741 commit 2a0a972

File tree

6 files changed

+38
-16
lines changed

6 files changed

+38
-16
lines changed

src/main/java/com/rest/api/entity/User.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@NoArgsConstructor // 인자없는 생성자를 자동으로 생성합니다.
2424
@AllArgsConstructor // 인자를 모두 갖춘 생성자를 자동으로 생성합니다.
2525
@Table(name = "user") // 'user' 테이블과 매핑됨을 명시
26-
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
26+
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) // Post Entity에서 User와의 관계를 Json으로 변환시 오류 방지를 위한 코드
2727
public class User extends CommonDateEntity implements UserDetails {
2828
@Id // pk
2929
@GeneratedValue(strategy = GenerationType.IDENTITY)

src/main/java/com/rest/api/entity/board/Board.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
import lombok.Getter;
55
import lombok.NoArgsConstructor;
66

7-
import javax.persistence.Entity;
8-
import javax.persistence.GeneratedValue;
9-
import javax.persistence.GenerationType;
10-
import javax.persistence.Id;
7+
import javax.persistence.*;
118

129
@Entity
1310
@Getter
1411
@NoArgsConstructor
1512
public class Board extends CommonDateEntity {
1613
@Id
1714
@GeneratedValue(strategy = GenerationType.IDENTITY)
18-
private Long id;
15+
private Long boardId;
16+
@Column(nullable = false, length = 100)
1917
private String name;
2018
}

src/main/java/com/rest/api/entity/board/Post.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,38 @@
1313
public class Post extends CommonDateEntity {
1414
@Id
1515
@GeneratedValue(strategy = GenerationType.IDENTITY)
16-
private Long id;
16+
private Long postId;
17+
@Column(nullable = false, length = 50)
1718
private String author;
19+
@Column(nullable = false, length = 100)
1820
private String title;
21+
@Column(length = 500)
1922
private String content;
20-
private Long boardId;
2123

2224
@ManyToOne(fetch = FetchType.LAZY)
23-
private User user;
25+
@JoinColumn(name = "board_id")
26+
private Board board; // 게시글 - 게시판의 관계 - N:1
2427

25-
public Post(User user, Long boardId, String author, String title, String content) {
28+
29+
@ManyToOne(fetch = FetchType.LAZY)
30+
@JoinColumn(name = "msrl")
31+
private User user; // 게시글 - 회원의 관계 - N:1
32+
33+
// Join 테이블이 Json결과에 표시되지 않도록 처리.
34+
protected Board getBoard() {
35+
return board;
36+
}
37+
38+
// 생성자
39+
public Post(User user, Board board, String author, String title, String content) {
2640
this.user = user;
27-
this.boardId = boardId;
41+
this.board = board;
2842
this.author = author;
2943
this.title = title;
3044
this.content = content;
3145
}
3246

47+
// 수정시 데이터 처리
3348
public Post setUpdate(String author, String title, String content) {
3449
this.author = author;
3550
this.title = title;

src/main/java/com/rest/api/model/board/ParamsPost.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,22 @@
55
import lombok.NoArgsConstructor;
66
import lombok.Setter;
77

8+
import javax.validation.constraints.Max;
89
import javax.validation.constraints.NotEmpty;
910

1011
@Getter
1112
@Setter
1213
@NoArgsConstructor
1314
public class ParamsPost {
1415
@NotEmpty
16+
@Max(50)
1517
@ApiModelProperty(value = "작성자명", required = true)
1618
private String author;
1719
@NotEmpty
20+
@Max(100)
1821
@ApiModelProperty(value = "제목", required = true)
1922
private String title;
20-
@NotEmpty
23+
@Max(500)
2124
@ApiModelProperty(value = "내용", required = true)
2225
private String content;
2326
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.rest.api.repo.board;
22

3+
import com.rest.api.entity.board.Board;
34
import com.rest.api.entity.board.Post;
45
import org.springframework.data.jpa.repository.JpaRepository;
56

67
import java.util.List;
78

89
public interface PostJpaRepo extends JpaRepository<Post, Long> {
9-
List<Post> findByBoardId(Long boardId);
10+
List<Post> findByBoard(Board board);
1011
}

src/main/java/com/rest/api/service/board/BoardService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,29 @@ public class BoardService {
2626
private final PostJpaRepo postJpaRepo;
2727
private final UserJpaRepo userJpaRepo;
2828

29+
// 게시판 이름으로 게시판을 조회. 없을경우 CResourceNotExistException 처리
2930
public Board findBoard(String boardName) {
3031
return Optional.ofNullable(boardJpaRepo.findByName(boardName)).orElseThrow(CResourceNotExistException::new);
3132
}
3233

34+
// 게시판 이름으로 게시물 리스트 조회.
3335
public List<Post> findPosts(String boardName) {
34-
Board board = findBoard(boardName);
35-
return postJpaRepo.findByBoardId(board.getId());
36+
return postJpaRepo.findByBoard(findBoard(boardName));
3637
}
3738

39+
// 게시물ID로 게시물 단건 조회. 없을경우 CResourceNotExistException 처리
3840
public Post getPost(long postId) {
3941
return postJpaRepo.findById(postId).orElseThrow(CResourceNotExistException::new);
4042
}
4143

44+
// 게시물을 등록합니다. 게시물의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
4245
public Post writePost(String uid, String boardName, ParamsPost paramsPost) {
4346
Board board = findBoard(boardName);
44-
Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board.getId(), paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent());
47+
Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board, paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent());
4548
return postJpaRepo.save(post);
4649
}
4750

51+
// 게시물을 수정합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
4852
public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
4953
Post post = getPost(postId);
5054
User user = post.getUser();
@@ -55,6 +59,7 @@ public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
5559
return postJpaRepo.save(post);
5660
}
5761

62+
// 게시물을 삭제합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
5863
public boolean deletePost(long postId, String uid) {
5964
Post post = getPost(postId);
6065
User user = post.getUser();

0 commit comments

Comments
 (0)