Bugfix board
This commit is contained in:
@@ -23,7 +23,7 @@ import java.util.stream.Collectors;
|
||||
@NoArgsConstructor // 인자없는 생성자를 자동으로 생성합니다.
|
||||
@AllArgsConstructor // 인자를 모두 갖춘 생성자를 자동으로 생성합니다.
|
||||
@Table(name = "user") // 'user' 테이블과 매핑됨을 명시
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) // Post Entity에서 User와의 관계를 Json으로 변환시 오류 방지를 위한 코드
|
||||
public class User extends CommonDateEntity implements UserDetails {
|
||||
@Id // pk
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
|
||||
@@ -4,10 +4,7 @@ import com.rest.api.entity.common.CommonDateEntity;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@@ -15,6 +12,7 @@ import javax.persistence.Id;
|
||||
public class Board extends CommonDateEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Long boardId;
|
||||
@Column(nullable = false, length = 100)
|
||||
private String name;
|
||||
}
|
||||
|
||||
@@ -13,23 +13,38 @@ import javax.persistence.*;
|
||||
public class Post extends CommonDateEntity {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
private Long postId;
|
||||
@Column(nullable = false, length = 50)
|
||||
private String author;
|
||||
@Column(nullable = false, length = 100)
|
||||
private String title;
|
||||
@Column(length = 500)
|
||||
private String content;
|
||||
private Long boardId;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
private User user;
|
||||
@JoinColumn(name = "board_id")
|
||||
private Board board; // 게시글 - 게시판의 관계 - N:1
|
||||
|
||||
public Post(User user, Long boardId, String author, String title, String content) {
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "msrl")
|
||||
private User user; // 게시글 - 회원의 관계 - N:1
|
||||
|
||||
// Join 테이블이 Json결과에 표시되지 않도록 처리.
|
||||
protected Board getBoard() {
|
||||
return board;
|
||||
}
|
||||
|
||||
// 생성자
|
||||
public Post(User user, Board board, String author, String title, String content) {
|
||||
this.user = user;
|
||||
this.boardId = boardId;
|
||||
this.board = board;
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
// 수정시 데이터 처리
|
||||
public Post setUpdate(String author, String title, String content) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
|
||||
@@ -5,6 +5,7 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Getter
|
||||
@@ -12,12 +13,14 @@ import javax.validation.constraints.NotEmpty;
|
||||
@NoArgsConstructor
|
||||
public class ParamsPost {
|
||||
@NotEmpty
|
||||
@Max(50)
|
||||
@ApiModelProperty(value = "작성자명", required = true)
|
||||
private String author;
|
||||
@NotEmpty
|
||||
@Max(100)
|
||||
@ApiModelProperty(value = "제목", required = true)
|
||||
private String title;
|
||||
@NotEmpty
|
||||
@Max(500)
|
||||
@ApiModelProperty(value = "내용", required = true)
|
||||
private String content;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package com.rest.api.repo.board;
|
||||
|
||||
import com.rest.api.entity.board.Board;
|
||||
import com.rest.api.entity.board.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostJpaRepo extends JpaRepository<Post, Long> {
|
||||
List<Post> findByBoardId(Long boardId);
|
||||
List<Post> findByBoard(Board board);
|
||||
}
|
||||
@@ -26,25 +26,29 @@ public class BoardService {
|
||||
private final PostJpaRepo postJpaRepo;
|
||||
private final UserJpaRepo userJpaRepo;
|
||||
|
||||
// 게시판 이름으로 게시판을 조회. 없을경우 CResourceNotExistException 처리
|
||||
public Board findBoard(String boardName) {
|
||||
return Optional.ofNullable(boardJpaRepo.findByName(boardName)).orElseThrow(CResourceNotExistException::new);
|
||||
}
|
||||
|
||||
// 게시판 이름으로 게시물 리스트 조회.
|
||||
public List<Post> findPosts(String boardName) {
|
||||
Board board = findBoard(boardName);
|
||||
return postJpaRepo.findByBoardId(board.getId());
|
||||
return postJpaRepo.findByBoard(findBoard(boardName));
|
||||
}
|
||||
|
||||
// 게시물ID로 게시물 단건 조회. 없을경우 CResourceNotExistException 처리
|
||||
public Post getPost(long postId) {
|
||||
return postJpaRepo.findById(postId).orElseThrow(CResourceNotExistException::new);
|
||||
}
|
||||
|
||||
// 게시물을 등록합니다. 게시물의 회원UID가 조회되지 않으면 CUserNotFoundException 처리합니다.
|
||||
public Post writePost(String uid, String boardName, ParamsPost paramsPost) {
|
||||
Board board = findBoard(boardName);
|
||||
Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board.getId(), paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent());
|
||||
Post post = new Post(userJpaRepo.findByUid(uid).orElseThrow(CUserNotFoundException::new), board, paramsPost.getAuthor(), paramsPost.getTitle(), paramsPost.getContent());
|
||||
return postJpaRepo.save(post);
|
||||
}
|
||||
|
||||
// 게시물을 수정합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
|
||||
public Post updatePost(long postId, String uid, ParamsPost paramsPost) {
|
||||
Post post = getPost(postId);
|
||||
User user = post.getUser();
|
||||
@@ -55,6 +59,7 @@ public class BoardService {
|
||||
return postJpaRepo.save(post);
|
||||
}
|
||||
|
||||
// 게시물을 삭제합니다. 게시물 등록자와 로그인 회원정보가 틀리면 CNotOwnerException 처리합니다.
|
||||
public boolean deletePost(long postId, String uid) {
|
||||
Post post = getPost(postId);
|
||||
User user = post.getUser();
|
||||
|
||||
Reference in New Issue
Block a user