From a02ee65a7eb0b940b983242abdd630085fd07673 Mon Sep 17 00:00:00 2001 From: yulmam Date: Thu, 18 Jul 2024 14:39:43 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EA=B2=8C=EC=8B=9C=ED=8C=90=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 78 +++++------ .../entity/dto/RequestBoardDetailDto.java | 2 - .../board/entity/dto/RequestBoardDto.java | 14 ++ .../entity/dto/RequestBoardUpdateDto.java | 12 ++ .../board/entity/dto/RequestCommentDto.java | 4 +- .../entity/dto/ResponseBoardDetailDto.java | 21 ++- .../board/entity/dto/ResponseBoardDto.java | 7 +- .../edufocus/board/entity/vo/Board.java | 52 +++++++- .../edufocus/board/entity/vo/Comment.java | 40 +++++- .../board/repository/BoardRepository.java | 13 ++ .../board/repository/CommentRepository.java | 11 ++ .../edufocus/board/service/BoardService.java | 23 +++- .../board/service/BoardServiceImpl.java | 122 +++++++++++++++++- 13 files changed, 347 insertions(+), 52 deletions(-) delete mode 100644 backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDetailDto.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDto.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardUpdateDto.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java diff --git a/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java b/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java index 848335b..a8a4db6 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/controller/BoardController.java @@ -1,13 +1,13 @@ package com.edufocus.edufocus.board.controller; -import com.edufocus.edufocus.board.entity.dto.RequestBoardDetailDto; +import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; +import com.edufocus.edufocus.board.entity.dto.RequestBoardUpdateDto; import com.edufocus.edufocus.board.entity.dto.RequestCommentDto; -import com.edufocus.edufocus.board.entity.dto.ResponseBoardDetailDto; -import com.edufocus.edufocus.board.entity.dto.ResponseBoardDto; +import com.edufocus.edufocus.board.entity.vo.Board; import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.service.BoardService; +import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.constraints.Positive; -import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -29,15 +29,11 @@ public class BoardController { @GetMapping() public ResponseEntity searchBoards( - Pageable pageable, - @RequestParam(value = "userId", required = true) int userId + @RequestParam(value = "category", required = false, defaultValue = "announcement") String category, + @RequestParam(value = "lectureId", required = true) long lectureId, + @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo ){ - - if(boardService.checkRegistration(userId, lectureId)){ - throw new RuntimeException(); - } - - List boards = boardService.findBoards(userId, category, lectureId); + List boards = boardService.findBoards(pageNo, category, lectureId); if(boards.isEmpty()){ return ResponseEntity.noContent().build(); @@ -48,39 +44,35 @@ public class BoardController { @GetMapping(value = "/{boardId}") public ResponseEntity getBoardDetail( - @PathVariable @Positive int boardId, - @RequestParam(value = "userId", required = true) @Positive int userId + @PathVariable @Positive int boardId ){ - ResponseBoardDetailDto responseBoardDetail = boardService.findBoardDetail(userId, boardId); + Board responseBoardDetail = boardService.findBoardDetail(boardId); return ResponseEntity.ok(responseBoardDetail); } @PostMapping public ResponseEntity addBoard( - @RequestBody RequestBoardDetailDto requestBoardDetailDto, - @RequestParam(value = "userId", required = true) @Positive int userId + @RequestBody RequestBoardDto requestBoardDto, + HttpServletRequest request ){ - if(boardService.checkRegistration(userId, requestBoardDetailDto.getLectureId())){ - throw new RuntimeException(); - } - boardService.createBoard(userId, requestBoardDetailDto); + long userId = Long.parseLong(request.getHeader("Authentication")); + + boardService.createBoard(userId, requestBoardDto); return new ResponseEntity<>(HttpStatus.OK); } @PutMapping(value = "/{boardId}") public ResponseEntity updateBoard( - @PathVariable @Positive int boardId, - @RequestBody RequestBoardDetailDto requestBoardDetailDto, - @RequestParam(value = "userId", required = true) @Positive int userId + @PathVariable @Positive long boardId, + @RequestBody RequestBoardUpdateDto requestBoardUpdateDto, + HttpServletRequest request ){ - if(boardService.checkRegistration(userId, requestBoardDetailDto.getLectureId())){ - throw new RuntimeException(); - } + long userId = Long.parseLong(request.getHeader("Authentication")); - boardService.createBoard(userId, requestBoardDetailDto); + boardService.updateBoard(boardId, requestBoardUpdateDto); return new ResponseEntity<>(HttpStatus.OK); } @@ -88,12 +80,8 @@ public class BoardController { @DeleteMapping(value = "/{boardId}") public ResponseEntity deleteBoard( @PathVariable int boardId, - @RequestParam @Positive int userId + HttpServletRequest request ){ - if(boardService.checkBoardOwner(userId, boardId)){ - throw new RuntimeException(); - } - boardService.deleteBoard(boardId); return new ResponseEntity<>(HttpStatus.OK); @@ -102,8 +90,10 @@ public class BoardController { @GetMapping(value = "/comment/{boardId}") public ResponseEntity getComments( @PathVariable @Positive int boardId, - @RequestParam @Positive int userId + HttpServletRequest request ){ + long userId = Long.parseLong(request.getHeader("Authentication")); + List comments = boardService.findComments(userId, boardId); return ResponseEntity.ok(comments); @@ -112,23 +102,25 @@ public class BoardController { @PostMapping(value = "/comment/{boardId}") public ResponseEntity addComment( @PathVariable @Positive int boardId, - @RequestParam int userId, - @RequestBody RequestCommentDto requestCommentDto + @RequestParam String content, + HttpServletRequest request ){ - boardService.createComment(userId, boardId, requestCommentDto); + long userId = Long.parseLong(request.getHeader("Authentication")); + + boardService.createComment(userId, boardId, content); return new ResponseEntity<>(HttpStatus.OK); } @PutMapping(value = "/comment/{commentId}") public ResponseEntity updateComment( - @RequestParam int userId, @PathVariable @Positive int commentId, - @RequestBody RequestCommentDto requestCommentDto + @RequestParam String content, + HttpServletRequest request ){ - boardService.checkCommentOwner(userId, commentId); + long userId = Long.parseLong(request.getHeader("Authentication")); - boardService.updateComment(commentId, requestCommentDto); + boardService.updateComment(commentId, content); return new ResponseEntity<>(HttpStatus.OK); } @@ -136,9 +128,9 @@ public class BoardController { @DeleteMapping(value = "/comment/{commentId}") public ResponseEntity deleteComment( @PathVariable @Positive int commentId, - @RequestParam int userId + HttpServletRequest request ){ - boardService.checkCommentOwner(userId, commentId); + long userId = Long.parseLong(request.getHeader("Authentication")); boardService.deleteComment(commentId); diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDetailDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDetailDto.java deleted file mode 100644 index a88c1e0..0000000 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDetailDto.java +++ /dev/null @@ -1,2 +0,0 @@ -package com.edufocus.edufocus.board.entity.dto;public class RequestBoartDetailDto { -} diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDto.java new file mode 100644 index 0000000..ca59a75 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardDto.java @@ -0,0 +1,14 @@ +package com.edufocus.edufocus.board.entity.dto; + + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RequestBoardDto { + private long lectureId; + private String title; + private String category; + private String content; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardUpdateDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardUpdateDto.java new file mode 100644 index 0000000..58ff7a7 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestBoardUpdateDto.java @@ -0,0 +1,12 @@ +package com.edufocus.edufocus.board.entity.dto; + + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RequestBoardUpdateDto { + private String title; + private String content; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestCommentDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestCommentDto.java index 9a4ffcf..6578cad 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestCommentDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/RequestCommentDto.java @@ -1,2 +1,4 @@ -package com.edufocus.edufocus.board.entity.dto;public class RequestCommentDto { +package com.edufocus.edufocus.board.entity.dto; + +public class RequestCommentDto { } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDetailDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDetailDto.java index 383839a..182a8cc 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDetailDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDetailDto.java @@ -1,2 +1,21 @@ -package com.edufocus.edufocus.board.entity.dto;public class ResponseBoardDetailDto { +package com.edufocus.edufocus.board.entity.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalTime; + +@Builder +@Getter +@Setter +@AllArgsConstructor +public class ResponseBoardDetailDto { + private int id; + private String name; + private String content; + private int viewCount; + private LocalTime createdAt; + private LocalTime modifiedAt; } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDto.java index cb700e9..3d34b1a 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardDto.java @@ -1,2 +1,7 @@ -package com.edufocus.edufocus.board.entity.dto;public class ResponseBoardDto { +package com.edufocus.edufocus.board.entity.dto; + +public class ResponseBoardDto { + private int id; + private String name; + private String title; } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Board.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Board.java index aca1d4d..7f8de48 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Board.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Board.java @@ -1,2 +1,52 @@ -package com.edufocus.edufocus.board.entity.vo;public class Board { +package com.edufocus.edufocus.board.entity.vo; + + +import com.edufocus.edufocus.lecture.entity.Lecture; +import com.edufocus.edufocus.user.model.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.hibernate.annotations.CreationTimestamp; + +import java.time.LocalDate; +import java.util.Date; + +@Entity +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Setter +public class Board { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String title; + + @Column(nullable = false) + private String category; + + @Column(nullable = false) + private String content; + + @Column(nullable = true) + private int viewCount; + + @CreationTimestamp + LocalDate createdAt; + + @CreationTimestamp + LocalDate modifiedAt; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "user_id") + private User user; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "lecture_id") + private Lecture lecture; } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Comment.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Comment.java index 92d3c3e..c3059c9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Comment.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/vo/Comment.java @@ -1,2 +1,40 @@ -package com.edufocus.edufocus.board.entity.vo;public class Comment { +package com.edufocus.edufocus.board.entity.vo; + + +import com.edufocus.edufocus.user.model.entity.User; +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.time.LocalTime; + +@Entity +@Builder +@NoArgsConstructor +@AllArgsConstructor +@Setter +public class Comment { + @Id + @GeneratedValue + private long id; + @Column + private String content; + + @Column + private LocalTime createdAt; + + @Column + private LocalTime updatedAt; + + @ManyToOne + @JoinColumn(name = "user_id") + User user; + + @ManyToOne + @JoinColumn(name = "board_id") + Board board; + + } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java b/backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java new file mode 100644 index 0000000..2cdb7c1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java @@ -0,0 +1,13 @@ +package com.edufocus.edufocus.board.repository; + +import com.edufocus.edufocus.board.entity.vo.Board; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface BoardRepository extends JpaRepository { + Page findByLectureIdAndCategory(Long lectureId, String category, Pageable pageable); + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java b/backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java new file mode 100644 index 0000000..9002ab8 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java @@ -0,0 +1,11 @@ +package com.edufocus.edufocus.board.repository; + +import com.edufocus.edufocus.board.entity.vo.Comment; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface CommentRepository extends JpaRepository { + List findByBoardId(long boardId); + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardService.java b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardService.java index fad3c9d..ac20175 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardService.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardService.java @@ -1,2 +1,23 @@ -package com.edufocus.edufocus.board.service;public interface BoardService { +package com.edufocus.edufocus.board.service; + +import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; +import com.edufocus.edufocus.board.entity.dto.RequestBoardUpdateDto; +import com.edufocus.edufocus.board.entity.dto.RequestCommentDto; +import com.edufocus.edufocus.board.entity.vo.Board; +import com.edufocus.edufocus.board.entity.vo.Comment; + +import java.util.List; + +public interface BoardService { + + public List findBoards(int pageNo, String category, long lectureId); + public Board findBoardDetail(long boardId); + public void createBoard(long userId, RequestBoardDto requestBoardDto); + public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto); + public void deleteBoard(long boardId); + public List findComments(long userId, long boardId); + public void createComment(long userId, long boardId, String content); + public void updateComment(long commentId, String content); + public void deleteComment(long commentId); + } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java index 7292811..308bd57 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/service/BoardServiceImpl.java @@ -1,2 +1,122 @@ -package com.edufocus.edufocus.board.service;public class BoardServiceImpl { +package com.edufocus.edufocus.board.service; + + +import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; +import com.edufocus.edufocus.board.entity.dto.RequestBoardUpdateDto; +import com.edufocus.edufocus.board.entity.dto.RequestCommentDto; +import com.edufocus.edufocus.board.entity.vo.Board; +import com.edufocus.edufocus.board.entity.vo.Comment; +import com.edufocus.edufocus.board.repository.BoardRepository; +import com.edufocus.edufocus.board.repository.CommentRepository; +import com.edufocus.edufocus.lecture.entity.Lecture; +import com.edufocus.edufocus.lecture.repository.LectureRepository; +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.repository.UserRepository; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class BoardServiceImpl implements BoardService { + + private static final int PAGE_SIZE = 10; + + private final UserRepository userRepository; + private final LectureRepository lectureRepository; + private final BoardRepository boardRepository; + private final CommentRepository commentRepository; + + public BoardServiceImpl(BoardRepository boardRepository, CommentRepository commentRepository, UserRepository userRepository, LectureRepository lectureRepository){ + this.boardRepository = boardRepository; + this.commentRepository = commentRepository; + this.userRepository = userRepository; + this.lectureRepository = lectureRepository; + } + + + @Override + public List findBoards(int pageNo, String category, long lectureId) { + Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE); + Page boards = boardRepository.findByLectureIdAndCategory(lectureId, category, pageable); + return boards.getContent(); + } + + @Override + public Board findBoardDetail(long boardId) { + Board board = boardRepository.findById(boardId).get(); + return board; + } + + @Override + public void createBoard(long userId, RequestBoardDto requestBoardDto) { + User user = userRepository.findById(userId).get(); + Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).get(); + + Board board = Board.builder() + .title(requestBoardDto.getTitle()) + .category(requestBoardDto.getCategory()) + .content(requestBoardDto.getCategory()) + .user(user) + .lecture(lecture) + .build(); + + boardRepository.save(board); + } + + @Override + public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) { + Board board = boardRepository.findById(boardId).get(); + + board.setTitle(requestBoardUpdateDto.getTitle()); + board.setContent(requestBoardUpdateDto.getContent()); + + boardRepository.save(board); + } + + + @Override + public void deleteBoard(long boardId) { + Board board = boardRepository.findById(boardId).get(); + + boardRepository.delete(board); + } + + @Override + public List findComments(long userId, long boardId) { + return commentRepository.findByBoardId(boardId); + } + + @Override + public void createComment(long userId, long boardId, String content) { + User user = userRepository.findById(userId).get(); + Board board = boardRepository.findById(boardId).get(); + + Comment comment = Comment.builder() + .content(content) + .board(board) + .user(user) + .build(); + + commentRepository.save(comment); + } + + + + @Override + public void updateComment(long commentId, String content) { + Comment comment = commentRepository.findById(commentId).get(); + comment.setContent(content); + commentRepository.save(comment); + } + + @Override + public void deleteComment(long commentId) { + Comment comment = commentRepository.findById(commentId).get(); + commentRepository.delete(comment); + } + + }