Skip to content

Commit

Permalink
✨ feat: 리뷰 작성 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tmddus2 committed Aug 4, 2024
1 parent c74ca8f commit 312968b
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import io.swagger.v3.oas.annotations.servers.Server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@OpenAPIDefinition(
servers = {
@Server(url = "/", description = "Default Server URL")
}
)
@SpringBootApplication
@EnableJpaAuditing
public class PurithmApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.purithm.domain.review.controller;

import com.example.purithm.domain.review.dto.request.ReviewRequestDto;
import com.example.purithm.domain.review.dto.response.CreatedReviewDto;
import com.example.purithm.domain.review.dto.response.ReviewResponseDto;
import com.example.purithm.domain.review.service.ReviewService;
import com.example.purithm.global.response.SuccessResponse;
Expand All @@ -23,7 +25,7 @@ public SuccessResponse<ReviewResponseDto> getReviewDetail(Long id, Long reviewId
}

@PostMapping
public SuccessResponse<Void> writeReview(Long id, ReviewResponseDto request) {
return null;
public SuccessResponse<CreatedReviewDto> writeReview(Long id, ReviewRequestDto request) {
return SuccessResponse.of(reviewService.writeReview(id, request));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.purithm.domain.review.controller;

import com.example.purithm.domain.review.dto.request.ReviewRequestDto;
import com.example.purithm.domain.review.dto.response.CreatedReviewDto;
import com.example.purithm.domain.review.dto.response.ReviewResponseDto;
import com.example.purithm.global.auth.annotation.LoginInfo;
import com.example.purithm.global.response.SuccessResponse;
Expand All @@ -15,5 +17,5 @@ public interface ReviewControllerDocs {

@Operation(summary = "리뷰를 작성합니다.")
@ApiResponse(responseCode = "200", description = "리뷰 작성 성공")
public SuccessResponse<Void> writeReview(@LoginInfo Long id, @RequestBody ReviewResponseDto request);
public SuccessResponse<CreatedReviewDto> writeReview(@LoginInfo Long id, @RequestBody ReviewRequestDto request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;

public record ReviewRequestDto(
@Schema(description = "필터 id")
Long filterId,
@Schema(description = "퓨어 지수")
int pureDegree,
@Schema(description = "리뷰 내용")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.purithm.domain.review.dto.response;

import lombok.Builder;

@Builder
public record CreatedReviewDto(
Long id
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
Expand All @@ -15,15 +16,24 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Temporal;
import jakarta.persistence.TemporalType;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Builder
@AllArgsConstructor
@EntityListeners(AuditingEntityListener.class)
public class Review {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -41,9 +51,11 @@ public class Review {

@Convert(converter = StringListConverter.class)
private List<String> pictures;

@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
@Column(updatable = false)
private Date createdAt;

int pureDegree;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

import org.springframework.stereotype.Service;

import com.example.purithm.domain.filter.entity.Filter;
import com.example.purithm.domain.filter.repository.FilterRepository;
import com.example.purithm.domain.review.dto.request.ReviewRequestDto;
import com.example.purithm.domain.review.dto.response.CreatedReviewDto;
import com.example.purithm.domain.review.dto.response.ReviewResponseDto;
import com.example.purithm.domain.review.entity.Review;
import com.example.purithm.domain.review.repository.ReviewRepository;
import com.example.purithm.domain.user.entity.User;
import com.example.purithm.domain.user.repository.UserRepository;
import com.example.purithm.global.exception.CustomException;
import com.example.purithm.global.exception.Error;

Expand All @@ -15,6 +21,8 @@
public class ReviewService {

private final ReviewRepository reviewRepository;
private final UserRepository userRepository;
private final FilterRepository filterRepository;

public ReviewResponseDto getReview(Long id) {
Review review = reviewRepository.findById(id)
Expand All @@ -29,4 +37,23 @@ public ReviewResponseDto getReview(Long id) {
.pureDegree(review.getPureDegree())
.build();
}

public CreatedReviewDto writeReview(Long id, ReviewRequestDto request) {
User user = userRepository.findById(id)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));
Filter filter = filterRepository.findById(request.filterId())
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));

Review review = Review.builder()
.user(user)
.filter(filter)
.review(request.content())
.pictures(request.picture())
.pureDegree(request.pureDegree())
.build();
Review savedReview = reviewRepository.save(review);

return CreatedReviewDto.builder()
.id(savedReview.getId()).build();
}
}

0 comments on commit 312968b

Please sign in to comment.