Skip to content

Commit

Permalink
[BE] refactor: 리뷰 작성을 위한 정보 요청 기능 리팩터링 (#162)
Browse files Browse the repository at this point in the history
* refactor: 불필요한 update 방지

* feat: controller에 리뷰 생성 요청에 대한 응답 기능 구현

* feat: controller에 리뷰 작성을 위해 필요한 정보를 응답 기능 구현

* feat: service에 리뷰 작성을 위해 필요한 정보 조회
 기능 구현

* chore: 사용하지 않는 dto 삭제

* test: 리뷰 리뷰 작성을 위해 필요한 정보 조회 기능 테스트 작성

* refactor: swagger 설정 일부 수정 및 에러 응답코드 노출되도록 변경

* refactor: service에서 원시타입 long을 반환하도록 변경

* docs: api 문서 dto 항목설명 변경

* test: 사용하지 않는 변수 선언하지 않도록 변경

* docs: api 문서 검증 내용에 대한 어체 변경

* refactor: 트랜잭션 조회 명시 추가

* refactor: dto 이름 변경
  • Loading branch information
Kimprodp authored Aug 1, 2024
1 parent 94602db commit 125f85f
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 89 deletions.
64 changes: 33 additions & 31 deletions backend/src/main/java/reviewme/review/controller/ReviewApi.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
//package reviewme.review.controller;
//
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.responses.ApiResponse;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestParam;
//import reviewme.review.dto.request.CreateReviewRequest;
//import reviewme.review.dto.response.ReceivedReviewsResponse;
//import reviewme.review.dto.response.ReviewCreationResponse;
//import reviewme.review.dto.response.ReviewDetailResponse;
//
//@Tag(name = "리뷰 관리")
//public interface ReviewApi {
//
// @Operation(
// summary = "리뷰 등록",
// description = "리뷰 작성 정보를 받아 리뷰를 등록한다.",
// responses = @ApiResponse(responseCode = "201")
// )
// ResponseEntity<Void> createReview(@RequestBody CreateReviewRequest request);
//
package reviewme.review.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import reviewme.review.dto.request.CreateReviewRequest;
import reviewme.review.dto.response.ReviewSetupResponse;

@Tag(name = "리뷰 관리")
public interface ReviewApi {

@Operation(summary = "리뷰 등록", description = "리뷰 작성 정보를 받아 리뷰를 등록한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "리뷰 등록 요청 성공")
})
ResponseEntity<Void> createReview(@RequestBody CreateReviewRequest request);

@Operation(summary = "리뷰 작성 정보 조회", description = "리뷰 생성 시 필요한 정보를 조회한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "리뷰 폼 응답 성공",
content = @Content(schema = @Schema(implementation = ReviewSetupResponse.class))),
@ApiResponse(responseCode = "400", description = "응답 실패 : 올바르지 않은 리뷰 요청 코드",
content = @Content)
})
ResponseEntity<ReviewSetupResponse> findReviewCreationSetup(@RequestParam String reviewRequestCode);

// @Operation(
// summary = "리뷰 조회",
// description = "단일 리뷰를 조회한다."
Expand All @@ -35,10 +43,4 @@
// ResponseEntity<ReceivedReviewsResponse> findMyReceivedReview(@RequestParam long memberId,
// @RequestParam(required = false) Long lastReviewId,
// @RequestParam(defaultValue = "10") int size);
//
// @Operation(
// summary = "리뷰 작성 정보 조회",
// description = "리뷰 생성 시 필요한 정보를 조회한다."
// )
// ResponseEntity<ReviewCreationResponse> findReviewCreationSetup(@RequestParam long reviewerGroupId);
//}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package reviewme.review.controller;

import jakarta.validation.Valid;
import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reviewme.review.dto.request.CreateReviewRequest;
import reviewme.review.dto.response.ReviewSetupResponse;
import reviewme.review.service.ReviewService;

@RestController
@RequiredArgsConstructor
public class ReviewController implements ReviewApi{

private final ReviewService reviewService;

@PostMapping("/reviews")
public ResponseEntity<Void> createReview(@Valid @RequestBody CreateReviewRequest request) {
long savedReviewId = reviewService.createReview(request);
return ResponseEntity.created(URI.create("/reviews/" + savedReviewId)).build();
}

@GetMapping("/reviews/write")
public ResponseEntity<ReviewSetupResponse> findReviewCreationSetup(@RequestParam String reviewRequestCode) {
ReviewSetupResponse response = reviewService.findReviewCreationSetup(reviewRequestCode);
return ResponseEntity.ok(response);
}
}
2 changes: 1 addition & 1 deletion backend/src/main/java/reviewme/review/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Review {
private long reviewGroupId;

@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "review_id", nullable = false)
@JoinColumn(name = "review_id", nullable = false, updatable = false)
private List<ReviewContent> reviewContents;

@Column(name = "created_at", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

@Schema(description = "리뷰 내용 등록 요청")
@Schema(name = "리뷰 내용 등록 요청")
public record CreateReviewContentRequest(

@Schema(description = "리뷰 문항")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package reviewme.review.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;

@Schema(name = "리뷰 작성 요청")
public record CreateReviewRequest(

@NotBlank
@Schema(description = "리뷰 요청 코드")
@NotBlank(message = "리뷰 요청 코드가 필요해요.")
String reviewRequestCode,

@NotNull
@Schema(description = "리뷰 답변 목록")
@NotNull(message = "리뷰 답변이 없어요.")
List<CreateReviewContentRequest> reviewContents,

@NotNull
@Schema(description = "선택 키워드 ID 목록")
@NotNull(message = "선택된 키워드가 없어요.")
List<Long> keywords
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "리뷰 문항 응답")
public record QuestionResponse(
@Schema(name = "리뷰 문항 응답")
public record QuestionSetupResponse(

@Schema(description = "리뷰 문항 ID")
long id,
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package reviewme.keyword.dto.response;
package reviewme.review.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "키워드 응답")
public record KeywordResponse(
@Schema(name = "키워드 응답")
public record ReviewSetUpKeyword(

@Schema(description = "키워드 ID")
long id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package reviewme.review.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import java.util.List;

@Schema(name = "리뷰 작성 폼 응답")
public record ReviewSetupResponse(

@Schema(description = "리뷰이 이름")
String revieweeName,

@Schema(description = "프로젝트명")
String projectName,

@Schema(description = "리뷰 문항 목록")
List<QuestionSetupResponse> questions,

@Schema(description = "키워드 목록")
List<ReviewSetUpKeyword> keywords
) {
}
30 changes: 30 additions & 0 deletions backend/src/main/java/reviewme/review/service/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reviewme.review.dto.response.ReviewSetUpKeyword;
import reviewme.keyword.repository.KeywordRepository;
import reviewme.review.domain.Review;
import reviewme.review.domain.ReviewContent;
import reviewme.review.domain.ReviewKeyword;
import reviewme.review.dto.request.CreateReviewContentRequest;
import reviewme.review.dto.request.CreateReviewRequest;
import reviewme.review.dto.response.QuestionSetupResponse;
import reviewme.review.dto.response.ReviewSetupResponse;
import reviewme.review.repository.QuestionRepository;
import reviewme.review.repository.ReviewContentRepository;
import reviewme.review.repository.ReviewKeywordRepository;
import reviewme.review.repository.ReviewRepository;
Expand All @@ -25,6 +30,9 @@ public class ReviewService {
private final ReviewKeywordRepository reviewKeywordRepository;
private final ReviewContentRepository reviewContentRepository;
private final ReviewGroupRepository reviewGroupRepository;
private final QuestionRepository questionRepository;
private final KeywordRepository keywordRepository;

private final ReviewCreationQuestionValidator reviewCreationQuestionValidator;
private final ReviewCreationKeywordValidator reviewCreationKeywordValidator;

Expand Down Expand Up @@ -63,4 +71,26 @@ private void saveReviewKeywords(List<Long> selectedKeywordIds, long savedReviewI
.toList();
reviewKeywordRepository.saveAll(reviewKeywords);
}

@Transactional(readOnly = true)
public ReviewSetupResponse findReviewCreationSetup(String reviewRequestCode) {
ReviewGroup reviewGroup = reviewGroupRepository.findByReviewRequestCode(reviewRequestCode)
.orElseThrow(InvalidReviewRequestCodeException::new);
return createReviewSetupResponse(reviewGroup);
}

private ReviewSetupResponse createReviewSetupResponse(ReviewGroup reviewGroup) {
List<QuestionSetupResponse> questionSetupRespons = questionRepository.findAll()
.stream()
.map(question -> new QuestionSetupResponse(question.getId(), question.getContent()))
.toList();

List<ReviewSetUpKeyword> reviewSetUpKeywordRespons = keywordRepository.findAll()
.stream()
.map(keyword -> new ReviewSetUpKeyword(keyword.getId(), keyword.getContent()))
.toList();

return new ReviewSetupResponse(reviewGroup.getReviewee(), reviewGroup.getProjectName(),
questionSetupRespons, reviewSetUpKeywordRespons);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import reviewme.review.domain.Review;
import reviewme.review.dto.request.CreateReviewContentRequest;
import reviewme.review.dto.request.CreateReviewRequest;
import reviewme.review.dto.response.ReviewSetupResponse;
import reviewme.review.repository.QuestionRepository;
import reviewme.review.repository.ReviewContentRepository;
import reviewme.review.repository.ReviewKeywordRepository;
Expand Down Expand Up @@ -79,4 +80,29 @@ class ReviewServiceTest {
() -> assertThat(reviewKeywordRepository.findAllByReviewId(reviewId)).hasSize(2)
);
}

@Test
void 리뷰_작성을_위해_필요한_정보를_조회한다() {
// given
keywordRepository.save(추진력이_좋아요.create());
keywordRepository.save(회의를_이끌어요.create());
questionRepository.save(소프트스킬이_어떤가요.create());
questionRepository.save(기술역량이_어떤가요.create());

String reviewee = "테드";
String projectName = "리뷰미 프로젝트";
String reviewRequestCode = "reviewRequestCode";
reviewGroupRepository.save(new ReviewGroup(reviewee, projectName, reviewRequestCode, "groupAccessCode"));

// when
ReviewSetupResponse reviewCreationSetup = reviewService.findReviewCreationSetup(reviewRequestCode);

// then
assertAll(
() -> assertThat(reviewCreationSetup.revieweeName()).isEqualTo(reviewee),
() -> assertThat(reviewCreationSetup.projectName()).isEqualTo(projectName),
() -> assertThat(reviewCreationSetup.questions()).hasSize(2),
() -> assertThat(reviewCreationSetup.keywords()).hasSize(2)
);
}
}

0 comments on commit 125f85f

Please sign in to comment.