Skip to content

Commit

Permalink
Merge pull request #149 from 2023-Team-Luna-Achieve/BE/Feat/#148
Browse files Browse the repository at this point in the history
[Feat] 유저 댓글 API 수정, 무중단 배포 ec2 nginx 세팅 완료
  • Loading branch information
yoojaeyoonGit authored Jan 7, 2024
2 parents 13233a7 + e49869e commit 673572e
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig {
private static final String ADMIN = "ADMIN";
private final TokenProvider tokenProvider;
private final JwtExtractUtil jwtExtractUtil;
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
Expand Down Expand Up @@ -84,11 +85,13 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.authorizeHttpRequests() // HttpServletRequest를 사용하는 요청들에 대한 접근제한을 설정하겠다.
.antMatchers("/").permitAll() // 로그인
.antMatchers("/api/sign-in").permitAll() // 로그인
.antMatchers("/api/users/sign-up").permitAll() // 회원가입 api
.antMatchers("/api/user/sign-up").permitAll() // 회원가입
.antMatchers("/api/email/verification/request").permitAll() // 이메일 인증요청
.antMatchers("/api/email/verification/confirm").permitAll() // 인증번호 확인
.antMatchers("/api/refresh").permitAll() // 로그인
.antMatchers("/favicon.ico").permitAll()
// .antMatchers("/api/notice")
// .hasRole(ADMIN)
.anyRequest().authenticated()// 그 외 인증 없이 접근X
.and()
.addFilterBefore(new JwtFilter(jwtExtractUtil, customUserDetailsService, tokenProvider), UsernamePasswordAuthenticationFilter.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import java.util.Optional;

@Repository
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Long> {
void deleteAllByUserId(Long id);
Optional<RefreshToken> findRefreshTokenByToken(String token);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backend.backend.noticeboard.dto;

import backend.backend.noticeboard.entity.Category;
import backend.backend.noticeboardcomment.dto.CommentResponse;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -15,6 +16,7 @@ public class NoticeBoardResponseDto {
private Category category;
private String context;
private int viewCount;
// private List<CommentResponse> commentResponse;


@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,7 @@

import backend.backend.noticeboard.dto.NoticeBoardRequestDto;
import backend.backend.noticeboard.dto.NoticeBoardResponseDto;
import backend.backend.noticeboard.validator.AuthorizationValidator;
import backend.backend.user.entity.User;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface NoticeBoardService {
// Page<NoticeBoardResponseDto> getAllNoticeBoards(Pageable pageable, Long offset);

Expand All @@ -28,7 +17,6 @@ public interface NoticeBoardService {
void deleteNoticeBoard(Long id, User user);
}


//커서기반 페이지네이션
// NoticeBoardResponseDto.PagedNoticeBoardResponseDto getNoticeBoards(int size, Long lastNoticeBoardId);

Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import backend.backend.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,50 @@

import backend.backend.auth.jwt.CurrentUser;
import backend.backend.noticeboardcomment.dto.CommentRequestDto;
import backend.backend.noticeboardcomment.dto.CommentResponseDto;
import backend.backend.noticeboardcomment.dto.CommentResponse;
import backend.backend.noticeboardcomment.service.CommentService;
import backend.backend.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@ApiOperation(value = "작성글 API", notes = "회원 작성글의 댓글을 작성한다.")
@Api(tags = "게시글 댓글 API", description = "회원 작성글의 댓글을 작성한다.")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/comments")
@RequestMapping("/api")
public class CommentController {

private final CommentService commentService;

@GetMapping("/notice-board/{noticeBoardId}")
public List<CommentResponseDto> getAllCommentsByNoticeBoardId(@PathVariable Long noticeBoardId) {
@ApiOperation(value = "공지사항 댓글 전체 조회 API", notes = "특정 공지사항의 댓글을 전체조회 한다.")
@GetMapping("/{noticeBoardId}")
public List<CommentResponse> getAllCommentsByNoticeBoardId(@PathVariable Long noticeBoardId) {
return commentService.getAllCommentsByNoticeBoardId(noticeBoardId);
}

@PostMapping("/{noticeBoardId}/create")
public ResponseEntity<CommentResponseDto> createComment(@PathVariable("noticeBoardId") Long noticeBoardId,
@RequestBody CommentRequestDto commentRequestDto,
@CurrentUser User user) {
CommentResponseDto createdComment = commentService.createComment(user, noticeBoardId, commentRequestDto);
@ApiOperation(value = "공지사항 생성 API", notes = "특정 공지사항의 댓글을 생성 한다.")
@PostMapping("/noticeboard/{noticeBoardId}")
public ResponseEntity<CommentResponse> createComment(@PathVariable("noticeBoardId") Long noticeBoardId,
@RequestBody CommentRequestDto commentRequestDto,
@CurrentUser User user) {
CommentResponse createdComment = commentService.createComment(user, noticeBoardId, commentRequestDto);
return ResponseEntity.ok(createdComment);
}

@DeleteMapping("/{commentId}")
@ApiOperation(value = "공지사항 단일 조회 API", notes = "공지사항 댓글을 단일 조회 한다.")
@GetMapping("/comments/{commentId}")
public ResponseEntity<CommentResponse> getCommentById(@PathVariable Long commentId) {
CommentResponse commentDto = commentService.getCommentById(commentId);
return ResponseEntity.ok(commentDto);
}

@ApiOperation(value = "공지사항 삭제 API", notes = "공지사항 댓글을 삭제 한다.")
@DeleteMapping("/comments/{commentId}")
public ResponseEntity<Void> deleteComment(@PathVariable Long commentId) {
commentService.deleteComment(commentId);
return ResponseEntity.noContent().build();
}

// 추가적인 기능이 있다면 여기에 추가

@GetMapping("/{commentId}")
public ResponseEntity<CommentResponseDto> getCommentById(@PathVariable Long commentId) {
CommentResponseDto commentDto = commentService.getCommentById(commentId);
return ResponseEntity.ok(commentDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package backend.backend.noticeboardcomment.dto;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

@Getter
@Setter
public class CommentResponse {
private Long id;
private Long noticeBoardId;
private String author;
private String context;
private LocalDateTime created_at;

public CommentResponse(Long id, Long noticeBoardId, String author, String context, LocalDateTime created_at) {
this.id = id;
this.noticeBoardId = noticeBoardId;
this.author = author;
this.context = context;
this.created_at = created_at;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import backend.backend.noticeboardcomment.entity.NoticeBoardComment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface CommentRepository extends JpaRepository<NoticeBoardComment, Long> {
List<NoticeBoardComment> findAllByNoticeBoardId(Long noticeBoardId);

@Query("SELECT c FROM NoticeBoardComment c JOIN FETCH c.user WHERE c.noticeBoard.id = :noticeBoardId")
List<NoticeBoardComment> findAllByNoticeBoardId(@Param("noticeBoardId") Long noticeBoardId);
// 추가적인 쿼리 메서드가 필요하다면 여기에 추가할 수 있습니다.
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package backend.backend.noticeboardcomment.service;

import backend.backend.noticeboardcomment.dto.CommentRequestDto;
import backend.backend.noticeboardcomment.dto.CommentResponseDto;
import backend.backend.noticeboardcomment.dto.CommentResponse;
import backend.backend.user.entity.User;
import org.springframework.stereotype.Service;

import java.util.List;

public interface CommentService {
List<CommentResponseDto> getAllCommentsByNoticeBoardId(Long noticeBoardId);
CommentResponseDto createComment(User user, Long noticeBoardId, CommentRequestDto commentRequestDto);
List<CommentResponse> getAllCommentsByNoticeBoardId(Long noticeBoardId);
CommentResponse createComment(User user, Long noticeBoardId, CommentRequestDto commentRequestDto);
void deleteComment(Long commentId);
CommentResponseDto getCommentById(Long commentId);

CommentResponse getCommentById(Long commentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,36 @@
import backend.backend.exception.ErrorCode;
import backend.backend.exception.NotFoundException;
import backend.backend.noticeboardcomment.dto.CommentRequestDto;
import backend.backend.noticeboardcomment.dto.CommentResponseDto;
import backend.backend.noticeboardcomment.dto.CommentResponse;
import backend.backend.noticeboardcomment.entity.NoticeBoardComment;
import backend.backend.noticeboardcomment.repository.CommentRepository;
import backend.backend.noticeboard.entity.NoticeBoard;
import backend.backend.noticeboard.repository.NoticeBoardRepository;
import backend.backend.user.entity.User;
import backend.backend.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CommentServiceImpl implements backend.backend.noticeboardcomment.service.CommentService {

private final CommentRepository commentRepository;
private final NoticeBoardRepository noticeBoardRepository;
private final UserRepository userRepository;

public CommentServiceImpl(CommentRepository commentRepository, NoticeBoardRepository noticeBoardRepository, UserRepository userRepository) {
this.commentRepository = commentRepository;
this.noticeBoardRepository = noticeBoardRepository;
this.userRepository = userRepository;
}

@Override
public List<CommentResponseDto> getAllCommentsByNoticeBoardId(Long noticeBoardId) {
public List<CommentResponse> getAllCommentsByNoticeBoardId(Long noticeBoardId) {
return commentRepository.findAllByNoticeBoardId(noticeBoardId).stream()
.map(this::convertToDto)
.collect(Collectors.toList());
}

@Override
public CommentResponseDto createComment(User user, Long noticeBoardId, CommentRequestDto commentRequestDto) {
public CommentResponse createComment(User user, Long noticeBoardId, CommentRequestDto commentRequestDto) {
NoticeBoard noticeBoard = noticeBoardRepository.findById(noticeBoardId)
.orElseThrow(() -> new NotFoundException(ErrorCode.BOARD_NOT_FOUND));

Expand All @@ -56,28 +51,25 @@ public CommentResponseDto createComment(User user, Long noticeBoardId, CommentRe

// 기타 필요한 메소드 정의

private CommentResponseDto convertToDto(NoticeBoardComment comment) {
private CommentResponse convertToDto(NoticeBoardComment comment) {
if (comment == null) {
return null; // 또는 예외를 throw하거나 기본값을 반환할 수 있습니다.
return null;
}

CommentResponseDto commentResponseDto = new CommentResponseDto();
commentResponseDto.setId(comment.getId());
commentResponseDto.setNoticeBoardId(comment.getNoticeBoard().getId());
commentResponseDto.setUserId(comment.getUser().getId());
commentResponseDto.setContext(comment.getContext());
commentResponseDto.setCreated_at(comment.getCreated_at());
// 나머지 필드 설정

return commentResponseDto;
return new CommentResponse(
comment.getId(),
comment.getNoticeBoard().getId(),
comment.getUser().getName(),
comment.getContext(),
comment.getCreated_at()
);
}
@Override
public void deleteComment(Long commentId) {
commentRepository.deleteById(commentId);
}

@Override
public CommentResponseDto getCommentById(Long commentId) {
public CommentResponse getCommentById(Long commentId) {
NoticeBoardComment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new RuntimeException("Comment not found with id: " + commentId));
return convertToDto(comment);
Expand Down
5 changes: 3 additions & 2 deletions backend/src/main/java/backend/backend/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import backend.backend.noticeboard.entity.NoticeBoard;
//import backend.backend.noticeboardcomment.entity.Comment;
import backend.backend.noticeboardcomment.entity.NoticeBoardComment;
import backend.backend.reservation.entity.Reservation;
import lombok.*;

Expand Down Expand Up @@ -32,8 +33,8 @@ public class User {
@OneToMany(mappedBy = "user")
private List<NoticeBoard> noticeBoards;

// @OneToMany(mappedBy = "user")
// private List<Comment> comments;
@OneToMany(mappedBy = "user")
private List<NoticeBoardComment> comments;

@OneToMany(mappedBy = "user")
private List<Reservation> reservation;
Expand Down
Loading

0 comments on commit 673572e

Please sign in to comment.