refactor : 서비스 리턴값 변경
This commit is contained in:
parent
828df9c91c
commit
cebabf77c3
@ -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<Board> boards = boardService.findBoards(pageNo, category, lectureId);
|
||||
List<ResponseBoardSummaryDto> 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<ResponseCommentDto> comments = boardService.findComments(boardId);
|
||||
|
||||
List<Comment> 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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<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();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<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);
|
||||
|
||||
}
|
||||
|
@ -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<Comment, Long> {
|
||||
|
||||
@Query("select c from Comment c join fetch c.user where c.board.id =:boardId")
|
||||
List<Comment> findByBoardId(long boardId);
|
||||
|
||||
}
|
||||
|
@ -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<Board> findBoards(int pageNo, String category, long lectureId);
|
||||
public Board findBoardDetail(long boardId);
|
||||
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 deleteBoard(long boardId);
|
||||
public List<Comment> 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<ResponseCommentDto> findComments(long boardId);
|
||||
|
||||
public void updateComment(long commentId, RequestCommentDto requestCommentDto);
|
||||
|
||||
public void deleteComment(long commentId);
|
||||
|
||||
}
|
||||
|
@ -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<Board> findBoards(int pageNo, String category, long lectureId) {
|
||||
@Transactional
|
||||
public List<ResponseBoardSummaryDto> findBoards(int pageNo, String category, long lectureId) {
|
||||
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
|
||||
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<Comment> findComments(long userId, long boardId) {
|
||||
return commentRepository.findByBoardId(boardId);
|
||||
@Transactional
|
||||
public List<ResponseCommentDto> 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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user