Skip to content

Commit

Permalink
Merge pull request #132 from dongyeon1031/main
Browse files Browse the repository at this point in the history
refactor : 좋아요 및 댓글 리팩토링 및 EntityType Enum 추가
  • Loading branch information
TaetaetaE01 authored Oct 8, 2024
2 parents 2a9ad6d + 015552b commit 989d108
Show file tree
Hide file tree
Showing 25 changed files with 279 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.example.bigbrotherbe.domain.campusNotice.util.CampusNoticeMultipartFile;
import com.example.bigbrotherbe.global.file.dto.FileSaveDTO;
import com.example.bigbrotherbe.global.file.entity.File;
import com.example.bigbrotherbe.global.file.enums.FileType;
import com.example.bigbrotherbe.global.common.enums.EntityType;
import com.example.bigbrotherbe.global.file.service.FileService;
import lombok.*;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -37,7 +37,7 @@ public CampusNotice toCampusNoticeEntity(FileService fileService, CampusNoticeTy
List<File> files = null;
if (fileService.checkExistRequestFile(multipartFiles)) {
FileSaveDTO fileSaveDTO = FileSaveDTO.builder()
.fileType(FileType.CAMPUS_NOTICE.getType())
.fileType(EntityType.CAMPUS_NOTICE_TYPE.getType())
.multipartFileList(multipartFiles)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
import com.example.bigbrotherbe.domain.comment.dto.CommentReplyRequest;
import com.example.bigbrotherbe.domain.comment.dto.CommentUpdateRequest;
import com.example.bigbrotherbe.domain.comment.service.CommentService;
import com.example.bigbrotherbe.global.exception.response.ApiResponse;
import com.example.bigbrotherbe.global.common.exception.response.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static com.example.bigbrotherbe.global.exception.enums.SuccessCode.SUCCESS;
import static com.example.bigbrotherbe.global.common.exception.enums.SuccessCode.SUCCESS;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/comment")
public class CommentController {
private final CommentService commentService;

@PostMapping("/comment")
public ResponseEntity<ApiResponse<Void>> registerComment(@RequestPart(value = "commentRegisterRequest") CommentRegisterRequest commentRegisterRequest){
@PostMapping()
public ResponseEntity<ApiResponse<Void>> registerComment(@RequestBody CommentRegisterRequest commentRegisterRequest){
this.commentService.registerComment(commentRegisterRequest);
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}

@PostMapping("/reply")
public ResponseEntity<ApiResponse<Void>> registerReply(@RequestPart(value = "commentReplyRequest") CommentReplyRequest commentReplyRequest){
this.commentService.registerReply(commentReplyRequest);
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}
// @PostMapping("/reply")
// public ResponseEntity<ApiResponse<Void>> registerReply(@RequestBody CommentReplyRequest commentReplyRequest){
// this.commentService.registerReply(commentReplyRequest);
// return ResponseEntity.ok(ApiResponse.success(SUCCESS));
// }

@PutMapping("/{commentId}")
public ResponseEntity<ApiResponse<Void>> updateComment(@PathVariable("commentId") Long id,
@RequestPart(value = "commentUpdateRequest")CommentUpdateRequest commentUpdateRequest){
@RequestBody CommentUpdateRequest commentUpdateRequest){
this.commentService.updateComment(id, commentUpdateRequest);
return ResponseEntity.ok(ApiResponse.success(SUCCESS));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

@Getter
public class CommentRegisterRequest {
private Long parentId;
private String content;
private String entityType;
private Long entityId;
Expand All @@ -16,4 +17,11 @@ public Comment toCommentEntity(Member member){
.members(member)
.build();
}
public Comment toCommentEntity(Member member, Comment parent){
return Comment.builder()
.content(this.content)
.members(member)
.parent(parent)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.example.bigbrotherbe.domain.comment.entity;

import com.example.bigbrotherbe.domain.BaseTimeEntity;
import com.example.bigbrotherbe.domain.event.entity.Event;
import com.example.bigbrotherbe.domain.member.entity.Member;
import com.example.bigbrotherbe.domain.notice.entity.Notice;
import com.example.bigbrotherbe.global.common.enums.EntityType;
import com.example.bigbrotherbe.global.entity.BaseTimeEntity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.SQLDelete;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE comment SET deleted = true WHERE comment_id = ?")
public class Comment extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -22,6 +25,12 @@ public class Comment extends BaseTimeEntity {
@Column(name = "content")
private String content;

@Enumerated // enum type 명시
private EntityType entityType;

@Column(nullable = false)
private boolean deleted = Boolean.FALSE;

@JsonIgnore
@ManyToOne
@JoinColumn(name = "parent_id")
Expand Down Expand Up @@ -66,6 +75,8 @@ public void update(String content) {
this.content = content;
}

public void setEntityType(EntityType entityType){this.entityType = entityType;}

public void linkNotice(Notice notice) {
this.notice = notice;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.example.bigbrotherbe.domain.comment.repository;

import com.example.bigbrotherbe.domain.comment.entity.Comment;
import io.lettuce.core.dynamic.annotation.Param;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

public interface CommentRepository extends JpaRepository<Comment, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

public interface CommentService {
public void registerComment(CommentRegisterRequest commentRegisterRequest);
public void registerReply(CommentReplyRequest commentReplyRequest);
public void updateComment(Long id, CommentUpdateRequest commentUpdateRequest);
public void deleteComment(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.example.bigbrotherbe.domain.comment.service;

import com.example.bigbrotherbe.domain.comment.dto.CommentRegisterRequest;
import com.example.bigbrotherbe.domain.comment.dto.CommentReplyRequest;
import com.example.bigbrotherbe.domain.comment.dto.CommentUpdateRequest;
import com.example.bigbrotherbe.domain.comment.entity.Comment;
import com.example.bigbrotherbe.domain.comment.enums.EntityType;
import com.example.bigbrotherbe.domain.comment.repository.CommentRepository;
import com.example.bigbrotherbe.domain.event.repository.EventRepository;
import com.example.bigbrotherbe.domain.member.entity.Member;
import com.example.bigbrotherbe.domain.notice.repository.NoticeRepository;
import com.example.bigbrotherbe.global.exception.BusinessException;
import com.example.bigbrotherbe.global.jwt.component.AuthUtil;
import com.example.bigbrotherbe.global.auth.util.AuthUtil;
import com.example.bigbrotherbe.global.common.constant.Constant;
import com.example.bigbrotherbe.global.common.enums.EntityType;
import com.example.bigbrotherbe.global.common.exception.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.example.bigbrotherbe.global.exception.enums.ErrorCode.NOT_FOUNT_ENTITY;
import static com.example.bigbrotherbe.global.exception.enums.ErrorCode.NO_EXIST_COMMENT;
import static com.example.bigbrotherbe.global.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
Expand All @@ -29,21 +28,19 @@ public class CommentServiceImpl implements CommentService{
@Override
public void registerComment(CommentRegisterRequest commentRegisterRequest) {
Member member = authUtil.getLoginMember();

Comment comment = commentRegisterRequest.toCommentEntity(member);
this.setCommentEntity(comment, commentRegisterRequest.getEntityType(), commentRegisterRequest.getEntityId());

commentRepository.save(comment);
}

@Override
public void registerReply(CommentReplyRequest commentReplyRequest) {
Member member = authUtil.getLoginMember();
Comment parentComment = commentRepository.findById(commentReplyRequest.getParentId())
.orElseThrow(() -> new BusinessException(NO_EXIST_COMMENT));

Comment comment = commentReplyRequest.toCommentEntity(member, parentComment);
this.setCommentEntity(comment, parentComment);
Comment comment;
if (commentRegisterRequest.getParentId() != null){
Comment parentComment = commentRepository.findById(commentRegisterRequest.getParentId())
.orElseThrow(() -> new BusinessException(NO_EXIST_COMMENT));
if (parentComment.isDeleted()){
throw new BusinessException(NO_EXIST_COMMENT);
}
comment = commentRegisterRequest.toCommentEntity(member, parentComment);
this.setCommentEntity(comment, parentComment);
}else{
comment = commentRegisterRequest.toCommentEntity(member);
this.setCommentEntity(comment, commentRegisterRequest.getEntityType(), commentRegisterRequest.getEntityId());
}

commentRepository.save(comment);
}
Expand All @@ -52,6 +49,13 @@ public void registerReply(CommentReplyRequest commentReplyRequest) {
public void updateComment(Long id, CommentUpdateRequest commentUpdateRequest) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new BusinessException(NO_EXIST_COMMENT));

if (!comment.getMembers().equals(authUtil.getLoginMember())){
throw new BusinessException(NOT_REGISTER_MEMBER);
}
if (comment.isDeleted()){
throw new BusinessException(NO_EXIST_COMMENT);
}
comment.update(commentUpdateRequest.getContent());
commentRepository.save(comment);
}
Expand All @@ -61,34 +65,56 @@ public void deleteComment(Long id) {
Comment comment = commentRepository.findById(id)
.orElseThrow(() -> new BusinessException(NO_EXIST_COMMENT));

if (!comment.getMembers().equals(authUtil.getLoginMember())){
throw new BusinessException(NOT_REGISTER_MEMBER);
}
if (comment.isDeleted()){
throw new BusinessException(NO_EXIST_COMMENT);
}
commentRepository.delete(comment);
}

private void setCommentEntity(Comment comment, String entityType, Long entityId){
/*
comment와 관계를 맺은 entity 연결
*/
if (entityType.equals(EntityType.NOTICE.getType())){
comment.linkNotice(noticeRepository.findById(entityId)
.orElseThrow(() -> new BusinessException(NOT_FOUNT_ENTITY)));
}else if(entityType.equals(EntityType.EVENT.getType())){
comment.linkEvent(this.eventRepository.findById(entityId)
.orElseThrow(() -> new BusinessException(NOT_FOUNT_ENTITY)));
}else {
throw new BusinessException(NOT_FOUNT_ENTITY);
EntityType type;
switch (entityType){
case Constant.Entity.NOTICE:
comment.linkNotice(noticeRepository.findById(entityId)
.orElseThrow(() -> new BusinessException(NOT_FOUNT_ENTITY)));
type = EntityType.NOTICE_TYPE;
break;
case Constant.Entity.EVENT:
comment.linkEvent(this.eventRepository.findById(entityId)
.orElseThrow(() -> new BusinessException(NOT_FOUNT_ENTITY)));
type = EntityType.EVENT_TYPE;
break;
default:
throw new BusinessException(NOT_FOUNT_ENTITY);
}
this.setCommentEntityType(comment, type);
}

private void setCommentEntity(Comment comment, Comment parent){
/*
부모 comment외 관계를 맺은 entity 연결
부모 comment와 관계를 맺은 entity 연결
*/
if(parent.getNotice() != null){
comment.linkNotice(parent.getNotice());
}else if(parent.getEvent() != null){
comment.linkEvent(parent.getEvent());
}else{
throw new BusinessException(NOT_FOUNT_ENTITY);
EntityType type = EntityType.getEntityType(parent.getEntityType().getType());
switch (parent.getEntityType().getType()){
case Constant.Entity.NOTICE:
comment.linkNotice(parent.getNotice());
break;
case Constant.Entity.EVENT:
comment.linkEvent(parent.getEvent());
break;
default:
throw new BusinessException(NOT_FOUNT_ENTITY);
}
this.setCommentEntityType(comment, type);
}

private void setCommentEntityType(Comment comment, EntityType entityType){
comment.setEntityType(entityType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.example.bigbrotherbe.global.file.dto.FileSaveDTO;
import com.example.bigbrotherbe.global.file.dto.FileUpdateDTO;
import com.example.bigbrotherbe.global.file.entity.File;
import com.example.bigbrotherbe.global.file.enums.FileType;
import com.example.bigbrotherbe.global.common.enums.EntityType;
import com.example.bigbrotherbe.global.file.service.FileService;
import com.example.bigbrotherbe.global.file.util.FileUtil;
import com.example.bigbrotherbe.global.auth.util.AuthUtil;
Expand All @@ -26,6 +26,8 @@

import java.util.List;

import static com.example.bigbrotherbe.global.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class EventServiceImpl implements EventService {
Expand Down Expand Up @@ -54,7 +56,7 @@ public void registerEvent(EventRegisterRequest eventRegisterRequest, List<Multip
List<File> files = null;
if (fileService.checkExistRequestFile(multipartFiles)) {
FileSaveDTO fileSaveDTO = FileSaveDTO.builder()
.fileType(FileType.EVENT.getType())
.fileType(EntityType.EVENT_TYPE.getType())
.multipartFileList(multipartFiles)
.build();

Expand Down Expand Up @@ -86,7 +88,7 @@ public void updateEvent(Long eventId, EventUpdateRequest eventUpdateRequest, Lis
List<File> files = null;
if (fileService.checkExistRequestFile(multipartFiles)) {
FileUpdateDTO fileUpdateDTO = FileUpdateDTO.builder()
.fileType(FileType.EVENT.getType())
.fileType(EntityType.EVENT_TYPE.getType())
.multipartFileList(multipartFiles)
.files(event.getFiles())
.build();
Expand All @@ -113,7 +115,7 @@ public void deleteEvent(Long eventId) {
}

FileDeleteDTO fileDeleteDTO = FileDeleteDTO.builder()
.fileType(FileType.EVENT.getType())
.fileType(EntityType.EVENT_TYPE.getType())
.files(event.getFiles())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.example.bigbrotherbe.global.file.dto.FileSaveDTO;
import com.example.bigbrotherbe.global.file.dto.FileUpdateDTO;
import com.example.bigbrotherbe.global.file.entity.File;
import com.example.bigbrotherbe.global.file.enums.FileType;
import com.example.bigbrotherbe.global.common.enums.EntityType;
import com.example.bigbrotherbe.global.file.service.FileService;
import com.example.bigbrotherbe.global.auth.util.AuthUtil;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +24,8 @@

import java.util.List;

import static com.example.bigbrotherbe.global.common.exception.enums.ErrorCode.*;

@Service
@RequiredArgsConstructor
public class FAQServiceImpl implements FAQService {
Expand All @@ -48,7 +50,7 @@ public void register(FAQRegisterRequest faqRegisterRequest, List<MultipartFile>
List<File> files = null;
if (fileService.checkExistRequestFile(multipartFiles)) {
FileSaveDTO fileSaveDTO = FileSaveDTO.builder()
.fileType(FileType.FAQ.getType())
.fileType(EntityType.FAQ_TYPE.getType())
.multipartFileList(multipartFiles)
.build();

Expand Down Expand Up @@ -78,7 +80,7 @@ public void modify(Long faqId, FAQModifyRequest faqModifyRequest, List<Multipart
List<File> files = null;
if (fileService.checkExistRequestFile(multipartFiles)) {
FileUpdateDTO fileUpdateDTO = FileUpdateDTO.builder()
.fileType(FileType.FAQ.getType())
.fileType(EntityType.FAQ_TYPE.getType())
.multipartFileList(multipartFiles)
.files(faq.getFiles())
.build();
Expand Down
Loading

0 comments on commit 989d108

Please sign in to comment.