refactor : 서비스 리턴값 변경

This commit is contained in:
yulmam 2024-07-19 13:07:22 +09:00
parent 828df9c91c
commit cebabf77c3
12 changed files with 172 additions and 93 deletions

View File

@ -1,11 +1,10 @@
package com.edufocus.edufocus.board.controller; package com.edufocus.edufocus.board.controller;
import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; import com.edufocus.edufocus.board.entity.dto.*;
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.Board;
import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.entity.vo.Comment;
import com.edufocus.edufocus.board.service.BoardService; import com.edufocus.edufocus.board.service.BoardService;
import com.edufocus.edufocus.user.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.Positive; import jakarta.validation.constraints.Positive;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -18,13 +17,16 @@ import java.util.List;
/** /**
* @author Haneol Kim * @author Haneol Kim
*/ */
@RestController("/board") @RestController
@RequestMapping("/board")
public class BoardController { public class BoardController {
private final JWTUtil jwtUtil;
private final BoardService boardService; private final BoardService boardService;
public BoardController(BoardService boardService){ public BoardController(BoardService boardService, JWTUtil jwtUtil){
this.boardService = boardService; this.boardService = boardService;
this.jwtUtil = jwtUtil;
} }
@GetMapping() @GetMapping()
@ -33,22 +35,22 @@ public class BoardController {
@RequestParam(value = "lectureId", required = true) long lectureId, @RequestParam(value = "lectureId", required = true) long lectureId,
@RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo @RequestParam(value = "pageNo", required = false, defaultValue = "0") int pageNo
){ ){
List<Board> boards = boardService.findBoards(pageNo, category, lectureId); List<ResponseBoardSummaryDto> boardSummaries = boardService.findBoards(pageNo, category, lectureId);
if(boards.isEmpty()){ if(boardSummaries.isEmpty()){
return ResponseEntity.noContent().build(); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} }
return ResponseEntity.ok(boards); return new ResponseEntity<>(boardSummaries, HttpStatus.OK);
} }
@GetMapping(value = "/{boardId}") @GetMapping(value = "/{boardId}")
public ResponseEntity<?> getBoardDetail( 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 @PostMapping
@ -56,8 +58,8 @@ public class BoardController {
@RequestBody RequestBoardDto requestBoardDto, @RequestBody RequestBoardDto requestBoardDto,
HttpServletRequest request HttpServletRequest request
){ ){
String token = request.getHeader("Authorization");
long userId = Long.parseLong(request.getHeader("Authentication")); long userId = Long.parseLong(jwtUtil.getUserId(token));
boardService.createBoard(userId, requestBoardDto); boardService.createBoard(userId, requestBoardDto);
@ -66,12 +68,9 @@ public class BoardController {
@PutMapping(value = "/{boardId}") @PutMapping(value = "/{boardId}")
public ResponseEntity<?> updateBoard( public ResponseEntity<?> updateBoard(
@PathVariable @Positive long boardId, @PathVariable long boardId,
@RequestBody RequestBoardUpdateDto requestBoardUpdateDto, @RequestBody RequestBoardUpdateDto requestBoardUpdateDto
HttpServletRequest request
){ ){
long userId = Long.parseLong(request.getHeader("Authentication"));
boardService.updateBoard(boardId, requestBoardUpdateDto); boardService.updateBoard(boardId, requestBoardUpdateDto);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
@ -89,49 +88,41 @@ public class BoardController {
@GetMapping(value = "/comment/{boardId}") @GetMapping(value = "/comment/{boardId}")
public ResponseEntity<?> getComments( public ResponseEntity<?> getComments(
@PathVariable @Positive int boardId, @PathVariable int boardId
HttpServletRequest request
){ ){
long userId = Long.parseLong(request.getHeader("Authentication")); List<ResponseCommentDto> comments = boardService.findComments(boardId);
List<Comment> comments = boardService.findComments(userId, boardId); return new ResponseEntity<>(comments, HttpStatus.OK);
return ResponseEntity.ok(comments);
} }
@PostMapping(value = "/comment/{boardId}") @PostMapping(value = "/comment/{boardId}")
public ResponseEntity<?> addComment( public ResponseEntity<?> addComment(
@PathVariable @Positive int boardId, @PathVariable int boardId,
@RequestParam String content, @RequestBody RequestCommentDto requestCommentDto,
HttpServletRequest request 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); return new ResponseEntity<>(HttpStatus.OK);
} }
@PutMapping(value = "/comment/{commentId}") @PutMapping(value = "/comment/{commentId}")
public ResponseEntity<?> updateComment( public ResponseEntity<?> updateComment(
@PathVariable @Positive int commentId, @PathVariable int commentId,
@RequestParam String content, @RequestBody RequestCommentDto requestCommentDto
HttpServletRequest request
){ ){
long userId = Long.parseLong(request.getHeader("Authentication")); boardService.updateComment(commentId, requestCommentDto);
boardService.updateComment(commentId, content);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);
} }
@DeleteMapping(value = "/comment/{commentId}") @DeleteMapping(value = "/comment/{commentId}")
public ResponseEntity<?> deleteComment( public ResponseEntity<?> deleteComment(
@PathVariable @Positive int commentId, @PathVariable int commentId
HttpServletRequest request
){ ){
long userId = Long.parseLong(request.getHeader("Authentication"));
boardService.deleteComment(commentId); boardService.deleteComment(commentId);
return new ResponseEntity<>(HttpStatus.OK); return new ResponseEntity<>(HttpStatus.OK);

View File

@ -1,6 +1,7 @@
package com.edufocus.edufocus.board.entity.dto; package com.edufocus.edufocus.board.entity.dto;
import com.edufocus.edufocus.board.entity.vo.Board;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;

View File

@ -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;
} }

View File

@ -12,8 +12,9 @@ import java.time.LocalTime;
@Setter @Setter
@AllArgsConstructor @AllArgsConstructor
public class ResponseBoardDetailDto { public class ResponseBoardDetailDto {
private int id; private long id;
private String name; private String name;
private String title;
private String content; private String content;
private int viewCount; private int viewCount;
private LocalTime createdAt; private LocalTime createdAt;

View File

@ -1,7 +1,15 @@
package com.edufocus.edufocus.board.entity.dto; 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 name;
private String title; private String title;
} }

View File

@ -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;
} }

View File

@ -1,23 +1,24 @@
package com.edufocus.edufocus.board.entity.vo; 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.lecture.entity.Lecture;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.*;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.CreationTimestamp;
import java.time.LocalDate; import java.time.LocalTime;
import java.util.Date; import java.util.List;
@Entity @Entity
@Builder @Builder
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Setter @Setter
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Board { public class Board {
@Id @Id
@ -37,10 +38,10 @@ public class Board {
private int viewCount; private int viewCount;
@CreationTimestamp @CreationTimestamp
LocalDate createdAt; LocalTime createdAt;
@CreationTimestamp @CreationTimestamp
LocalDate modifiedAt; LocalTime modifiedAt;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
@ -49,4 +50,28 @@ public class Board {
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "lecture_id") @JoinColumn(name = "lecture_id")
private Lecture lecture; private Lecture lecture;
@OneToMany(mappedBy = "board")
private List<Comment> 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();
}
} }

View File

@ -1,7 +1,9 @@
package com.edufocus.edufocus.board.entity.vo; 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.edufocus.edufocus.user.model.entity.User;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
@ -15,6 +17,7 @@ import java.time.LocalTime;
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
@Setter @Setter
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Comment { public class Comment {
@Id @Id
@GeneratedValue @GeneratedValue
@ -26,7 +29,7 @@ public class Comment {
private LocalTime createdAt; private LocalTime createdAt;
@Column @Column
private LocalTime updatedAt; private LocalTime modifiedAt;
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
@ -36,5 +39,14 @@ public class Comment {
@JoinColumn(name = "board_id") @JoinColumn(name = "board_id")
Board board; Board board;
public ResponseCommentDto makeCommentDto() {
return ResponseCommentDto.builder()
.id(id)
.name(user.getEmail())
.content(content)
.createAt(createdAt)
.modifiedAt(modifiedAt)
.build();
}
} }

View File

@ -4,10 +4,12 @@ import com.edufocus.edufocus.board.entity.vo.Board;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface BoardRepository extends JpaRepository<Board, Long> { public interface BoardRepository extends JpaRepository<Board, Long> {
@Query("select b from Board b join fetch b.user where b.category =:category and b.lecture.id=:lectureId")
Page<Board> findByLectureIdAndCategory(Long lectureId, String category, Pageable pageable); Page<Board> findByLectureIdAndCategory(Long lectureId, String category, Pageable pageable);
} }

View File

@ -2,10 +2,13 @@ package com.edufocus.edufocus.board.repository;
import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.entity.vo.Comment;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;
public interface CommentRepository extends JpaRepository<Comment, Long> { public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query("select c from Comment c join fetch c.user where c.board.id =:boardId")
List<Comment> findByBoardId(long boardId); List<Comment> findByBoardId(long boardId);
} }

View File

@ -1,23 +1,27 @@
package com.edufocus.edufocus.board.service; package com.edufocus.edufocus.board.service;
import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; import com.edufocus.edufocus.board.entity.dto.*;
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; import java.util.List;
public interface BoardService { public interface BoardService {
public List<Board> findBoards(int pageNo, String category, long lectureId);
public Board findBoardDetail(long boardId);
public void createBoard(long userId, RequestBoardDto requestBoardDto); public void createBoard(long userId, RequestBoardDto requestBoardDto);
public List<ResponseBoardSummaryDto> findBoards(int pageNo, String category, long lectureId);
public ResponseBoardDetailDto findBoardDetail(long boardId);
public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto); public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto);
public void deleteBoard(long boardId); public void deleteBoard(long boardId);
public List<Comment> findComments(long userId, long boardId);
public void createComment(long userId, long boardId, String content); public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto);
public void updateComment(long commentId, String content);
public List<ResponseCommentDto> findComments(long boardId);
public void updateComment(long commentId, RequestCommentDto requestCommentDto);
public void deleteComment(long commentId); public void deleteComment(long commentId);
} }

View File

@ -1,9 +1,7 @@
package com.edufocus.edufocus.board.service; package com.edufocus.edufocus.board.service;
import com.edufocus.edufocus.board.entity.dto.RequestBoardDto; import com.edufocus.edufocus.board.entity.dto.*;
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.Board;
import com.edufocus.edufocus.board.entity.vo.Comment; import com.edufocus.edufocus.board.entity.vo.Comment;
import com.edufocus.edufocus.board.repository.BoardRepository; 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.lecture.repository.LectureRepository;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository; 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.PageRequest;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Service @Service
public class BoardServiceImpl implements BoardService { public class BoardServiceImpl implements BoardService {
private static final int PAGE_SIZE = 10; private static final int PAGE_SIZE = 10;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
private final BoardRepository boardRepository; private final BoardRepository boardRepository;
private final CommentRepository commentRepository; private final CommentRepository commentRepository;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
public BoardServiceImpl(BoardRepository boardRepository, CommentRepository commentRepository, UserRepository userRepository, LectureRepository lectureRepository){ public BoardServiceImpl(BoardRepository boardRepository, CommentRepository commentRepository, UserRepository userRepository, LectureRepository lectureRepository){
this.boardRepository = boardRepository; this.boardRepository = boardRepository;
@ -37,28 +36,32 @@ public class BoardServiceImpl implements BoardService {
} }
@Override @Transactional
public List<Board> findBoards(int pageNo, String category, long lectureId) { public List<ResponseBoardSummaryDto> findBoards(int pageNo, String category, long lectureId) {
Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE); Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE);
Page<Board> boards = boardRepository.findByLectureIdAndCategory(lectureId, category, pageable);
return boards.getContent(); List<Board> boards = boardRepository.findByLectureIdAndCategory(lectureId, category, pageable).getContent();
return boards.stream().map(Board::makeSummaryDto)
.collect(Collectors.toList());
} }
@Override @Transactional
public Board findBoardDetail(long boardId) { public ResponseBoardDetailDto findBoardDetail(long boardId) {
Board board = boardRepository.findById(boardId).get(); return boardRepository.findById(boardId)
return board; .orElseThrow()
.makeDetailDto();
} }
@Override @Transactional
public void createBoard(long userId, RequestBoardDto requestBoardDto) { 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(); Lecture lecture = lectureRepository.findById(requestBoardDto.getLectureId()).get();
Board board = Board.builder() Board board = Board.builder()
.title(requestBoardDto.getTitle()) .title(requestBoardDto.getTitle())
.category(requestBoardDto.getCategory()) .category(requestBoardDto.getCategory())
.content(requestBoardDto.getCategory()) .content(requestBoardDto.getContent())
.user(user) .user(user)
.lecture(lecture) .lecture(lecture)
.build(); .build();
@ -66,7 +69,7 @@ public class BoardServiceImpl implements BoardService {
boardRepository.save(board); boardRepository.save(board);
} }
@Override @Transactional
public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) { public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) {
Board board = boardRepository.findById(boardId).get(); Board board = boardRepository.findById(boardId).get();
@ -77,25 +80,27 @@ public class BoardServiceImpl implements BoardService {
} }
@Override @Transactional
public void deleteBoard(long boardId) { public void deleteBoard(long boardId) {
Board board = boardRepository.findById(boardId).get(); Board board = boardRepository.findById(boardId).get();
boardRepository.delete(board); boardRepository.delete(board);
} }
@Override @Transactional
public List<Comment> findComments(long userId, long boardId) { public List<ResponseCommentDto> findComments(long boardId) {
return commentRepository.findByBoardId(boardId); return commentRepository.findByBoardId(boardId).stream()
.map(Comment::makeCommentDto)
.collect(Collectors.toList());
} }
@Override @Transactional
public void createComment(long userId, long boardId, String content) { public void createComment(long userId, long boardId, RequestCommentDto requestCommentDto) {
User user = userRepository.findById(userId).get(); User user = userRepository.findById(userId).get();
Board board = boardRepository.findById(boardId).get(); Board board = boardRepository.findById(boardId).get();
Comment comment = Comment.builder() Comment comment = Comment.builder()
.content(content) .content(requestCommentDto.getContent())
.board(board) .board(board)
.user(user) .user(user)
.build(); .build();
@ -105,16 +110,19 @@ public class BoardServiceImpl implements BoardService {
@Override @Transactional
public void updateComment(long commentId, String content) { public void updateComment(long commentId, RequestCommentDto requestCommentDto) {
Comment comment = commentRepository.findById(commentId).get(); Comment comment = commentRepository.findById(commentId).get();
comment.setContent(content);
comment.setContent(requestCommentDto.getContent());
commentRepository.save(comment); commentRepository.save(comment);
} }
@Override @Transactional
public void deleteComment(long commentId) { public void deleteComment(long commentId) {
Comment comment = commentRepository.findById(commentId).get(); Comment comment = commentRepository.findById(commentId).get();
commentRepository.delete(comment); commentRepository.delete(comment);
} }