Skip to content

Commit

Permalink
Merge branch 'feat-postcomment'
Browse files Browse the repository at this point in the history
  • Loading branch information
epdlrnldudnj committed Jan 23, 2024
2 parents 0508dea + 9e1fe2b commit 2d2b147
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,48 @@
package com.kimgreen.backend.domain.community.controller;

import com.kimgreen.backend.domain.community.dto.GetCommentDto;
import com.kimgreen.backend.domain.community.dto.PostCommentDto;
import com.kimgreen.backend.domain.community.entity.Comment;
import com.kimgreen.backend.domain.community.service.CommentService;
import com.kimgreen.backend.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.kimgreen.backend.response.Message.*;
import static com.kimgreen.backend.response.Response.success;
import static org.springframework.http.HttpStatus.OK;

@Tag(name = "Comment")
@RestController
@RequestMapping(value="/comment")
@AllArgsConstructor
public class CommentController {
private CommentService commentService;
@Operation(summary = "댓글 작성")
@ResponseStatus(OK)
@PostMapping("/write-comment")
public Response postComment(@RequestParam("postId") Long postId, @RequestBody PostCommentDto postCommentDto) {
commentService.postComment(postId, postCommentDto);
return success(POST_COMMENT_SUCCESS);
}

@Operation(summary = "댓글 삭제")
@ResponseStatus(OK)
@DeleteMapping("/delete-comment")
public Response deleteComment(@RequestParam("commentId") Long commentId){
commentService.deleteComment(commentId);
return success(DELETE_COMMENT_SUCCESS);
}

@Operation(summary = "댓글 목록 불러오기")
@ResponseStatus(OK)
@GetMapping("/get-comment")
public Response getComment(@RequestParam("postId") Long postId){
List <GetCommentDto> commentList = commentService.getComment(postId);
return success(GET_COMMENT_SUCCESS, commentList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kimgreen.backend.domain.community.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.kimgreen.backend.domain.community.entity.Comment;
import lombok.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Component
public class GetCommentDto {
private Long commentId;
private String writerProfileImg;
private String writerNickname;
private String writerBadge;
private String content;
private boolean isWriter;
private LocalDateTime createAt;

public static GetCommentDto from(Comment comment, String writerProfileImg, String writerBadge, boolean isWriter) {

return GetCommentDto.builder()
.commentId(comment.getCommentId())
.writerProfileImg(writerProfileImg)
.writerNickname(comment.getMember().getNickname())
.writerBadge(writerBadge)
.content(comment.getContent())
.isWriter(isWriter)
.createAt(comment.getCreatedAt())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kimgreen.backend.domain.community.dto;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Getter
@AllArgsConstructor
@NoArgsConstructor

public class PostCommentDto {
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public class Comment extends AuditEntity {

@Column
private String content;


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

@Repository
public interface PostImgRepository extends JpaRepository<PostImg, Long> {

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,88 @@
package com.kimgreen.backend.domain.community.service;

import com.kimgreen.backend.domain.community.dto.GetCommentDto;
import com.kimgreen.backend.domain.community.dto.PostCommentDto;
import com.kimgreen.backend.domain.community.entity.Comment;
import com.kimgreen.backend.domain.community.entity.Post;
import com.kimgreen.backend.domain.community.repository.CommentRepository;
import com.kimgreen.backend.domain.community.repository.PostRepository;
import com.kimgreen.backend.domain.member.entity.Member;
import com.kimgreen.backend.domain.member.entity.MemberProfileImg;
import com.kimgreen.backend.domain.member.repository.MemberProfileImgRepository;
import com.kimgreen.backend.domain.member.service.MemberService;
import com.kimgreen.backend.domain.profile.entity.RepresentativeBadge;
import com.kimgreen.backend.domain.profile.repository.RepresentativeBadgeRepository;
import com.kimgreen.backend.exception.PostNotFound;
import com.kimgreen.backend.exception.WrongPath;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@AllArgsConstructor

public class CommentService {

private final MemberService memberService;
private final CommentRepository commentRepository;
private final PostRepository postRepository;
private final GetCommentDto getCommentDto;
private final S3Service s3Service;
private final RepresentativeBadgeRepository representativeBadgeRepository;

private final MemberProfileImgRepository memberProfileImgRepository;

public void postComment(Long postId, PostCommentDto postCommentDto){
Member member = memberService.getCurrentMember();
Post post = postRepository.findById(postId).orElseThrow(PostNotFound::new);

Comment comment = Comment.builder()
.content(postCommentDto.getContent())
.post(post)
.member(member)
.build();
// 데이터 베이스에 저장하기
commentRepository.save(comment);
// 명세서의 오류들 처리하기???
}

public void deleteComment(Long commentId){
Member member = memberService.getCurrentMember();
Comment comment = commentRepository.findById(commentId).orElseThrow(WrongPath::new);
commentRepository.delete(comment);

}

public List<GetCommentDto> getComment(Long postId){
List <Comment> allCommentList = commentRepository.findAll(); // 모든 댓글 목록
Post post = postRepository.findById(postId).orElseThrow(PostNotFound::new); // 내가 보고싶은 포스트

List <GetCommentDto> commentList = new ArrayList<>(); // 리턴

for (Comment comment : allCommentList){
if (comment.getPost() == post){
MemberProfileImg memberProfileImg = memberProfileImgRepository.findByMember(comment.getMember());
RepresentativeBadge representativeBadge = representativeBadgeRepository.findByMember(comment.getMember());
/*
GetCommentDto.from(comment,
s3Service.getFullUrl(memberProfileImg.getImgUrl()),
representativeBadge.getRepresentativeBadge().name,
comment.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId()));
*/
GetCommentDto getCommentDto1 = GetCommentDto.builder()
.commentId(comment.getCommentId())
.writerProfileImg(s3Service.getFullUrl(memberProfileImg.getImgUrl()))
.writerNickname(comment.getMember().getNickname())
.writerBadge(representativeBadge.getRepresentativeBadge().name)
.content(comment.getContent())
.isWriter(comment.getMember().getMemberId().equals(memberService.getCurrentMember().getMemberId()))
.createAt(comment.getCreatedAt())
.build();
commentList.add(getCommentDto1);
}
}
return commentList;
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/kimgreen/backend/exception/WrongPath.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.kimgreen.backend.exception;

public class WrongPath extends RuntimeException{
}
23 changes: 7 additions & 16 deletions src/main/java/com/kimgreen/backend/response/Advice.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,16 @@ public Response BadgeNotFoundResponse() {
return Response.failure(HttpStatus.NOT_FOUND,"요청한 뱃지를 찾을 수 없습니다.");
}

@ExceptionHandler(ConvertingJSONException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Response ConvertingJSONExceptionResponse() {
return Response.failure(HttpStatus.INTERNAL_SERVER_ERROR,"JSON 변환 에러");
}

@ExceptionHandler(TokenNotValid.class)
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Response TokenNotValidResponse() {
return Response.failure(HttpStatus.UNAUTHORIZED,"토큰이 유효하지 않습니다.");
}
@ExceptionHandler(TokenNotFound.class)
@ExceptionHandler(PostNotFound.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Response TokenNotFoundResponse() {
return Response.failure(HttpStatus.NOT_FOUND,"토큰이 없습니다.");
public Response PostNotFoundResponse() {
return Response.failure(HttpStatus.NOT_FOUND, "요청한 게시글을 찾을 수 없습니다.");
}

@ExceptionHandler(PostNotFound.class)
@ExceptionHandler(WrongPath.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public Response PostNotFoundResponse(){return Response.failure(HttpStatus.NOT_FOUND, "요청한 게시글을 찾을 수 없습니다.");}
public Response WrongPath() {
return Response.failure(HttpStatus.NOT_FOUND, "잘못된 경로입니다.");
}

}
4 changes: 4 additions & 0 deletions src/main/java/com/kimgreen/backend/response/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public class Message {
public static String CALENDAR_SUCCESS="프로필 달력 불러오기 성공했습니다.";
public static String CALENDAR_DETAILS_SUCCESS="프로필 달력 상세정보 불러오기 성공했습니다.";

public static String POST_COMMENT_SUCCESS="댓글을 작성하는데 성공하였습니다.";
public static String DELETE_COMMENT_SUCCESS="댓글 삭제 성공했습니다.";
public static String GET_COMMENT_SUCCESS="댓글 목록을 불러오는데 성공하였습니다.";

}

0 comments on commit 2d2b147

Please sign in to comment.