Skip to content

Commit

Permalink
Merge pull request #26 from 28th-meetup/feat/mypage-1112
Browse files Browse the repository at this point in the history
[feat] 리뷰 관련 api 추가
  • Loading branch information
eckrin authored Nov 13, 2023
2 parents a7098d0 + 4985279 commit 1bfec4c
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public enum ErrorCode {

//store
STORE_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 가게입니다."),
STORE_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 가게입니다."),
STORE_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "이미 존재하는 가게이거나, 이미 가게를 생성한 유저입니다."),

//food
CATEGORY_NOT_FOUND_ERROR(false, HttpStatus.BAD_REQUEST.value(), "존재하지 않는 카테고리입니다."),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/CategoryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import jakarta.persistence.Column;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDto {

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kusitms/jipbap/food/dto/FoodDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class FoodDto {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.kusitms.jipbap.food.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RegisterCategoryRequestDto {
private String name;
private String image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import com.kusitms.jipbap.food.Category;
import com.kusitms.jipbap.store.Store;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RegisterFoodRequestDto {

private Long storeId;
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/OrderExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.common.response.ErrorCode;
import com.kusitms.jipbap.order.exception.OrderNotExistsException;
import com.kusitms.jipbap.store.exception.StoreExistsException;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class OrderExceptionHandler {
@ExceptionHandler(OrderNotExistsException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public CommonResponse<?> handleOrderNotExistsException(OrderNotExistsException e, HttpServletRequest request) {
log.warn("ORDER-001> 요청 URI: " + request.getRequestURI() + ", 에러 메세지: " + e.getMessage());
return new CommonResponse<>(ErrorCode.STORE_NOT_FOUND_ERROR);
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/ReviewController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.common.response.CommonResponse;
import com.kusitms.jipbap.order.dto.GetRegisteredReviewsResponseDto;
import com.kusitms.jipbap.order.dto.RegisterReviewRequestDto;
import com.kusitms.jipbap.order.dto.ReviewDto;
import com.kusitms.jipbap.security.Auth;
import com.kusitms.jipbap.security.AuthInfo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/review")
@RequiredArgsConstructor
public class ReviewController {

private final ReviewService reviewService;

@Operation(summary = "주문 리뷰 작성")
@PostMapping
public CommonResponse<ReviewDto> registerReview(@Auth AuthInfo authInfo, @RequestBody RegisterReviewRequestDto dto) {
return new CommonResponse<>(reviewService.registerReview(authInfo.getEmail(), dto));
}

@Operation(summary = "유저가 작성한 리뷰 모아보기")
@GetMapping
public CommonResponse<GetRegisteredReviewsResponseDto> getUserRegisteredReviews(@Auth AuthInfo authInfo) {
return new CommonResponse<>(reviewService.getUserRegisteredReviews(authInfo.getEmail()));
}

@Operation(summary = "가게 리뷰 모두보기")
@GetMapping("/{storeId}")
public CommonResponse<GetRegisteredReviewsResponseDto> getStoreRegisteredReviews(@PathVariable Long storeId) {
return new CommonResponse<>(reviewService.getStoreRegisteredReviews(storeId));
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/ReviewRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kusitms.jipbap.order;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ReviewRepository extends JpaRepository <Review, Long>, ReviewRepositoryExtension {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.user.User;

import java.util.List;

public interface ReviewRepositoryExtension {
List<Review> findAllReviewsByUser(User user);
List<Review> findAllReviewsByStore(Store store);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.user.User;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;

import static com.kusitms.jipbap.food.QFood.food;
import static com.kusitms.jipbap.order.QOrder.order;
import static com.kusitms.jipbap.order.QReview.review;

@RequiredArgsConstructor
public class ReviewRepositoryExtensionImpl implements ReviewRepositoryExtension {

private final JPAQueryFactory queryFactory;

@Override
public List<Review> findAllReviewsByUser(User user) {
return queryFactory.selectFrom(review)
.join(review.order, order)
.where(order.user.eq(user))
.fetch();
}

@Override
public List<Review> findAllReviewsByStore(Store store) {
return queryFactory.selectFrom(review)
.join(review.order, order)
.join(order.food, food)
.where(food.store.eq(store))
.fetch();
}

}
66 changes: 66 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/ReviewService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.kusitms.jipbap.order;

import com.kusitms.jipbap.order.dto.GetRegisteredReviewsResponseDto;
import com.kusitms.jipbap.order.dto.RegisterReviewRequestDto;
import com.kusitms.jipbap.order.dto.ReviewDto;
import com.kusitms.jipbap.order.exception.OrderNotExistsException;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.store.StoreRepository;
import com.kusitms.jipbap.store.exception.StoreNotExistsException;
import com.kusitms.jipbap.user.User;
import com.kusitms.jipbap.user.UserRepository;
import com.kusitms.jipbap.user.exception.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ReviewService {

private final ReviewRepository reviewRepository;
private final UserRepository userRepository;
private final OrderRepository orderRepository;
private final StoreRepository storeRepository;

@Transactional
public ReviewDto registerReview(String email, RegisterReviewRequestDto dto) {
userRepository.findByEmail(email).orElseThrow(()-> new UserNotFoundException("유저 정보가 존재하지 않습니다."));
Order order = orderRepository.findById(dto.getOrderId()).orElseThrow(()-> new OrderNotExistsException("orderId: "+dto.getOrderId()+"에 해당하는 주문이 존재하지 않습니다."));

Review review = reviewRepository.save(new Review(null, order, dto.getRating(), dto.getMessage()));

return new ReviewDto(review.getId(), review.getOrder().getId(), review.getRating(), review.getMessage());
}

@Transactional
public GetRegisteredReviewsResponseDto getUserRegisteredReviews(String email) {
User user = userRepository.findByEmail(email).orElseThrow(() -> new UserNotFoundException("유저 정보가 존재하지 않습니다."));

List<Review> reviews = reviewRepository.findAllReviewsByUser(user);

return new GetRegisteredReviewsResponseDto(
reviews.stream()
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getRating(), r.getMessage()))
.collect(Collectors.toList())
);
}

@Transactional
public GetRegisteredReviewsResponseDto getStoreRegisteredReviews(Long storeId) {
Store store = storeRepository.findById(storeId).orElseThrow(()-> new StoreNotExistsException("storeId: "+storeId+"에 해당하는 가게가 존재하지 않습니다."));

List<Review> reviews = reviewRepository.findAllReviewsByStore(store);

return new GetRegisteredReviewsResponseDto(
reviews.stream()
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getRating(), r.getMessage()))
.collect(Collectors.toList())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kusitms.jipbap.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class GetRegisteredReviewsResponseDto {
private List<ReviewDto> reviewList;
}
2 changes: 2 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/dto/OrderDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderDto {

private Long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class OrderFoodRequestDto {
private Long user;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.kusitms.jipbap.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class RegisterReviewRequestDto {
private Long orderId;
private Long rating;
private String message;
}
21 changes: 21 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/dto/ReviewDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.kusitms.jipbap.order.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ReviewDto {

private Long id;

private Long orderId;

private Long rating;

private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.kusitms.jipbap.order.exception;

public class OrderNotExistsException extends RuntimeException{
public OrderNotExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class BookmarkedStoreListResponseDto {

private List<StoreDto> stores;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class RegisterStoreRequestDto {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class StoreDetailResponseDto {

private StoreDto storeDto;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kusitms/jipbap/store/dto/StoreDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class StoreDto {

private Long id;
Expand Down
Loading

0 comments on commit 1bfec4c

Please sign in to comment.