feat : 게시판 구현

This commit is contained in:
yulmam 2024-07-18 14:39:43 +09:00
parent 4f5d367e01
commit a02ee65a7e
13 changed files with 347 additions and 52 deletions

View File

@ -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<ResponseBoardDto> boards = boardService.findBoards(userId, category, lectureId);
List<Board> 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<Comment> 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);

View File

@ -1,2 +0,0 @@
package com.edufocus.edufocus.board.entity.dto;public class RequestBoartDetailDto {
}

View File

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

View File

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

View File

@ -1,2 +1,4 @@
package com.edufocus.edufocus.board.entity.dto;public class RequestCommentDto {
package com.edufocus.edufocus.board.entity.dto;
public class RequestCommentDto {
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<Board, Long> {
Page<Board> findByLectureIdAndCategory(Long lectureId, String category, Pageable pageable);
}

View File

@ -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<Comment, Long> {
List<Comment> findByBoardId(long boardId);
}

View File

@ -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<Board> 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<Comment> 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);
}

View File

@ -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<Board> 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();
}
@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<Comment> 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);
}
}