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 a8a4db6..d25d940 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,11 +1,10 @@ package com.edufocus.edufocus.board.controller; -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.*; import com.edufocus.edufocus.board.entity.vo.Board; import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.service.BoardService; +import com.edufocus.edufocus.user.util.JWTUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.constraints.Positive; import org.springframework.http.HttpStatus; @@ -18,13 +17,16 @@ import java.util.List; /** * @author Haneol Kim */ -@RestController("/board") +@RestController +@RequestMapping("/board") public class BoardController { + private final JWTUtil jwtUtil; private final BoardService boardService; - public BoardController(BoardService boardService){ + public BoardController(BoardService boardService, JWTUtil jwtUtil){ this.boardService = boardService; + this.jwtUtil = jwtUtil; } @GetMapping() @@ -33,22 +35,22 @@ public class BoardController { @RequestParam(value = "lectureId", required = true) long lectureId, @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo ){ - List boards = boardService.findBoards(pageNo, category, lectureId); + List boardSummaries = boardService.findBoards(pageNo, category, lectureId); - if(boards.isEmpty()){ - return ResponseEntity.noContent().build(); + if(boardSummaries.isEmpty()){ + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } - return ResponseEntity.ok(boards); + return new ResponseEntity<>(boardSummaries, HttpStatus.OK); } @GetMapping(value = "/{boardId}") public ResponseEntity getBoardDetail( - @PathVariable @Positive int boardId + @PathVariable int boardId ){ - Board responseBoardDetail = boardService.findBoardDetail(boardId); + ResponseBoardDetailDto responseBoardDetailDto = boardService.findBoardDetail(boardId); - return ResponseEntity.ok(responseBoardDetail); + return new ResponseEntity<>(responseBoardDetailDto, HttpStatus.OK); } @PostMapping @@ -56,8 +58,8 @@ public class BoardController { @RequestBody RequestBoardDto requestBoardDto, HttpServletRequest request ){ - - long userId = Long.parseLong(request.getHeader("Authentication")); + String token = request.getHeader("Authorization"); + long userId = Long.parseLong(jwtUtil.getUserId(token)); boardService.createBoard(userId, requestBoardDto); @@ -66,12 +68,9 @@ public class BoardController { @PutMapping(value = "/{boardId}") public ResponseEntity updateBoard( - @PathVariable @Positive long boardId, - @RequestBody RequestBoardUpdateDto requestBoardUpdateDto, - HttpServletRequest request + @PathVariable long boardId, + @RequestBody RequestBoardUpdateDto requestBoardUpdateDto ){ - long userId = Long.parseLong(request.getHeader("Authentication")); - boardService.updateBoard(boardId, requestBoardUpdateDto); return new ResponseEntity<>(HttpStatus.OK); @@ -89,49 +88,41 @@ public class BoardController { @GetMapping(value = "/comment/{boardId}") public ResponseEntity getComments( - @PathVariable @Positive int boardId, - HttpServletRequest request + @PathVariable int boardId ){ - long userId = Long.parseLong(request.getHeader("Authentication")); + List comments = boardService.findComments(boardId); - List comments = boardService.findComments(userId, boardId); - - return ResponseEntity.ok(comments); + return new ResponseEntity<>(comments, HttpStatus.OK); } @PostMapping(value = "/comment/{boardId}") public ResponseEntity addComment( - @PathVariable @Positive int boardId, - @RequestParam String content, + @PathVariable int boardId, + @RequestBody RequestCommentDto requestCommentDto, HttpServletRequest request ){ - long userId = Long.parseLong(request.getHeader("Authentication")); + String token = request.getHeader("Authorization"); + long userId = Long.parseLong(jwtUtil.getUserId(token)); - boardService.createComment(userId, boardId, content); + boardService.createComment(userId, boardId, requestCommentDto); return new ResponseEntity<>(HttpStatus.OK); } @PutMapping(value = "/comment/{commentId}") public ResponseEntity updateComment( - @PathVariable @Positive int commentId, - @RequestParam String content, - HttpServletRequest request + @PathVariable int commentId, + @RequestBody RequestCommentDto requestCommentDto ){ - long userId = Long.parseLong(request.getHeader("Authentication")); - - boardService.updateComment(commentId, content); + boardService.updateComment(commentId, requestCommentDto); return new ResponseEntity<>(HttpStatus.OK); } @DeleteMapping(value = "/comment/{commentId}") public ResponseEntity deleteComment( - @PathVariable @Positive int commentId, - HttpServletRequest request + @PathVariable int commentId ){ - long userId = Long.parseLong(request.getHeader("Authentication")); - boardService.deleteComment(commentId); return new ResponseEntity<>(HttpStatus.OK); 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 index ca59a75..478017a 100644 --- 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 @@ -1,6 +1,7 @@ package com.edufocus.edufocus.board.entity.dto; +import com.edufocus.edufocus.board.entity.vo.Board; import lombok.Getter; import lombok.Setter; 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..4dfbd0b 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,10 @@ -package com.edufocus.edufocus.board.entity.dto;public class RequestCommentDto { +package com.edufocus.edufocus.board.entity.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class RequestCommentDto { + private String content; } 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 182a8cc..8cf6bc7 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 @@ -12,8 +12,9 @@ import java.time.LocalTime; @Setter @AllArgsConstructor public class ResponseBoardDetailDto { - private int id; + private long id; private String name; + private String title; private String content; private int viewCount; private LocalTime createdAt; diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardSummaryDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardSummaryDto.java index 3d34b1a..d3e72a5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardSummaryDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseBoardSummaryDto.java @@ -1,7 +1,15 @@ package com.edufocus.edufocus.board.entity.dto; -public class ResponseBoardDto { - private int id; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Builder +@Getter +@Setter +public class ResponseBoardSummaryDto { + private long id; private String name; private String title; } diff --git a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseCommentDto.java b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseCommentDto.java index 8726e1c..92156ce 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseCommentDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/entity/dto/ResponseCommentDto.java @@ -1,2 +1,18 @@ -package com.edufocus.edufocus.board.entity.dto;public class ResponseCommentDto { +package com.edufocus.edufocus.board.entity.dto; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +import java.time.LocalTime; + +@Builder +@Getter +@Setter +public class ResponseCommentDto { + private long id; + private String name; + private String content; + private LocalTime createAt; + private LocalTime modifiedAt; } 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 7f8de48..6272a17 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,23 +1,24 @@ package com.edufocus.edufocus.board.entity.vo; +import com.edufocus.edufocus.board.entity.dto.ResponseBoardDetailDto; +import com.edufocus.edufocus.board.entity.dto.ResponseBoardSummaryDto; import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.user.model.entity.User; +import com.fasterxml.jackson.annotation.JsonAutoDetect; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.NoArgsConstructor; -import lombok.Setter; +import lombok.*; import org.hibernate.annotations.CreationTimestamp; -import java.time.LocalDate; -import java.util.Date; +import java.time.LocalTime; +import java.util.List; @Entity @Builder @NoArgsConstructor @AllArgsConstructor @Setter +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) public class Board { @Id @@ -37,10 +38,10 @@ public class Board { private int viewCount; @CreationTimestamp - LocalDate createdAt; + LocalTime createdAt; @CreationTimestamp - LocalDate modifiedAt; + LocalTime modifiedAt; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") @@ -49,4 +50,28 @@ public class Board { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "lecture_id") private Lecture lecture; + + + @OneToMany(mappedBy = "board") + private List comments; + + public ResponseBoardSummaryDto makeSummaryDto(){ + return ResponseBoardSummaryDto.builder() + .id(id) + .title(title) + .name(user.getUserId()) + .build(); + } + + public ResponseBoardDetailDto makeDetailDto(){ + return ResponseBoardDetailDto.builder() + .id(id) + .name(user.getEmail()) + .title(title) + .content(content) + .viewCount(viewCount) + .createdAt(createdAt) + .modifiedAt(modifiedAt) + .build(); + } } 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 c3059c9..90c7ebf 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,7 +1,9 @@ package com.edufocus.edufocus.board.entity.vo; +import com.edufocus.edufocus.board.entity.dto.ResponseCommentDto; import com.edufocus.edufocus.user.model.entity.User; +import com.fasterxml.jackson.annotation.JsonAutoDetect; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; @@ -15,6 +17,7 @@ import java.time.LocalTime; @NoArgsConstructor @AllArgsConstructor @Setter +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) public class Comment { @Id @GeneratedValue @@ -26,7 +29,7 @@ public class Comment { private LocalTime createdAt; @Column - private LocalTime updatedAt; + private LocalTime modifiedAt; @ManyToOne @JoinColumn(name = "user_id") @@ -36,5 +39,14 @@ public class Comment { @JoinColumn(name = "board_id") Board board; + public ResponseCommentDto makeCommentDto() { + return ResponseCommentDto.builder() + .id(id) + .name(user.getEmail()) + .content(content) + .createAt(createdAt) + .modifiedAt(modifiedAt) + .build(); + } } 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 index 2cdb7c1..beff8b3 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/repository/BoardRepository.java @@ -4,10 +4,12 @@ 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 org.springframework.data.jpa.repository.Query; -import java.util.List; public interface BoardRepository extends JpaRepository { + + @Query("select b from Board b join fetch b.user where b.category =:category and b.lecture.id=:lectureId") 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 index 9002ab8..c877fd1 100644 --- a/backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/board/repository/CommentRepository.java @@ -2,10 +2,13 @@ package com.edufocus.edufocus.board.repository; import com.edufocus.edufocus.board.entity.vo.Comment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import java.util.List; public interface CommentRepository extends JpaRepository { + + @Query("select c from Comment c join fetch c.user where c.board.id =:boardId") 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 ac20175..f0d25f8 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,23 +1,27 @@ 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.entity.dto.*; 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 List findBoards(int pageNo, String category, long lectureId); + + public ResponseBoardDetailDto findBoardDetail(long boardId); + 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 createComment(long userId, long boardId, RequestCommentDto requestCommentDto); + + public List findComments(long boardId); + + public void updateComment(long commentId, RequestCommentDto requestCommentDto); + 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 308bd57..29c5006 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,9 +1,7 @@ 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.dto.*; import com.edufocus.edufocus.board.entity.vo.Board; import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.repository.BoardRepository; @@ -12,22 +10,23 @@ 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 org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; @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; + private final UserRepository userRepository; + private final LectureRepository lectureRepository; public BoardServiceImpl(BoardRepository boardRepository, CommentRepository commentRepository, UserRepository userRepository, LectureRepository lectureRepository){ this.boardRepository = boardRepository; @@ -37,28 +36,32 @@ public class BoardServiceImpl implements BoardService { } - @Override - public List findBoards(int pageNo, String category, long lectureId) { + @Transactional + 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(); + + List boards = boardRepository.findByLectureIdAndCategory(lectureId, category, pageable).getContent(); + + return boards.stream().map(Board::makeSummaryDto) + .collect(Collectors.toList()); } - @Override - public Board findBoardDetail(long boardId) { - Board board = boardRepository.findById(boardId).get(); - return board; + @Transactional + public ResponseBoardDetailDto findBoardDetail(long boardId) { + return boardRepository.findById(boardId) + .orElseThrow() + .makeDetailDto(); } - @Override + @Transactional public void createBoard(long userId, RequestBoardDto requestBoardDto) { - User user = userRepository.findById(userId).get(); + User user = userRepository.findById(userId).orElseThrow(); Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).get(); Board board = Board.builder() .title(requestBoardDto.getTitle()) .category(requestBoardDto.getCategory()) - .content(requestBoardDto.getCategory()) + .content(requestBoardDto.getContent()) .user(user) .lecture(lecture) .build(); @@ -66,7 +69,7 @@ public class BoardServiceImpl implements BoardService { boardRepository.save(board); } - @Override + @Transactional public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) { Board board = boardRepository.findById(boardId).get(); @@ -77,25 +80,27 @@ public class BoardServiceImpl implements BoardService { } - @Override + @Transactional 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); + @Transactional + public List findComments(long boardId) { + return commentRepository.findByBoardId(boardId).stream() + .map(Comment::makeCommentDto) + .collect(Collectors.toList()); } - @Override - public void createComment(long userId, long boardId, String content) { + @Transactional + public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto) { User user = userRepository.findById(userId).get(); Board board = boardRepository.findById(boardId).get(); Comment comment = Comment.builder() - .content(content) + .content(requestCommentDto.getContent()) .board(board) .user(user) .build(); @@ -105,16 +110,19 @@ public class BoardServiceImpl implements BoardService { - @Override - public void updateComment(long commentId, String content) { + @Transactional + public void updateComment(long commentId, RequestCommentDto requestCommentDto) { Comment comment = commentRepository.findById(commentId).get(); - comment.setContent(content); + + comment.setContent(requestCommentDto.getContent()); + commentRepository.save(comment); } - @Override + @Transactional public void deleteComment(long commentId) { Comment comment = commentRepository.findById(commentId).get(); + commentRepository.delete(comment); }