Skip to content

Commit

Permalink
Merge pull request #6 from 9oormthon-univ/feature/#5
Browse files Browse the repository at this point in the history
[FEAT] 기업 리뷰 등록 API 구현
  • Loading branch information
haeun1107 authored Nov 18, 2024
2 parents 1a8f3e7 + c9341cf commit 522f053
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package danpoong.soenter.domain.enterprise;
package danpoong.soenter.domain.enterprise.controller;

import danpoong.soenter.base.ApiResponse;
import danpoong.soenter.domain.enterprise.service.EnterpriseService;
import danpoong.soenter.domain.enterprise.entity.Enterprise;
import danpoong.soenter.domain.enterprise.entity.Region;
import io.swagger.v3.oas.annotations.Operation;
Expand Down
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> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package danpoong.soenter.domain.enterprise;
package danpoong.soenter.domain.enterprise.service;

import danpoong.soenter.base.ApiResponse;
import danpoong.soenter.domain.enterprise.entity.Enterprise;
Expand Down
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);
}
}
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 src/main/java/danpoong/soenter/domain/review/dto/ReviewDTO.java
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ public class Review {

@OneToMany(mappedBy = "review", cascade = CascadeType.ALL, orphanRemoval = true)
private List<TagList> tagList;

public void updateTagNum(int count) {
this.tagNum = count;
}
}
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> {

}
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> {
}
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);
}
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);
}
}

0 comments on commit 522f053

Please sign in to comment.