rest controller practice

This commit is contained in:
haerong22
2021-03-14 22:58:35 +09:00
parent 053d2d9d46
commit 5a8f260c75
8 changed files with 92 additions and 2 deletions

View File

@@ -39,6 +39,10 @@ public class Board {
@OneToMany(mappedBy = "board")
List<BoardLike> boardLikeList = new ArrayList<>();
@JsonIgnore
@OneToMany(mappedBy = "board")
List<BoardComment> boardCommentList = new ArrayList<>();
private String title;
private String content;

View File

@@ -0,0 +1,33 @@
package com.example.restcontroller.board.entity;
import com.example.restcontroller.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
public class BoardComment {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "FK_BOARD_COMMENT_USER_ID"))
private User user;
@ManyToOne()
@JoinColumn(foreignKey = @ForeignKey(name = "FK_BOARD_COMMENT_BOARD_ID"))
private Board board;
private String comments;
private LocalDateTime regDate;
}

View File

@@ -0,0 +1,14 @@
package com.example.restcontroller.board.repository;
import com.example.restcontroller.board.entity.BoardComment;
import com.example.restcontroller.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BoardCommentRepository extends JpaRepository<BoardComment, Long> {
List<BoardComment> findByUser(User user);
}

View File

@@ -1,6 +1,7 @@
package com.example.restcontroller.board.service;
import com.example.restcontroller.board.entity.Board;
import com.example.restcontroller.board.entity.BoardComment;
import com.example.restcontroller.board.entity.BoardReport;
import com.example.restcontroller.board.entity.BoardType;
import com.example.restcontroller.board.model.*;
@@ -44,4 +45,6 @@ public interface BoardService {
ServiceResult deleteBookmark(Long id, String email);
List<Board> postList(String email);
List<BoardComment> commentList(String email);
}

View File

@@ -28,6 +28,7 @@ public class BoardServiceImpl implements BoardService {
private final BoardReportRepository boardReportRepository;
private final BoardScrapRepository boardScrapRepository;
private final BoardBookmarkRepository boardBookmarkRepository;
private final BoardCommentRepository boardCommentRepository;
private final UserRepository userRepository;
@Transactional
@@ -363,4 +364,12 @@ public class BoardServiceImpl implements BoardService {
return boardRepository.findByUser(userEntity);
}
@Override
public List<BoardComment> commentList(String email) {
User userEntity = userRepository.findByEmail(email)
.orElseThrow(() -> new BizException("회원 정보가 존재하지 않습니다."));
return boardCommentRepository.findByUser(userEntity);
}
}

View File

@@ -5,6 +5,7 @@ import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.example.restcontroller.board.entity.Board;
import com.example.restcontroller.board.entity.BoardComment;
import com.example.restcontroller.board.service.BoardService;
import com.example.restcontroller.common.model.ResponseResult;
import com.example.restcontroller.notice.entity.Notice;
@@ -371,4 +372,18 @@ public class ApiUserController {
List<Board> list = boardService.postList(email);
return ResponseResult.success(list);
}
}
@GetMapping("/api/user/board/comment")
public ResponseEntity<?> chapter3_21(@RequestHeader("TOKEN") String token) {
String email = "";
try {
email = JWTUtils.getIssuer(token);
} catch (JWTVerificationException e) {
return ResponseResult.fail("토큰 정보가 정확하지 않습니다.");
}
List<BoardComment> list = boardService.commentList(email);
return ResponseResult.success(list);
}
}

View File

@@ -72,10 +72,15 @@ public class User {
@OneToMany(mappedBy = "user")
List<BoardBookmark> boardBookmarkList = new ArrayList<>();
@JsonIgnore
@OneToMany(mappedBy = "user")
List<BoardComment> boardCommentList = new ArrayList<>();
@JsonIgnore
@OneToMany(mappedBy = "user")
List<UserInterest> userList = new ArrayList<>();
@JsonIgnore
@OneToMany(mappedBy = "interestUser")
List<UserInterest> userInterestList = new ArrayList<>();

View File

@@ -48,4 +48,11 @@ values
(2, 1, '게시글4', '게시글 내용4', now(), 0),
(2, 2, '게시글5', '게시글 내용5', now(), 0),
(3, 1, '게시글6', '게시글 내용6', now(), 0),
(3, 3, '게시글7', '게시글 내용7', now(), 0);
(3, 3, '게시글7', '게시글 내용7', now(), 0);
insert into board_comment (comments, reg_date, board_id, user_id)
values
('게시글 1의 댓글1', now(), 1, 1),
('게시글 1의 댓글2', now(), 1, 2),
('게시글 2의 댓글1', now(), 2, 1),
('게시글 2의 댓글2', now(), 2, 3);