Merge branch 'backend' of https://github.com/TeamBNBN/edufocus into be/Lecture
This commit is contained in:
commit
8dd010b6c5
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package com.edufocus.edufocus.qna.controller;
|
package com.edufocus.edufocus.qna.controller;
|
||||||
|
|
||||||
import com.edufocus.edufocus.qna.entity.Qna;
|
import com.edufocus.edufocus.qna.entity.Qna;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaRequestDto;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaResponseDto;
|
||||||
import com.edufocus.edufocus.qna.service.QnaService;
|
import com.edufocus.edufocus.qna.service.QnaService;
|
||||||
import com.edufocus.edufocus.user.util.JWTUtil;
|
import com.edufocus.edufocus.user.util.JWTUtil;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
@ -21,28 +23,71 @@ import java.util.List;
|
|||||||
public class QnaController {
|
public class QnaController {
|
||||||
private final QnaService qnaService;
|
private final QnaService qnaService;
|
||||||
private final JWTUtil jwtUtil;
|
private final JWTUtil jwtUtil;
|
||||||
|
private static int PAGE_SIZE=10;
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public ResponseEntity<Qna> createQna(@RequestBody Qna qna , HttpServletRequest request) {
|
@PostMapping("/{lecture_id}")
|
||||||
|
public ResponseEntity<QnaResponseDto> createQna(@PathVariable("lecture_id") Long lecture_id, @RequestBody QnaRequestDto qnaRequestDto , HttpServletRequest request) {
|
||||||
|
|
||||||
|
|
||||||
try{
|
try{
|
||||||
String token = request.getHeader("Authorization");
|
String token = request.getHeader("Authorization");
|
||||||
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
Long userId = Long.parseLong(jwtUtil.getUserId(token));
|
||||||
|
|
||||||
qnaService.createQna(userId,qna);
|
QnaResponseDto qnaResponseDto= qnaService.createQna(userId,qnaRequestDto,lecture_id);
|
||||||
return new ResponseEntity<>(qna, HttpStatus.CREATED);
|
return new ResponseEntity<>( qnaResponseDto,HttpStatus.CREATED);
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping({"/answer/create/{qna_id}"})
|
||||||
|
public ResponseEntity<QnaResponseDto> createAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
QnaResponseDto responseDto = qnaService.createAnswer(qna_id,qnaRequestDto);
|
||||||
|
return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping({"/answer/update/{qna_id}"})
|
||||||
|
public ResponseEntity<QnaResponseDto> updateAnswer(@PathVariable("qna_id") Long qna_id, @RequestBody QnaRequestDto qnaRequestDto)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
QnaResponseDto responseDto = qnaService.updateAnswer(qna_id,qnaRequestDto);
|
||||||
|
return new ResponseEntity<>(responseDto,HttpStatus.ACCEPTED);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/answer/delete/{qna_id}")
|
||||||
|
public ResponseEntity<QnaResponseDto> deleteAnswer(@PathVariable("qna_id") Long qna_id)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
qnaService.deleteAnswer(qna_id);
|
||||||
|
return new ResponseEntity<>(HttpStatus.ACCEPTED);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public ResponseEntity<Qna> updateQna(@PathVariable Long id, @RequestBody Qna qna) {
|
public ResponseEntity<QnaResponseDto> updateQna(@PathVariable Long id, @RequestBody QnaRequestDto qnaRequestDto) {
|
||||||
|
|
||||||
try{
|
try{
|
||||||
qnaService.updateQna(id,qna);
|
QnaResponseDto qnaResponseDto= qnaService.updateQna(id,qnaRequestDto);
|
||||||
return new ResponseEntity<>(qna, HttpStatus.ACCEPTED);
|
return new ResponseEntity<>(qnaResponseDto, HttpStatus.ACCEPTED);
|
||||||
|
|
||||||
}catch (Exception e)
|
}catch (Exception e)
|
||||||
{
|
{
|
||||||
@ -72,15 +117,12 @@ public class QnaController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/all/{id}")
|
@GetMapping("/all/{id}")
|
||||||
public ResponseEntity<List<Qna>> getAllQna(@PathVariable Long id) {
|
public ResponseEntity<List<QnaResponseDto>> getAllQna(@PathVariable Long id) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
System.out.print("@@@@@@@@@@@@@@@@@@@@@@@>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
|
|
||||||
List<Qna> qnaList= qnaService.getAllQnasByLecture(id);
|
List<QnaResponseDto> qnaList= qnaService.getAllQnasByLecture(id,PAGE_SIZE);
|
||||||
for(Qna qna:qnaList)
|
|
||||||
{
|
|
||||||
System.out.print(qna.toString());
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED);
|
return new ResponseEntity<>(qnaList, HttpStatus.ACCEPTED);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
@ -3,8 +3,7 @@ package com.edufocus.edufocus.qna.entity;
|
|||||||
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 jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.Setter;
|
|
||||||
import org.checkerframework.checker.units.qual.C;
|
import org.checkerframework.checker.units.qual.C;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@ -12,6 +11,9 @@ import java.util.Date;
|
|||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class Qna {
|
public class Qna {
|
||||||
|
|
||||||
// 연관관계 주인
|
// 연관관계 주인
|
||||||
@ -42,6 +44,7 @@ public class Qna {
|
|||||||
private String answer;
|
private String answer;
|
||||||
|
|
||||||
|
|
||||||
|
private String name;
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name= "id")
|
@JoinColumn(name= "id")
|
||||||
private User user;
|
private User user;
|
||||||
@ -51,4 +54,5 @@ public class Qna {
|
|||||||
private Lecture lecture;
|
private Lecture lecture;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.edufocus.edufocus.qna.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class QnaRequestDto {
|
||||||
|
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private String content;
|
||||||
|
private String answer;
|
||||||
|
|
||||||
|
public static Qna toEntity(QnaRequestDto qnaRequestDto) {
|
||||||
|
{
|
||||||
|
|
||||||
|
return Qna.builder()
|
||||||
|
.content(qnaRequestDto.getContent())
|
||||||
|
.title(qnaRequestDto.getTitle())
|
||||||
|
.answer(qnaRequestDto.getAnswer())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package com.edufocus.edufocus.qna.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class QnaResponseDto {
|
||||||
|
|
||||||
|
|
||||||
|
private String title;
|
||||||
|
private String username;
|
||||||
|
private String content;
|
||||||
|
private Date createtAt;
|
||||||
|
private String answer;
|
||||||
|
public static QnaResponseDto toEntity(Qna qna)
|
||||||
|
{
|
||||||
|
return new QnaResponseDto(
|
||||||
|
|
||||||
|
qna.getTitle(),
|
||||||
|
qna.getUser().getName(),
|
||||||
|
qna.getContent(),
|
||||||
|
qna.getCreatedAt(),
|
||||||
|
qna.getAnswer()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
|||||||
package com.edufocus.edufocus.qna.repository;
|
package com.edufocus.edufocus.qna.repository;
|
||||||
|
|
||||||
import com.edufocus.edufocus.qna.entity.Qna;
|
import com.edufocus.edufocus.qna.entity.Qna;
|
||||||
|
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.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.query.Param;
|
import org.springframework.data.repository.query.Param;
|
||||||
@ -11,8 +13,7 @@ import java.util.List;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface QnaRepository extends JpaRepository<Qna, Long> {
|
public interface QnaRepository extends JpaRepository<Qna, Long> {
|
||||||
|
|
||||||
@Query(value = "SELECT * FROM qna WHERE lecture_id = :lectureId", nativeQuery = true)
|
|
||||||
List<Qna> findLecture(@Param("lectureId") Long lectureId);
|
|
||||||
|
|
||||||
|
|
||||||
|
List<Qna> findByLectureId(Long lecturerId);
|
||||||
|
Page<Qna> findByLectureId(Long lectureId, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package com.edufocus.edufocus.qna.service;
|
|||||||
|
|
||||||
import com.edufocus.edufocus.lecture.entity.Lecture;
|
import com.edufocus.edufocus.lecture.entity.Lecture;
|
||||||
import com.edufocus.edufocus.qna.entity.Qna;
|
import com.edufocus.edufocus.qna.entity.Qna;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaRequestDto;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaResponseDto;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
@ -11,9 +13,14 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public interface QnaService {
|
public interface QnaService {
|
||||||
|
|
||||||
void createQna(Long id,Qna qna) throws SQLException;
|
QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) throws SQLException;
|
||||||
void updateQna(Long id,Qna qna) throws SQLException;
|
QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) throws SQLException;
|
||||||
void deleteQna(Long id) throws SQLException;
|
void deleteQna(Long id) throws SQLException;
|
||||||
Qna getQna(Long id) throws SQLException;
|
Qna getQna(Long id) throws SQLException;
|
||||||
List<Qna> getAllQnasByLecture(Long lectureId) throws SQLException;
|
|
||||||
|
List<QnaResponseDto> getAllQnasByLecture(Long lectureId,int pageNumber) throws SQLException;
|
||||||
|
QnaResponseDto createAnswer(Long id,QnaRequestDto qnaRequestDto) throws SQLException;
|
||||||
|
QnaResponseDto updateAnswer(Long id,QnaRequestDto qnaRequestDto) throws SQLException;
|
||||||
|
void deleteAnswer(Long id) throws SQLException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,28 @@
|
|||||||
package com.edufocus.edufocus.qna.service;
|
package com.edufocus.edufocus.qna.service;
|
||||||
|
|
||||||
|
import com.edufocus.edufocus.lecture.entity.Lecture;
|
||||||
|
import com.edufocus.edufocus.lecture.repository.LectureRepository;
|
||||||
import com.edufocus.edufocus.qna.entity.Qna;
|
import com.edufocus.edufocus.qna.entity.Qna;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaRequestDto;
|
||||||
|
import com.edufocus.edufocus.qna.entity.QnaResponseDto;
|
||||||
import com.edufocus.edufocus.qna.repository.QnaRepository;
|
import com.edufocus.edufocus.qna.repository.QnaRepository;
|
||||||
|
import com.edufocus.edufocus.user.model.entity.User;
|
||||||
|
import com.edufocus.edufocus.user.model.repository.UserRepository;
|
||||||
import jakarta.transaction.Transactional;
|
import jakarta.transaction.Transactional;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.PageRequest;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -17,25 +30,46 @@ import java.util.Optional;
|
|||||||
public class QnaServiceImpl implements QnaService{
|
public class QnaServiceImpl implements QnaService{
|
||||||
|
|
||||||
private final QnaRepository qnaRepository;
|
private final QnaRepository qnaRepository;
|
||||||
|
private final LectureRepository lectureRepository;
|
||||||
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createQna(Long id,Qna qna) {
|
public QnaResponseDto createQna(Long id, QnaRequestDto qnaRequestDto, Long lecture_id) {
|
||||||
|
|
||||||
|
|
||||||
qna.setId(id);
|
Lecture lecture = lectureRepository.findById(lecture_id).orElse(null);
|
||||||
|
|
||||||
|
User user = userRepository.findById(id).orElse(null);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Qna qna = QnaRequestDto.toEntity(qnaRequestDto);
|
||||||
|
qna.setLecture(lecture);
|
||||||
|
qna.setUser(user);
|
||||||
|
|
||||||
|
qna.setCreatedAt(new Date());
|
||||||
|
|
||||||
qnaRepository.save(qna);
|
qnaRepository.save(qna);
|
||||||
|
return QnaResponseDto.toEntity(qna);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateQna(Long id,Qna qna) {
|
public QnaResponseDto updateQna(Long id,QnaRequestDto qnaRequestDto) {
|
||||||
|
|
||||||
|
|
||||||
Optional<Qna> findQna = qnaRepository.findById(id);
|
Qna findQna = qnaRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new RuntimeException("QnA not found"));
|
||||||
|
|
||||||
qna.setModifiedAt(new Date());
|
findQna.setModifiedAt(new Date());
|
||||||
qnaRepository.save(qna);
|
findQna.setTitle(qnaRequestDto.getTitle());
|
||||||
|
findQna.setContent(qnaRequestDto.getContent());
|
||||||
|
|
||||||
|
qnaRepository.save(findQna);
|
||||||
|
|
||||||
|
|
||||||
|
return QnaResponseDto.toEntity(findQna);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -51,11 +85,46 @@ qnaRepository.deleteById(id);
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Qna> getAllQnasByLecture(Long lectureId) {
|
public List<QnaResponseDto> getAllQnasByLecture(Long lectureId,int pageSize)
|
||||||
|
{
|
||||||
|
|
||||||
|
Pageable pageable = PageRequest.of(0, pageSize);
|
||||||
|
|
||||||
|
Page<Qna> qnaPage = qnaRepository.findByLectureId(lectureId, pageable);
|
||||||
|
|
||||||
|
|
||||||
System.out.printf(lectureId+"!!!!!!!!!!!!!!!!!!!!!!");
|
return qnaPage.getContent().stream()
|
||||||
return qnaRepository.findLecture(lectureId);
|
.map(QnaResponseDto::toEntity)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QnaResponseDto createAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException {
|
||||||
|
|
||||||
|
Qna findQna = qnaRepository.findById(id).orElse(null);
|
||||||
|
findQna.setAnswer(qnaRequestDto.getAnswer());
|
||||||
|
|
||||||
|
qnaRepository.save(findQna);
|
||||||
|
|
||||||
|
return QnaResponseDto.toEntity(findQna);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public QnaResponseDto updateAnswer(Long id, QnaRequestDto qnaRequestDto) throws SQLException {
|
||||||
|
|
||||||
|
Qna findQna = qnaRepository.findById(id).orElse(null);
|
||||||
|
findQna.setAnswer(qnaRequestDto.getAnswer());
|
||||||
|
|
||||||
|
qnaRepository.save(findQna);
|
||||||
|
|
||||||
|
return QnaResponseDto.toEntity(findQna);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void deleteAnswer(Long id) throws SQLException {
|
||||||
|
|
||||||
|
qnaRepository.deleteById(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user