Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/fcm order manage #73

Merged
merged 4 commits into from
Nov 22, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public enum ErrorCode {
ORDER_NOT_EXISTS_BY_ORDER_STATUS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "ํ•ด๋‹น ์ฃผ๋ฌธ ์ƒํƒœ์˜ ์ฃผ๋ฌธ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),
UNAUTHORIZED_ACCESS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "ํ•ด๋‹น ์ฃผ๋ฌธ์— ์ ‘๊ทผํ•  ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค."),
ORDER_STATUS_ALREADY_EXISTS_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์ด๋ฏธ ํ•ด๋‹น ์ฃผ๋ฌธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค."),
ALREADY_EXISTS_REVIEW_ERROR(false, HttpStatus.BAD_REQUEST.value(), "์ด๋ฏธ ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ•œ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค."),
;

private Boolean isSuccess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public String sendNotificationByToken(FCMRequestDto dto) {
Optional <User> user = userRepository.findById(dto.getUserId());

if (user.isPresent()){
log.info("fcmํ† ํฐ" + user.get().getFcmToken());
log.info("fcm token" + user.get().getFcmToken());
if (user.get().getFcmToken() != null) {

Message message = Message.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.kusitms.jipbap.notification;public class NotificationRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,11 @@ public CommonResponse<?> handleOrderStatusAlreadyExistsException(OrderStatusAlre
return new CommonResponse<>(ErrorCode.ORDER_STATUS_ALREADY_EXISTS_ERROR);
}

@ExceptionHandler(AlreadyExistsReviewException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public CommonResponse<?> handleAlreadyExistsReviewException(AlreadyExistsReviewException e, HttpServletRequest request) {
log.warn("ORDER-007> ์š”์ฒญ URI: " + request.getRequestURI() + ", ์—๋Ÿฌ ๋ฉ”์„ธ์ง€: " + e.getMessage());
return new CommonResponse<>(ErrorCode.ALREADY_EXISTS_REVIEW_ERROR);
}

}
9 changes: 2 additions & 7 deletions src/main/java/com/kusitms/jipbap/order/OrderRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kusitms.jipbap.food.Food;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand All @@ -10,13 +11,7 @@

public interface OrderRepository extends JpaRepository <Order, Long> {

// @Query("SELECT o.orderDetail.food, SUM(o.orderCount) as totalSales FROM Order o WHERE o.regionId= :regionId GROUP BY o.food ORDER BY totalSales DESC, MAX(o.createdAt) DESC")
// List<Food> findTop10BestSellingFoodsInRegion(@Param("regionId") Long regionId);

Optional<List<Order>> findByStore_IdAndStatus(Long storeId, OrderStatus status);

// @Query("SELECT o.food FROM Order o WHERE o.regionId = :regionId ORDER BY o.id DESC LIMIT 4")
// List<Food> findLatestFoodsByRegionId(@Param("regionId") Long regionId);
Optional<List<Order>> findByStore_IdAndStatus(Long storeId, OrderStatus status, Sort sort);

Optional<List<Order>> findByUser_Id(Long userId);

Expand Down
31 changes: 24 additions & 7 deletions src/main/java/com/kusitms/jipbap/order/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.kusitms.jipbap.food.exception.FoodOptionNotExistsException;
import com.kusitms.jipbap.notification.FCMNotificationService;
import com.kusitms.jipbap.notification.FCMRequestDto;
import com.kusitms.jipbap.notification.NotificationRepository;
import com.kusitms.jipbap.order.dto.*;
import com.kusitms.jipbap.order.exception.*;
import com.kusitms.jipbap.store.Store;
Expand All @@ -18,6 +19,7 @@
import com.kusitms.jipbap.user.exception.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -35,6 +37,7 @@ public class OrderService {
private final UserRepository userRepository;
private final FoodRepository foodRepository;
private final FoodOptionRepository foodOptionRepository;
private final NotificationRepository notificationRepository;
private final StoreRepository storeRepository;
private final FCMNotificationService fcmNotificationService;

Expand Down Expand Up @@ -107,7 +110,7 @@ public OwnerOrderStatusResponse getStoreOrderHistoryByOrderStatus(String email,

OrderStatus status = OrderStatus.fromString(orderStatus);

List<Order> orderList = orderRepository.findByStore_IdAndStatus(store.getId(), status)
List<Order> orderList = orderRepository.findByStore_IdAndStatus(store.getId(), status, Sort.by(Sort.Direction.DESC, "createdAt"))
.orElseThrow(() -> new OrderNotExistsByOrderStatusException("ํ•ด๋‹น ๊ฐ€๊ฒŒ์˜ ์ฃผ๋ฌธ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ฃผ๋ฌธ ๋‚ด์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

List<OrderPreviewResponse> orderPreviewResponses = orderList.stream()
Expand Down Expand Up @@ -136,13 +139,27 @@ public void processOrder(String email, Long orderId, String status) {
if(order.getStatus() == newStatus){
throw new OrderStatusAlreadyExistsException("์ด๋ฏธ ํ•ด๋‹น ์ฃผ๋ฌธ ์ƒํƒœ์ž…๋‹ˆ๋‹ค.");
}
order.setStatus(newStatus); // ์ฃผ๋ฌธ ์ƒํƒœ ๋ณ€๊ฒฝ

// ์•Œ๋ฆผ ๋“ฑ ๋กœ์ง ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
FCMRequestDto dto = new FCMRequestDto(seller.getId(), "์ฃผ๋ฌธ์ด ๋“ค์–ด์™”์Šต๋‹ˆ๋‹ค", "์ฃผ๋ฌธ์„ ํ™•์ธํ•ด์ฃผ์„ธ์š”.");
String ans = fcmNotificationService.sendNotificationByToken(dto);
log.info(ans);
order.setStatus(newStatus); // ์ฃผ๋ฌธ ์ƒํƒœ ๋ณ€๊ฒฝ
orderRepository.save(order);

User buyer = order.getUser();
if(newStatus.equals(OrderStatus.ACCEPTED)) { //ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ์ˆ˜๋ฝํ•จ
FCMRequestDto dto = new FCMRequestDto(buyer.getId(), "๊ฐ€๊ฒŒ๊ฐ€ ์ฃผ๋ฌธ์„ ์ˆ˜๋ฝํ–ˆ์Šต๋‹ˆ๋‹ค.", "๋ง›์žˆ๋Š” ํ•œ์‹ ์ง‘๋ฐฅ์ด ๊ณง ์ฐพ์•„๊ฐ‘๋‹ˆ๋‹ค!");
String ans = fcmNotificationService.sendNotificationByToken(dto);
log.info("ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ์ˆ˜๋ฝ, ๊ตฌ๋งค์ž์—๊ฒŒ ์•Œ๋ฆผ ์ „์†ก ๊ฒฐ๊ณผ: " + ans);

}
else if(newStatus.equals(OrderStatus.REJECTED)) { //ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ์ทจ์†Œํ•จ
FCMRequestDto dto = new FCMRequestDto(buyer.getId(), "๊ฐ€๊ฒŒ๊ฐ€ ์ฃผ๋ฌธ์„ ์ทจ์†Œํ–ˆ์Šต๋‹ˆ๋‹ค.", "๋‹ค๋ฅธ ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ•ด ์ฃผ์„ธ์š”.");
String ans = fcmNotificationService.sendNotificationByToken(dto);
log.info("ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ๊ฑฐ์ ˆ, ๊ตฌ๋งค์ž์—๊ฒŒ ์•Œ๋ฆผ ์ „์†ก ๊ฒฐ๊ณผ: " + ans);
}
else if(newStatus.equals(OrderStatus.COMPLETED)) { //ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ์™„๋ฃŒํ•จ
FCMRequestDto dto = new FCMRequestDto(buyer.getId(), "์Œ์‹์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.", "ํ•œ์‹ ์ง‘๋ฐฅ, ๋ง›์žˆ๊ฒŒ ์ฆ๊ธฐ์„ธ์š”!");
String ans = fcmNotificationService.sendNotificationByToken(dto);
log.info("ํŒ๋งค์ž๊ฐ€ ์ฃผ๋ฌธ์„ ์™„๋ฃŒ, ๊ตฌ๋งค์ž์—๊ฒŒ ์•Œ๋ฆผ ์ „์†ก ๊ฒฐ๊ณผ: " + ans);
}
}

public List<OrderHistoryResponse> getMyOrderHistory(String email) {
Expand All @@ -168,7 +185,7 @@ public StoreProcessingResponse getStoreProcessingOrder(String email) {
.orElseThrow(() -> new StoreNotExistsException("ํ•ด๋‹น ์œ ์ €์˜ ๊ฐ€๊ฒŒ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."));

//์ „์ฒด ์ฃผ๋ฌธ๋‚ด์—ญ์—์„œ ํ•ด๋‹น ๊ฐ€๊ฒŒ์— ์†ํ•˜๋Š” ์ฃผ๋ฌธ๋‚ด์—ญ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ
List<Order> orderList = orderRepository.findByStore_IdAndStatus(store.getId(), OrderStatus.ACCEPTED)
List<Order> orderList = orderRepository.findByStore_IdAndStatus(store.getId(), OrderStatus.ACCEPTED, Sort.by(Sort.Direction.DESC, "createdAt"))
.orElseThrow(() -> new OrderNotExistsByOrderStatusException("ํ•ด๋‹น ๊ฐ€๊ฒŒ์˜ ์ฃผ๋ฌธ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ฃผ๋ฌธ ๋‚ด์—ญ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

//์ฃผ๋ฌธ๋‚ด์—ญ ์ค‘์—์„œ ์Œ์‹๋ณ„๋กœ ๋ฌถ๊ธฐ
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/kusitms/jipbap/order/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.kusitms.jipbap.common.entity.DateEntity;
import com.kusitms.jipbap.food.Food;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.user.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -29,4 +30,5 @@ public class Review extends DateEntity {
private Long rating;
private String message;
private String image;

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

import com.kusitms.jipbap.store.Store;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ReviewRepository extends JpaRepository <Review, Long>, ReviewRepositoryExtension {
List<Review> findAllReviewsByOrder_Store(Store store);
}
42 changes: 29 additions & 13 deletions src/main/java/com/kusitms/jipbap/order/ReviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import com.amazonaws.services.s3.AmazonS3;
import com.kusitms.jipbap.common.exception.S3RegisterFailureException;
import com.kusitms.jipbap.common.utils.S3Utils;
import com.kusitms.jipbap.notification.FCMNotificationService;
import com.kusitms.jipbap.notification.FCMRequestDto;
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.AlreadyExistsReviewException;
import com.kusitms.jipbap.order.exception.OrderNotExistsException;
import com.kusitms.jipbap.store.Store;
import com.kusitms.jipbap.store.StoreRepository;
Expand Down Expand Up @@ -33,6 +36,7 @@ public class ReviewService {
private final UserRepository userRepository;
private final OrderRepository orderRepository;
private final StoreRepository storeRepository;
private final FCMNotificationService fcmNotificationService;

private final AmazonS3 amazonS3;

Expand All @@ -41,12 +45,19 @@ public class ReviewService {

@Transactional
public ReviewDto registerReview(String email, RegisterReviewRequestDto dto, MultipartFile image) {
userRepository.findByEmail(email).orElseThrow(()-> new UserNotFoundException("์œ ์ € ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
Order order = orderRepository.findById(dto.getOrderId()).orElseThrow(()-> new OrderNotExistsException("orderId: "+dto.getOrderId()+"์— ํ•ด๋‹นํ•˜๋Š” ์ฃผ๋ฌธ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
userRepository.findByEmail(email)
.orElseThrow(()-> new UserNotFoundException("์œ ์ € ์ •๋ณด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

Order order = orderRepository.findById(dto.getOrderId())
.orElseThrow(()-> new OrderNotExistsException("orderId: "+dto.getOrderId()+"์— ํ•ด๋‹นํ•˜๋Š” ์ฃผ๋ฌธ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));

if(order.getReview() != null){
throw new AlreadyExistsReviewException("์ด๋ฏธ ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ•œ ์ฃผ๋ฌธ์ž…๋‹ˆ๋‹ค.");
}

Store store = order.getStore();

String imageUri = null;

// ์ด๋ฏธ์ง€๊ฐ€ null์ด ์•„๋‹Œ ๊ฒฝ์šฐ s3 ์—…๋กœ๋“œ
if(image!=null) {
try {
Expand All @@ -56,13 +67,18 @@ public ReviewDto registerReview(String email, RegisterReviewRequestDto dto, Mult
}
}

// ๋ฆฌ๋ทฐ ์ž‘์„ฑ
Review review = reviewRepository.save(new Review(null, order, dto.getRating(), dto.getMessage(), imageUri));
// ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์—…๋ฐ์ดํŠธ
store.increaseReviewCount();
// ํ‰์  ์—…๋ฐ์ดํŠธ
store.updateAvgRate(Double.parseDouble(String.valueOf(dto.getRating())));

return new ReviewDto(review.getId(), review.getOrder().getId(), review.getRating(), review.getMessage(), review.getImage());
store.increaseReviewCount(); // ๋ฆฌ๋ทฐ ๊ฐœ์ˆ˜ ์—…๋ฐ์ดํŠธ
store.updateAvgRate(Double.parseDouble(String.valueOf(dto.getRating()))); // ํ‰์  ์—…๋ฐ์ดํŠธ

// ์ฃผ๋ฌธ๋‚ด์—ญ์— ๋ฆฌ๋ทฐ ์ถ”๊ฐ€ ๋ฐ ํŒ๋งค์ž์—๊ฒŒ ๋ฆฌ๋ทฐ ์ž‘์„ฑ๋˜์—ˆ๋‹ค๋Š” ์•Œ๋ฆผ ์ „์†ก
order.setReview(review);
FCMRequestDto fcmRequestDto = new FCMRequestDto(store.getOwner().getId(), "๊ณ ๊ฐ์ด ๋ฆฌ๋ทฐ๋ฅผ ์ž‘์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.", "๋‚ด์šฉ์„ ํ™•์ธํ•ด ๋ณด์‹œ๊ฒ ์–ด์š”?");
String ans = fcmNotificationService.sendNotificationByToken(fcmRequestDto);
log.info("๊ตฌ๋งค์ž๊ฐ€ ๋ฆฌ๋ทฐ ์ž‘์„ฑ ์™„๋ฃŒ ์•Œ๋ฆผ ์ „์†ก: " + ans);
return new ReviewDto(review.getId(), review.getOrder().getId(), review.getOrder().getUser().getUsername(), review.getCreatedAt().toString(), review.getOrder().getOrderDetail().get(0).getFood().getName(), review.getRating(), review.getMessage(), review.getImage());
}

@Transactional
Expand All @@ -73,20 +89,20 @@ public GetRegisteredReviewsResponseDto getUserRegisteredReviews(String email) {

return new GetRegisteredReviewsResponseDto(
reviews.stream()
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getRating(), r.getMessage(), r.getImage()))
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getOrder().getUser().getUsername(), r.getCreatedAt().toString(), r.getOrder().getOrderDetail().get(0).getFood().getName(), r.getRating(), r.getMessage(), r.getImage()))
.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);
Store store = storeRepository.findById(storeId)
.orElseThrow(()-> new StoreNotExistsException("storeId: "+storeId+"์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ€๊ฒŒ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."));
List<Review> reviews = reviewRepository.findAllReviewsByOrder_Store(store);

return new GetRegisteredReviewsResponseDto(
reviews.stream()
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getRating(), r.getMessage(), r.getImage()))
.map(r -> new ReviewDto(r.getId(), r.getOrder().getId(), r.getOrder().getUser().getUsername(), r.getCreatedAt().toString(), r.getOrder().getOrderDetail().get(0).getFood().getName(), r.getRating(), r.getMessage(), r.getImage()))
.collect(Collectors.toList())
);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/kusitms/jipbap/order/dto/ReviewDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
public class ReviewDto {

private Long id;

private Long orderId;

private String writerName;
private String time;
private String foodName;
private Long rating;

private String message;
private String image;

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

public class AlreadyExistsReviewException extends RuntimeException {
public AlreadyExistsReviewException(String message) { super(message); }
}
Loading