Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] issue344: 리뷰 리팩토링 #348

Merged
merged 15 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.woowacourse.moamoa.common.entity.BaseEntity;
import com.woowacourse.moamoa.referenceroom.service.exception.NotLinkAuthorException;
import com.woowacourse.moamoa.referenceroom.service.exception.NotRelatedLinkException;
import com.woowacourse.moamoa.review.domain.AssociatedStudy;
import com.woowacourse.moamoa.studyroom.domain.review.AssociatedStudy;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class LinkDao {

public Slice<LinkData> findAllByStudyId(final Long studyId, final Pageable pageable) {
final String sql = "SELECT link.id, link.link_url, link.description, link.created_date, link.last_modified_date, "
+ "member.github_id, member.username, member.image_url, member.profile_url "
+ "member.id member_id, member.username, member.image_url, member.profile_url "
+ "FROM link "
+ "JOIN member ON link.member_id = member.id "
+ "WHERE link.deleted = false "
Expand Down Expand Up @@ -55,11 +55,11 @@ private RowMapper<LinkData> rowMapper() {
final LocalDate createdDate = rs.getObject("created_date", LocalDate.class);
final LocalDate lastModifiedDate = rs.getObject("last_modified_date", LocalDate.class);

final Long githubId = rs.getLong("github_id");
final Long memberId = rs.getLong("member_id");
final String username = rs.getString("username");
final String imageUrl = rs.getString("image_url");
final String profileUrl = rs.getString("profile_url");
final MemberData memberData = new MemberData(githubId, username, imageUrl, profileUrl);
final MemberData memberData = new MemberData(memberId, username, imageUrl, profileUrl);

return new LinkData(id, memberData, linkUrl, description, createdDate, lastModifiedDate);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.woowacourse.moamoa.referenceroom.service.exception.NotParticipatedMemberException;
import com.woowacourse.moamoa.referenceroom.service.request.CreatingLinkRequest;
import com.woowacourse.moamoa.referenceroom.service.request.EditingLinkRequest;
import com.woowacourse.moamoa.review.domain.AssociatedStudy;
import com.woowacourse.moamoa.studyroom.domain.review.AssociatedStudy;
import com.woowacourse.moamoa.study.domain.Study;
import com.woowacourse.moamoa.study.domain.repository.StudyRepository;
import com.woowacourse.moamoa.study.service.exception.StudyNotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.woowacourse.moamoa.referenceroom.domain.Author;
import com.woowacourse.moamoa.referenceroom.domain.Link;
import com.woowacourse.moamoa.review.domain.AssociatedStudy;
import com.woowacourse.moamoa.studyroom.domain.review.AssociatedStudy;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.woowacourse.moamoa.referenceroom.domain.Author;
import com.woowacourse.moamoa.referenceroom.domain.Link;
import com.woowacourse.moamoa.review.domain.AssociatedStudy;
import com.woowacourse.moamoa.studyroom.domain.review.AssociatedStudy;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.AllArgsConstructor;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,6 @@ public Study(
this.attachedTags = attachedTags;
}

public boolean isReviewWritable(final Long memberId) {
return participants.isParticipation(memberId) && !studyPlanner.isPreparing();
}

public boolean isParticipant(final Long memberId) {
return participants.isParticipation(memberId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.woowacourse.moamoa.review.controller;
package com.woowacourse.moamoa.studyroom.controller;

import com.woowacourse.moamoa.auth.config.AuthenticatedMemberId;
import com.woowacourse.moamoa.review.service.ReviewService;
import com.woowacourse.moamoa.review.service.request.EditingReviewRequest;
import com.woowacourse.moamoa.review.service.request.WriteReviewRequest;
import com.woowacourse.moamoa.studyroom.service.ReviewService;
import com.woowacourse.moamoa.studyroom.service.request.review.ReviewRequest;
import java.net.URI;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand All @@ -27,28 +26,30 @@ public class ReviewController {
public ResponseEntity<Void> writeReview(
@AuthenticatedMemberId final Long memberId,
@PathVariable(name = "study-id") final Long studyId,
@Valid @RequestBody final WriteReviewRequest writeReviewRequest
@Valid @RequestBody final ReviewRequest reviewRequest
) {
final Long id = reviewService.writeReview(memberId, studyId, writeReviewRequest);
final Long id = reviewService.writeReview(memberId, studyId, reviewRequest);
return ResponseEntity.created(URI.create("/api/studies/" + studyId + "/reviews/" + id)).build();
}

@PutMapping("/{review-id}")
public ResponseEntity<Void> updateReview(
@AuthenticatedMemberId final Long memberId,
@PathVariable(name = "study-id") final Long studyId,
@PathVariable(name = "review-id") final Long reviewId,
@Valid @RequestBody final EditingReviewRequest editingReviewRequest
@Valid @RequestBody final ReviewRequest reviewRequest
) {
reviewService.updateReview(memberId, reviewId, editingReviewRequest);
reviewService.updateReview(memberId, studyId, reviewId, reviewRequest);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{review-id}")
public ResponseEntity<Void> deleteReview(
@AuthenticatedMemberId final Long memberId,
@PathVariable(name = "study-id") final Long studyId,
@PathVariable(name = "review-id") final Long reviewId
) {
reviewService.deleteReview(memberId, reviewId);
reviewService.deleteReview(memberId, studyId, reviewId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.woowacourse.moamoa.review.controller;
package com.woowacourse.moamoa.studyroom.controller;

import com.woowacourse.moamoa.review.service.SearchingReviewService;
import com.woowacourse.moamoa.review.service.request.SizeRequest;
import com.woowacourse.moamoa.review.service.response.ReviewsResponse;
import com.woowacourse.moamoa.studyroom.service.SearchingReviewService;
import com.woowacourse.moamoa.studyroom.service.request.review.SizeRequest;
import com.woowacourse.moamoa.studyroom.service.response.review.ReviewsResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woowacourse.moamoa.review.controller.converter;
package com.woowacourse.moamoa.studyroom.controller.converter;

import com.woowacourse.moamoa.review.service.request.SizeRequest;
import com.woowacourse.moamoa.studyroom.service.request.review.SizeRequest;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public Accessor(final Long memberId, final Long studyId) {
this.studyId = studyId;
}

Long getStudyId() {
public Long getStudyId() {
return studyId;
}

Long getMemberId() {
public Long getMemberId() {
return memberId;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.woowacourse.moamoa.studyroom.domain;

import com.woowacourse.moamoa.member.service.exception.NotParticipatedMemberException;
import com.woowacourse.moamoa.studyroom.domain.review.AssociatedStudy;
import com.woowacourse.moamoa.studyroom.domain.review.Review;
import com.woowacourse.moamoa.studyroom.domain.review.Reviewer;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Column;
Expand Down Expand Up @@ -48,6 +51,13 @@ public Article write(final Accessor accessor, final String title, final String c
throw new NotParticipatedMemberException();
}

public Review writeReview(final Accessor accessor, final String content) {
if (!isPermittedAccessor(accessor)) {
sc0116 marked this conversation as resolved.
Show resolved Hide resolved
throw new NotParticipatedMemberException();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아마 StudyRoom에 UneditableArticleException이 있을 건데 이거와 합치는게 좋을 듯 합니다. 당연히 이름도 Article을 빼서 적절한 이름으로 변경해야겠죠 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 합치는건 좋은데 리뷰 작성시 예외 발생하는 경우에 "수정할 수 없는 아티클 예외"를 사용하는게 맞나용?

}
return new Review(new AssociatedStudy(studyId), new Reviewer(accessor.getMemberId()), content);
}

sc0116 marked this conversation as resolved.
Show resolved Hide resolved
@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -64,5 +74,4 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(studyId);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woowacourse.moamoa.review.domain.repository;
package com.woowacourse.moamoa.studyroom.domain.repository.review;

import com.woowacourse.moamoa.review.domain.Review;
import com.woowacourse.moamoa.studyroom.domain.review.Review;
import org.springframework.data.jpa.repository.JpaRepository;

public interface JpaReviewRepository extends JpaRepository<Review, Long>, ReviewRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woowacourse.moamoa.review.domain.repository;
package com.woowacourse.moamoa.studyroom.domain.repository.review;

import com.woowacourse.moamoa.review.domain.Review;
import com.woowacourse.moamoa.studyroom.domain.review.Review;
import java.util.Optional;

public interface ReviewRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacourse.moamoa.review.domain;
package com.woowacourse.moamoa.studyroom.domain.review;

import static lombok.AccessLevel.PROTECTED;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.woowacourse.moamoa.review.domain;
package com.woowacourse.moamoa.studyroom.domain.review;

import static javax.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

import com.woowacourse.moamoa.common.entity.BaseEntity;
import com.woowacourse.moamoa.review.service.exception.UnwrittenReviewException;
import com.woowacourse.moamoa.studyroom.domain.Accessor;
import com.woowacourse.moamoa.studyroom.domain.review.exception.ReviewNotWrittenInTheStudyException;
import com.woowacourse.moamoa.studyroom.domain.review.exception.UnwrittenReviewException;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
Expand Down Expand Up @@ -44,21 +46,28 @@ public Review(
this(null, associatedStudy, reviewer, content, false);
}

public static Review writeNewReview(Long studyId, Long memberId, String content) {
return new Review(new AssociatedStudy(studyId), new Reviewer(memberId), content);
public void updateContent(final Accessor accessor, final String content) {
validateReview(new AssociatedStudy(accessor.getStudyId()), new Reviewer(accessor.getMemberId()));
this.content = content;
}

public void updateContent(final Reviewer reviewer, final String content) {
validateReviewer(reviewer);
this.content = content;
public void delete(final Accessor accessor) {
validateReview(new AssociatedStudy(accessor.getStudyId()), new Reviewer(accessor.getMemberId()));
deleted = true;
}

public void delete(final Reviewer reviewer) {
private void validateReview(final AssociatedStudy associatedStudy, final Reviewer reviewer) {
validateReviewWrittenInTheStudy(associatedStudy);
validateReviewer(reviewer);
deleted = true;
}

public void validateReviewer(final Reviewer reviewer) {
private void validateReviewWrittenInTheStudy(final AssociatedStudy associatedStudy) {
if (!this.associatedStudy.equals(associatedStudy)) {
throw new ReviewNotWrittenInTheStudyException();
}
}

private void validateReviewer(final Reviewer reviewer) {
if (!this.reviewer.equals(reviewer)) {
throw new UnwrittenReviewException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woowacourse.moamoa.review.domain;
package com.woowacourse.moamoa.studyroom.domain.review;

import javax.persistence.Column;
import javax.persistence.Embeddable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.woowacourse.moamoa.studyroom.domain.review.exception;

import com.woowacourse.moamoa.common.exception.BadRequestException;

public class ReviewNotWrittenInTheStudyException extends BadRequestException {

public ReviewNotWrittenInTheStudyException() {
super("해당 스터디에 작성된 후기가 아닙니다.");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.woowacourse.moamoa.review.service.exception;
package com.woowacourse.moamoa.studyroom.domain.review.exception;

import com.woowacourse.moamoa.common.exception.BadRequestException;

public class UnwrittenReviewException extends BadRequestException {
public UnwrittenReviewException() {

public UnwrittenReviewException() {
super("내가 작성한 후기가 아닙니다.");
}
}
Loading