commit
388ad8921e
@ -0,0 +1,130 @@
|
||||
package com.edufocus.edufocus.board.controller;
|
||||
|
||||
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;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author Haneol Kim
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/board")
|
||||
public class BoardController {
|
||||
|
||||
private final JWTUtil jwtUtil;
|
||||
private final BoardService boardService;
|
||||
|
||||
public BoardController(BoardService boardService, JWTUtil jwtUtil){
|
||||
this.boardService = boardService;
|
||||
this.jwtUtil = jwtUtil;
|
||||
}
|
||||
|
||||
@GetMapping()
|
||||
public ResponseEntity<?> searchBoards(
|
||||
@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
|
||||
){
|
||||
List<ResponseBoardSummaryDto> boardSummaries = boardService.findBoards(pageNo, category, lectureId);
|
||||
|
||||
if(boardSummaries.isEmpty()){
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(boardSummaries, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/{boardId}")
|
||||
public ResponseEntity<?> getBoardDetail(
|
||||
@PathVariable int boardId
|
||||
){
|
||||
ResponseBoardDetailDto responseBoardDetailDto = boardService.findBoardDetail(boardId);
|
||||
|
||||
return new ResponseEntity<>(responseBoardDetailDto, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<?> addBoard(
|
||||
@RequestBody RequestBoardDto requestBoardDto,
|
||||
HttpServletRequest request
|
||||
){
|
||||
String token = request.getHeader("Authorization");
|
||||
long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||
|
||||
boardService.createBoard(userId, requestBoardDto);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/{boardId}")
|
||||
public ResponseEntity<?> updateBoard(
|
||||
@PathVariable long boardId,
|
||||
@RequestBody RequestBoardUpdateDto requestBoardUpdateDto
|
||||
){
|
||||
boardService.updateBoard(boardId, requestBoardUpdateDto);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/{boardId}")
|
||||
public ResponseEntity<?> deleteBoard(
|
||||
@PathVariable int boardId,
|
||||
HttpServletRequest request
|
||||
){
|
||||
boardService.deleteBoard(boardId);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/comment/{boardId}")
|
||||
public ResponseEntity<?> getComments(
|
||||
@PathVariable int boardId
|
||||
){
|
||||
List<ResponseCommentDto> comments = boardService.findComments(boardId);
|
||||
|
||||
return new ResponseEntity<>(comments, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/comment/{boardId}")
|
||||
public ResponseEntity<?> addComment(
|
||||
@PathVariable int boardId,
|
||||
@RequestBody RequestCommentDto requestCommentDto,
|
||||
HttpServletRequest request
|
||||
){
|
||||
String token = request.getHeader("Authorization");
|
||||
long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||
|
||||
boardService.createComment(userId, boardId, requestCommentDto);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping(value = "/comment/{commentId}")
|
||||
public ResponseEntity<?> updateComment(
|
||||
@PathVariable int commentId,
|
||||
@RequestBody RequestCommentDto requestCommentDto
|
||||
){
|
||||
boardService.updateComment(commentId, requestCommentDto);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/comment/{commentId}")
|
||||
public ResponseEntity<?> deleteComment(
|
||||
@PathVariable int commentId
|
||||
){
|
||||
boardService.deleteComment(commentId);
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.edufocus.edufocus.board.entity.dto;
|
||||
|
||||
|
||||
import com.edufocus.edufocus.board.entity.vo.Board;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class RequestBoardDto {
|
||||
private long lectureId;
|
||||
private String title;
|
||||
private String category;
|
||||
private String content;
|
||||
}
|
@ -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;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.edufocus.edufocus.board.entity.dto;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class RequestCommentDto {
|
||||
private String content;
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
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 long id;
|
||||
private String name;
|
||||
private String title;
|
||||
private String content;
|
||||
private int viewCount;
|
||||
private LocalTime createdAt;
|
||||
private LocalTime modifiedAt;
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.edufocus.edufocus.board.entity.dto;
|
||||
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Builder
|
||||
@Getter
|
||||
@Setter
|
||||
public class ResponseBoardSummaryDto {
|
||||
private long id;
|
||||
private String name;
|
||||
private String title;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
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;
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
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.*;
|
||||
import org.hibernate.annotations.CreationTimestamp;
|
||||
|
||||
import java.time.LocalTime;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
|
||||
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
|
||||
LocalTime createdAt;
|
||||
|
||||
@CreationTimestamp
|
||||
LocalTime modifiedAt;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id")
|
||||
private User user;
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
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;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.time.LocalTime;
|
||||
|
||||
@Entity
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Setter
|
||||
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
|
||||
public class Comment {
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
@Column
|
||||
private String content;
|
||||
|
||||
@Column
|
||||
private LocalTime createdAt;
|
||||
|
||||
@Column
|
||||
private LocalTime modifiedAt;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id")
|
||||
User user;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "board_id")
|
||||
Board board;
|
||||
|
||||
public ResponseCommentDto makeCommentDto() {
|
||||
return ResponseCommentDto.builder()
|
||||
.id(id)
|
||||
.name(user.getEmail())
|
||||
.content(content)
|
||||
.createAt(createdAt)
|
||||
.modifiedAt(modifiedAt)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
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 org.springframework.data.jpa.repository.Query;
|
||||
|
||||
|
||||
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);
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
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);
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.edufocus.edufocus.board.service;
|
||||
|
||||
import com.edufocus.edufocus.board.entity.dto.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BoardService {
|
||||
|
||||
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 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);
|
||||
|
||||
}
|
@ -0,0 +1,130 @@
|
||||
package com.edufocus.edufocus.board.service;
|
||||
|
||||
|
||||
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;
|
||||
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.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 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;
|
||||
this.commentRepository = commentRepository;
|
||||
this.userRepository = userRepository;
|
||||
this.lectureRepository = lectureRepository;
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public List<ResponseBoardSummaryDto> findBoards(int pageNo, String category, long lectureId) {
|
||||
Pageable pageable = PageRequest.of(pageNo, PAGE_SIZE);
|
||||
|
||||
List<Board> boards = boardRepository.findByLectureIdAndCategory(lectureId, category, pageable).getContent();
|
||||
|
||||
return boards.stream().map(Board::makeSummaryDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ResponseBoardDetailDto findBoardDetail(long boardId) {
|
||||
return boardRepository.findById(boardId)
|
||||
.orElseThrow()
|
||||
.makeDetailDto();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void createBoard(long userId, RequestBoardDto requestBoardDto) {
|
||||
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.getContent())
|
||||
.user(user)
|
||||
.lecture(lecture)
|
||||
.build();
|
||||
|
||||
boardRepository.save(board);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateBoard(long boardId, RequestBoardUpdateDto requestBoardUpdateDto) {
|
||||
Board board = boardRepository.findById(boardId).get();
|
||||
|
||||
board.setTitle(requestBoardUpdateDto.getTitle());
|
||||
board.setContent(requestBoardUpdateDto.getContent());
|
||||
|
||||
boardRepository.save(board);
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public void deleteBoard(long boardId) {
|
||||
Board board = boardRepository.findById(boardId).get();
|
||||
|
||||
boardRepository.delete(board);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<ResponseCommentDto> findComments(long boardId) {
|
||||
return commentRepository.findByBoardId(boardId).stream()
|
||||
.map(Comment::makeCommentDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@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(requestCommentDto.getContent())
|
||||
.board(board)
|
||||
.user(user)
|
||||
.build();
|
||||
|
||||
commentRepository.save(comment);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Transactional
|
||||
public void updateComment(long commentId, RequestCommentDto requestCommentDto) {
|
||||
Comment comment = commentRepository.findById(commentId).get();
|
||||
|
||||
comment.setContent(requestCommentDto.getContent());
|
||||
|
||||
commentRepository.save(comment);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteComment(long commentId) {
|
||||
Comment comment = commentRepository.findById(commentId).get();
|
||||
|
||||
commentRepository.delete(comment);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user