Skip to content

Commit

Permalink
❇️ 리뷰 공개 여부 변경 기능 개선
Browse files Browse the repository at this point in the history
기존에는 공개 여부 boolean 값을 요청으로 받아 해당 값으로 설정하였음. 요청에서 특정 값을 받지 않고 현재 저장된 값의 반대로 전환한 후, 그 결과를 응답으로 보내도록 개선함.
na-yk committed Nov 20, 2023
1 parent 4bbc8c8 commit f433455
Showing 5 changed files with 31 additions and 27 deletions.
11 changes: 5 additions & 6 deletions src/main/java/dev/steady/review/controller/ReviewController.java
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@
import dev.steady.global.auth.Auth;
import dev.steady.global.auth.UserInfo;
import dev.steady.review.dto.request.ReviewCreateRequest;
import dev.steady.review.dto.request.ReviewUpdateRequest;
import dev.steady.review.dto.response.ReviewMyResponse;
import dev.steady.review.dto.response.ReviewSwitchResponse;
import dev.steady.review.service.ReviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
@@ -38,12 +38,11 @@ public ResponseEntity<Void> createReview(@PathVariable Long steadyId,
}

@PatchMapping("/reviews/{reviewId}")
public ResponseEntity<Void> updateReviewIsPublic(@PathVariable Long reviewId,
@RequestBody ReviewUpdateRequest request,
@Auth UserInfo userInfo) {
reviewService.updateReviewIsPublic(reviewId, request, userInfo);
public ResponseEntity<ReviewSwitchResponse> updateReviewIsPublic(@PathVariable Long reviewId,
@Auth UserInfo userInfo) {
ReviewSwitchResponse response = reviewService.switchReviewIsPublic(reviewId, userInfo);

return ResponseEntity.ok().build();
return ResponseEntity.ok(response);
}

@GetMapping("/reviews/my")
14 changes: 11 additions & 3 deletions src/main/java/dev/steady/review/service/ReviewService.java
Original file line number Diff line number Diff line change
@@ -10,8 +10,8 @@
import dev.steady.review.domain.repository.ReviewRepository;
import dev.steady.review.domain.repository.UserCardRepository;
import dev.steady.review.dto.request.ReviewCreateRequest;
import dev.steady.review.dto.request.ReviewUpdateRequest;
import dev.steady.review.dto.response.ReviewMyResponse;
import dev.steady.review.dto.response.ReviewSwitchResponse;
import dev.steady.review.dto.response.ReviewsBySteadyResponse;
import dev.steady.review.dto.response.UserCardResponse;
import dev.steady.review.infrastructure.ReviewQueryRepository;
@@ -85,14 +85,22 @@ public void createUserCards(ReviewCreateRequest request) {
userCardRepository.saveAll(userCards);
}

/**
* 리뷰의 공개 여부(isPublic)을 변경하고 그 결과를 반환한다.
*
* @param reviewId 리뷰 식별자
* @param userInfo 사용자 정보
* @return 리뷰 공개 여부 변경된 결과
*/
@Transactional
public void updateReviewIsPublic(Long reviewId, ReviewUpdateRequest request, UserInfo userInfo) {
public ReviewSwitchResponse switchReviewIsPublic(Long reviewId, UserInfo userInfo) {
Review review = reviewRepository.getById(reviewId);
Participant reviewee = review.getReviewee();
if (!Objects.equals(reviewee.getUserId(), userInfo.userId())) {
throw new ForbiddenException(REVIEW_AUTH_FAILURE);
}
review.updateIsPublic(request.isPublic());
review.switchIsPublic();
return ReviewSwitchResponse.from(review);
}

@Transactional(readOnly = true)
Original file line number Diff line number Diff line change
@@ -11,9 +11,8 @@
import static dev.steady.global.auth.AuthFixture.createUserInfo;
import static dev.steady.review.fixture.ReviewFixture.createReviewCreateRequest;
import static dev.steady.review.fixture.ReviewFixture.createReviewMyResponse;
import static dev.steady.review.fixture.ReviewFixture.createReviewUpdateRequest;
import static dev.steady.review.fixture.ReviewFixture.createReviewSwitchResponse;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willDoNothing;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName;
@@ -77,24 +76,24 @@ void updateReviewIsPublicTest() throws Exception {
var auth = new Authentication(userId);
given(jwtResolver.getAuthentication(TOKEN)).willReturn(auth);

var request = createReviewUpdateRequest(false);
var reviewId = 1L;
willDoNothing().given(reviewService).updateReviewIsPublic(reviewId, request, userInfo);
var response = createReviewSwitchResponse(true);
given(reviewService.switchReviewIsPublic(reviewId, userInfo)).willReturn(response);

// when, then
mockMvc.perform(patch("/api/v1/reviews/{reviewId}", reviewId)
.contentType(APPLICATION_JSON)
.header(AUTHORIZATION, TOKEN)
.content(objectMapper.writeValueAsString(request))
)
.andDo(document("review-v1-update",
resourceDetails().tag("리뷰").description("리뷰 공개 여부 수정")
.requestSchema(Schema.schema("ReviewUpdateRequest")),
requestFields(
fieldWithPath("isPublic").type(BOOLEAN).description("수정할 리뷰 공개 상태")
.responseSchema(Schema.schema("ReviewSwitchRepsonse")),
responseFields(
fieldWithPath("isPublic").type(BOOLEAN).description("수정된 리뷰 공개 여부 상태")
)
))
.andExpect(status().isOk());
.andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(response)));
}

@Test
6 changes: 3 additions & 3 deletions src/test/java/dev/steady/review/fixture/ReviewFixture.java
Original file line number Diff line number Diff line change
@@ -4,9 +4,9 @@
import dev.steady.review.domain.Review;
import dev.steady.review.domain.UserCard;
import dev.steady.review.dto.request.ReviewCreateRequest;
import dev.steady.review.dto.request.ReviewUpdateRequest;
import dev.steady.review.dto.response.ReviewDetailResponse;
import dev.steady.review.dto.response.ReviewMyResponse;
import dev.steady.review.dto.response.ReviewSwitchResponse;
import dev.steady.review.dto.response.ReviewsBySteadyResponse;
import dev.steady.review.dto.response.UserCardResponse;
import dev.steady.steady.domain.Participant;
@@ -63,8 +63,8 @@ public static ReviewCreateRequest createReviewCreateRequest() {
);
}

public static ReviewUpdateRequest createReviewUpdateRequest(boolean isPublic) {
return new ReviewUpdateRequest(isPublic);
public static ReviewSwitchResponse createReviewSwitchResponse(boolean isPublic) {
return new ReviewSwitchResponse(isPublic);
}

public static ReviewMyResponse createReviewMyResponse() {
10 changes: 4 additions & 6 deletions src/test/java/dev/steady/review/service/ReviewServiceTest.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import dev.steady.review.domain.repository.ReviewRepository;
import dev.steady.review.domain.repository.UserCardRepository;
import dev.steady.review.dto.response.ReviewMyResponse;
import dev.steady.review.dto.response.ReviewSwitchResponse;
import dev.steady.review.infrastructure.ReviewQueryRepository;
import dev.steady.review.infrastructure.UserCardQueryRepository;
import dev.steady.steady.domain.repository.ParticipantRepository;
@@ -33,7 +34,6 @@
import static dev.steady.review.fixture.ReviewFixture.createCard;
import static dev.steady.review.fixture.ReviewFixture.createReview;
import static dev.steady.review.fixture.ReviewFixture.createReviewCreateRequest;
import static dev.steady.review.fixture.ReviewFixture.createReviewUpdateRequest;
import static dev.steady.review.fixture.ReviewFixture.createUserCard;
import static dev.steady.steady.domain.Participant.createMember;
import static dev.steady.steady.domain.SteadyStatus.FINISHED;
@@ -187,22 +187,20 @@ void createUserCardsTest() {

@Test
@DisplayName("리뷰이는 본인의 리뷰 코멘트를 비공개로 설정할 수 있다.")
void updateReviewIsPublicTest() {
void switchReviewIsPublicTest() {
// given
var userInfo = createUserInfo(revieweeUser.getId());
var steady = steadyRepository.save(createSteady(leader, stacks, FINISHED));
var reviewer = participantRepository.save(createMember(reviewerUser, steady));
var reviewee = participantRepository.save(createMember(revieweeUser, steady));
var review = reviewRepository.save(createReview(reviewer, reviewee, steady));

var request = createReviewUpdateRequest(false);

// when
reviewService.updateReviewIsPublic(review.getId(), request, userInfo);
ReviewSwitchResponse response = reviewService.switchReviewIsPublic(review.getId(), userInfo);

// then
Review foundReview = reviewRepository.getById(review.getId());
assertThat(foundReview.isPublic()).isEqualTo(request.isPublic());
assertThat(foundReview.isPublic()).isEqualTo(response.isPublic());
}

@Test

0 comments on commit f433455

Please sign in to comment.