-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] 기업 리뷰 등록 API 구현
- Loading branch information
Showing
11 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
...main/enterprise/EnterpriseController.java → ...rise/controller/EnterpriseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/danpoong/soenter/domain/enterprise/repository/VisitRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package danpoong.soenter.domain.enterprise.repository; | ||
|
||
import danpoong.soenter.domain.enterprise.entity.Visit; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface VisitRepository extends JpaRepository<Visit, Long> { | ||
} |
2 changes: 1 addition & 1 deletion
2
.../domain/enterprise/EnterpriseService.java → ...enterprise/service/EnterpriseService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/danpoong/soenter/domain/review/controller/ReviewController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package danpoong.soenter.domain.review.controller; | ||
|
||
import danpoong.soenter.base.ApiResponse; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest; | ||
import danpoong.soenter.domain.review.service.ReviewService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/api/reviews") | ||
@RequiredArgsConstructor | ||
@Tag(name = "Review Controller", description = "리뷰 관련 API") | ||
public class ReviewController { | ||
|
||
private final ReviewService reviewService; | ||
|
||
@PostMapping | ||
@Operation(summary = "리뷰 등록 API", description = "사용자가 특정 기업에 대한 리뷰를 등록합니다.") | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공") | ||
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON400", description = "잘못된 요청입니다.") | ||
public ApiResponse<PostReviewResponse> createReview(@RequestBody @Valid PostReviewRequest postReviewRequest, Authentication authentication) { | ||
PostReviewResponse response = reviewService.createReview(postReviewRequest, authentication.getName()); | ||
return ApiResponse.onSuccess(response); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/danpoong/soenter/domain/review/converter/ReviewConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package danpoong.soenter.domain.review.converter; | ||
|
||
import danpoong.soenter.domain.enterprise.entity.Enterprise; | ||
import danpoong.soenter.domain.enterprise.entity.Visit; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest; | ||
import danpoong.soenter.domain.review.entity.Review; | ||
import danpoong.soenter.domain.review.entity.TagList; | ||
import danpoong.soenter.domain.user.entity.User; | ||
|
||
import java.time.LocalDate; | ||
|
||
public class ReviewConverter { | ||
public static Visit toVisit(User user, Enterprise enterprise, LocalDate visitDate) { | ||
return Visit.builder() | ||
.user(user) | ||
.enterprise(enterprise) | ||
.visitDate(visitDate) | ||
.build(); | ||
} | ||
|
||
public static Review toReview(User user, Enterprise enterprise, PostReviewRequest requestDto) { | ||
return Review.builder() | ||
.user(user) | ||
.enterprise(enterprise) | ||
.title(requestDto.getTitle()) | ||
.content(requestDto.getContent()) | ||
.createAt(LocalDate.now()) | ||
.build(); | ||
} | ||
|
||
public static TagList toTagList(Review review, Integer tagNum) { | ||
return TagList.builder() | ||
.review(review) | ||
.tagNum(tagNum) | ||
.build(); | ||
} | ||
|
||
public static PostReviewResponse toReviewResponseDto(Review review) { | ||
return PostReviewResponse.builder() | ||
.reviewId(review.getReviewId()) | ||
.title(review.getTitle()) | ||
.content(review.getContent()) | ||
.createAt(review.getCreateAt()) | ||
.build(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/danpoong/soenter/domain/review/dto/ReviewDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package danpoong.soenter.domain.review.dto; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
public class ReviewDTO { | ||
public static class ReviewRequest { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class PostReviewRequest { | ||
@NotNull | ||
private Long enterpriseId; | ||
private String title; | ||
private String content; | ||
private LocalDate visitDate; | ||
private List<Integer> tagNumbers; | ||
|
||
} | ||
} | ||
|
||
public static class ReviewResponse { | ||
@Getter | ||
@Builder | ||
public static class PostReviewResponse { | ||
private Long reviewId; | ||
private String title; | ||
private String content; | ||
private LocalDate createAt; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/danpoong/soenter/domain/review/repository/ReviewRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package danpoong.soenter.domain.review.repository; | ||
|
||
import danpoong.soenter.domain.review.entity.Review; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ReviewRepository extends JpaRepository<Review, Long> { | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/danpoong/soenter/domain/review/repository/TagListRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package danpoong.soenter.domain.review.repository; | ||
|
||
import danpoong.soenter.domain.review.entity.TagList; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface TagListRepository extends JpaRepository<TagList, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/danpoong/soenter/domain/review/service/ReviewService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package danpoong.soenter.domain.review.service; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest; | ||
|
||
public interface ReviewService { | ||
public ReviewDTO.ReviewResponse.PostReviewResponse createReview(PostReviewRequest postReviewRequest, String userId); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/danpoong/soenter/domain/review/service/ReviewServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package danpoong.soenter.domain.review.service; | ||
|
||
import danpoong.soenter.domain.enterprise.entity.Enterprise; | ||
import danpoong.soenter.domain.enterprise.entity.Visit; | ||
import danpoong.soenter.domain.enterprise.repository.EnterpriseRepository; | ||
import danpoong.soenter.domain.enterprise.repository.VisitRepository; | ||
import danpoong.soenter.domain.review.converter.ReviewConverter; | ||
import danpoong.soenter.domain.review.entity.Review; | ||
import danpoong.soenter.domain.review.entity.TagList; | ||
import danpoong.soenter.domain.review.repository.ReviewRepository; | ||
import danpoong.soenter.domain.review.repository.TagListRepository; | ||
import danpoong.soenter.domain.user.entity.User; | ||
import danpoong.soenter.domain.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewRequest.PostReviewRequest; | ||
import danpoong.soenter.domain.review.dto.ReviewDTO.ReviewResponse.PostReviewResponse; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ReviewServiceImpl implements ReviewService { | ||
|
||
private final ReviewRepository reviewRepository; | ||
private final VisitRepository visitRepository; | ||
private final UserRepository userRepository; | ||
private final EnterpriseRepository enterpriseRepository; | ||
private final TagListRepository tagListRepository; | ||
|
||
@Transactional | ||
public PostReviewResponse createReview(PostReviewRequest postReviewRequest, String userId) { | ||
User user = userRepository.findById(Long.valueOf(userId)) | ||
.orElseThrow(() -> new RuntimeException("존재하지 않는 사용자입니다.")); | ||
Enterprise enterprise = enterpriseRepository.findById(postReviewRequest.getEnterpriseId()) | ||
.orElseThrow(() -> new RuntimeException("존재하지 않는 기업입니다.")); | ||
|
||
// Visit 기록 저장 | ||
Visit visit = ReviewConverter.toVisit(user, enterprise, postReviewRequest.getVisitDate()); | ||
visitRepository.save(visit); | ||
|
||
// Review 저장 | ||
Review review = ReviewConverter.toReview(user, enterprise, postReviewRequest); | ||
reviewRepository.save(review); | ||
|
||
// TagList 저장 | ||
postReviewRequest.getTagNumbers().forEach(tagNum -> { | ||
TagList tag = ReviewConverter.toTagList(review, tagNum); | ||
tagListRepository.save(tag); | ||
}); | ||
|
||
// 저장된 TagList 개수 계산 후 Review의 tagNum 업데이트 | ||
int tagCount = postReviewRequest.getTagNumbers().size(); | ||
review.updateTagNum(tagCount); | ||
reviewRepository.save(review); | ||
|
||
return ReviewConverter.toReviewResponseDto(review); | ||
} | ||
} |