Skip to content

Commit

Permalink
comment component 연관관계 제거 (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
beomukim authored Nov 24, 2023
1 parent 41fa368 commit 2c56600
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
import org.devcourse.resumeme.business.comment.controller.dto.CommentResponse;
import org.devcourse.resumeme.business.comment.controller.dto.CommentUpdateRequest;
import org.devcourse.resumeme.business.comment.controller.dto.CommentWithReviewResponse;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.resume.domain.Resume;
import org.devcourse.resumeme.business.comment.domain.Comment;
import org.devcourse.resumeme.business.comment.service.CommentService;
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.service.EventService;
import org.devcourse.resumeme.business.resume.entity.Component;
import org.devcourse.resumeme.business.resume.service.ComponentService;
import org.devcourse.resumeme.business.resume.domain.Resume;
import org.devcourse.resumeme.business.resume.service.ResumeService;
import org.devcourse.resumeme.business.comment.service.CommentService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -36,15 +33,12 @@ public class CommentController {

private final EventService eventService;

private final ComponentService componentService;

@PostMapping
public CommentResponse createReview(@PathVariable Long resumeId, @RequestBody CommentCreateRequest request) {
Resume resume = resumeService.getOne(resumeId);
Component component = componentService.getOne(request.componentId());
Comment review = commentService.create(request.toEntity(resume, component), resumeId);
Comment review = commentService.create(request.toEntity(resume), resumeId);

return new CommentResponse(review.getId(), review.getContent(), component.getId(), review.getLastModifiedDate());
return new CommentResponse(review.getId(), review.getContent(), request.componentId(), review.getLastModifiedDate());
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import org.devcourse.resumeme.business.comment.domain.Comment;
import org.devcourse.resumeme.business.resume.domain.Resume;
import org.devcourse.resumeme.business.resume.entity.Component;

public record CommentCreateRequest(Long componentId, String content) {

public Comment toEntity(Resume resume, Component component) {
return new Comment(content, component, resume);
public Comment toEntity(Resume resume) {
return new Comment(content, componentId, resume);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public record CommentResponse(Long commentId, String content, Long componentId, LocalDateTime lastModifiedAt) {

public CommentResponse(Comment comment) {
this(comment.getId(), comment.getContent(), comment.getComponent().getId(), comment.getLastModifiedDate());
this(comment.getId(), comment.getContent(), comment.getComponentId(), comment.getLastModifiedDate());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.devcourse.resumeme.business.resume.domain.Resume;
import org.devcourse.resumeme.business.resume.entity.Component;
import org.devcourse.resumeme.common.domain.BaseEntity;
import org.devcourse.resumeme.global.exception.CustomException;

Expand All @@ -35,22 +34,20 @@ public class Comment extends BaseEntity {
private String content;

@Getter
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "component_id")
private Component component;
private Long componentId;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "resume_id")
private Resume resume;

public Comment(String content, Component component, Resume resume) {
public Comment(String content, Long componentId, Resume resume) {
check(isBlank(content), NO_EMPTY_VALUE);
notNull(resume);
notNull(component);
notNull(componentId);

this.content = content;
this.resume = resume;
this.component = component;
this.componentId = componentId;
}

public void checkSameResume(Long resumeId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package org.devcourse.resumeme.business.comment.repository;

import io.lettuce.core.dynamic.annotation.Param;
import org.devcourse.resumeme.business.comment.domain.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {

List<Comment> findAllByResumeId(Long resumeId);

@Modifying
@Query("update Comment c SET c.componentId = :newComponentId where c.componentId = :oldComponentId")
void updateComment(@Param("newComponentId") Long newComponentId, @Param("oldComponentId") Long oldComponentId);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.devcourse.resumeme.business.resume.service;

import lombok.RequiredArgsConstructor;
import org.devcourse.resumeme.business.comment.controller.dto.CommentResponse;
import org.devcourse.resumeme.business.comment.domain.Comment;
import org.devcourse.resumeme.business.comment.repository.CommentRepository;
import org.devcourse.resumeme.business.resume.entity.Component;
import org.devcourse.resumeme.business.resume.repository.ComponentRepository;
import org.devcourse.resumeme.global.exception.CustomException;
Expand All @@ -20,6 +23,8 @@ public class ComponentService {

private final ComponentRepository componentRepository;

private final CommentRepository commentRepository;

public Long create(Component block, String type) {
Long resumeId = block.getResumeId();
Optional<Component> existBlock1 = componentRepository.findExistBlock(resumeId, type);
Expand Down Expand Up @@ -61,7 +66,10 @@ public Long update(Long componentId, Component newComponent, String type) {

newComponent.updateOriginDate(component.getCreatedDate());

return create(newComponent, type);
Long newComponentId = create(newComponent, type);
commentRepository.updateComment(newComponentId, componentId);

return newComponentId;
}

public void copy(Long originResumeId, Long newResumeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.devcourse.resumeme.business.event.domain.Event;
import org.devcourse.resumeme.business.event.domain.EventInfo;
import org.devcourse.resumeme.business.event.domain.EventTimeInfo;
import org.devcourse.resumeme.business.event.domain.MenteeToEvent;
import org.devcourse.resumeme.business.resume.domain.Resume;
import org.devcourse.resumeme.business.resume.entity.Component;
import org.devcourse.resumeme.business.user.domain.Provider;
Expand Down Expand Up @@ -91,7 +90,7 @@ void setUp() {
Component component = new Component("career", "career", null, null, resumeId, List.of());
Resume resume = new Resume("titlem", mentee);

Comment comment = request.toEntity(resume, component);
Comment comment = request.toEntity(resume);
setId(comment, 1L);
setId(component, 1L);
Field lastModifiedAt = comment.getClass().getSuperclass().getDeclaredField("lastModifiedDate");
Expand Down Expand Up @@ -142,7 +141,7 @@ void setUp() {
event.acceptMentee(1L, 1L);

given(eventService.getOne(eventId)).willReturn(event);
Comment comment = new Comment("리뷰 내용내용", component, new Resume("title", mentee));
Comment comment = new Comment("리뷰 내용내용", 1L, new Resume("title", mentee));
setId(comment, 1L);
setId(component, 1L);
Field lastModifiedAt = comment.getClass().getSuperclass().getDeclaredField("lastModifiedDate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@ class CommentTest {
Component component = new Component("career", "career", null, null, 1L, List.of());

// when
Comment comment = new Comment(content, component, resume);
Comment comment = new Comment(content, 1L, resume);

// then
assertThat(comment).isNotNull();
}

@ParameterizedTest
@MethodSource("reviewCreate")
void 이벤트포지션_생성_검증조건_미달로인해_생성에_실패한다_null입력_오류(String content, BlockType type, Component component, Resume resume) {
void 이벤트포지션_생성_검증조건_미달로인해_생성에_실패한다_null입력_오류(String content, Resume resume) {
// then
assertThatThrownBy(() -> new Comment(content, component, resume))
assertThatThrownBy(() -> new Comment(content, 1L, resume))
.isInstanceOf(CustomException.class);
}

Expand All @@ -76,15 +76,11 @@ static Stream<Arguments> reviewCreate() {

Resume resume = new Resume("title", mentee);

Component component = new Component("career", "career", null, null, 1L, List.of());

return Stream.of(
Arguments.of(null, null, component, null),
Arguments.of(content, null, component, null),
Arguments.of(null, type, component, null),
Arguments.of(null, null, component, resume),
Arguments.of("", type, component, null),
Arguments.of(" ", null, null, resume)
Arguments.of(null, null),
Arguments.of(content, null),
Arguments.of(null, resume),
Arguments.of("", null)
);
}

Expand Down

0 comments on commit 2c56600

Please sign in to comment.