Skip to content

Commit

Permalink
feat : 당일 조회 api 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunWoo9930 committed Nov 20, 2024
1 parent 5f0e23c commit a1457fe
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,16 @@ List<Orders> findOrdersByStoreAndDateAndStatusNative(
@Param("updatedAt") LocalDateTime updatedAt,
@Param("orderStatus") String orderStatus);

@Query("SELECT o FROM Orders o " +
"WHERE o.store.id = :storeId " +
"AND o.updatedAt >= :startOfDay " +
"AND o.updatedAt < :endOfDay " +
"AND o.orderStatus = :orderStatus")
List<Orders> findOrdersByStoreAndTodayDateAndStatus(
@Param("storeId") Long storeId,
@Param("startOfDay") LocalDateTime startOfDay,
@Param("endOfDay") LocalDateTime endOfDay,
@Param("orderStatus") OrderStatus orderStatus);


}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jangburich.domain.store.dto.condition.StoreSearchConditionWithType;
import com.jangburich.domain.store.dto.response.OrdersDetailResponse;
import com.jangburich.domain.store.dto.response.OrdersGetResponse;
import com.jangburich.domain.store.dto.response.OrdersTodayResponse;
import com.jangburich.domain.store.dto.response.PaymentGroupDetailResponse;
import com.jangburich.domain.store.dto.response.SearchStoresResponse;
import com.jangburich.domain.store.service.StoreService;
Expand Down Expand Up @@ -118,12 +119,19 @@ public ResponseCustom<?> getPaymentHistory(Authentication authentication, Pageab

@Operation(summary = "지난 주문 조회", description = "가게에 있는 지난 주문을 조회합니다")
@GetMapping("/orders/last")
public ResponseCustom<List<OrdersGetResponse>> getOrders(Authentication authentication) {
public ResponseCustom<List<OrdersGetResponse>> getLastOrders(Authentication authentication) {
List<OrdersGetResponse> ordersLast = storeService.getOrdersLast(
AuthenticationParser.parseUserId(authentication));
return ResponseCustom.OK(ordersLast);
}

@Operation(summary = "오늘 주문 조회", description = "가게에 있는 오늘 주문을 조회합니다")
@GetMapping("/orders/today")
public ResponseCustom<OrdersTodayResponse> getTodayOrders(Authentication authentication) {
return ResponseCustom.OK(storeService.getTodayOrders(
AuthenticationParser.parseUserId(authentication)));
}

@Operation(summary = "주문 상세 조회", description = "가게에 있는 주문을 상세 조회합니다")
@GetMapping("/orders/{ordersId}")
public ResponseCustom<OrdersDetailResponse> getOrders(Authentication authentication, @RequestParam Long orderId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.jangburich.domain.store.dto.response;

import java.util.List;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class OrdersTodayResponse {
private Integer totalPrice;
private List<OrdersGetResponse> ordersGetResponses;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.jangburich.domain.menu.domain.MenuCreateRequestDTO;
import com.jangburich.domain.menu.repository.MenuRepository;
import com.jangburich.domain.order.domain.Cart;
import com.jangburich.domain.order.domain.OrderStatus;
import com.jangburich.domain.order.domain.Orders;
import com.jangburich.domain.order.domain.repository.CartRepository;
import com.jangburich.domain.order.domain.repository.OrdersRepository;
Expand All @@ -35,6 +36,7 @@
import com.jangburich.domain.store.dto.condition.StoreSearchConditionWithType;
import com.jangburich.domain.store.dto.response.OrdersDetailResponse;
import com.jangburich.domain.store.dto.response.OrdersGetResponse;
import com.jangburich.domain.store.dto.response.OrdersTodayResponse;
import com.jangburich.domain.store.dto.response.PaymentGroupDetailResponse;
import com.jangburich.domain.store.dto.response.SearchStoresResponse;
import com.jangburich.domain.store.exception.OrdersNotFoundException;
Expand Down Expand Up @@ -274,10 +276,10 @@ public List<OrdersGetResponse> getOrdersLast(String userId) {
List<Cart> carts = cartRepository.findAllByOrders(orders);
newOrdersGetResponse.setId(orders.getId());
newOrdersGetResponse.setMenuNames(carts.size() == 1 ? carts.get(0).getMenu().getName() :
carts.get(0).getMenu().getName() + "외 " + (carts.size() - 1) + "개");
carts.get(0).getMenu().getName() + " 외 " + (carts.size() - 1) + "개");
newOrdersGetResponse.setCount(carts.size());
newOrdersGetResponse.setDate(orders.getUpdatedAt());
Integer price = 0;
int price = 0;
for (Cart cart : carts) {
price += (cart.getMenu().getPrice() * cart.getQuantity());
}
Expand All @@ -288,6 +290,49 @@ public List<OrdersGetResponse> getOrdersLast(String userId) {
return ordersGetResponses;
}

public OrdersTodayResponse getTodayOrders(String userId) {
List<OrdersGetResponse> ordersGetResponses = new ArrayList<>();

User user = userRepository.findByProviderId(userId)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

Owner owner = ownerRepository.findByUser(user)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

Store store = storeRepository.findByOwner(owner)
.orElseThrow(() -> new DefaultNullPointerException(ErrorCode.INVALID_AUTHENTICATION));

LocalDateTime startOfDay = LocalDate.now().atStartOfDay(); // 오늘 시작
LocalDateTime endOfDay = LocalDate.now().plusDays(1).atStartOfDay(); // 내일 시작 (오늘의 끝)

List<Orders> allByStore = ordersRepository.findOrdersByStoreAndTodayDateAndStatus(
store.getId(), startOfDay, endOfDay, OrderStatus.TICKET_USED);
int totalPrice = 0;
for (Orders orders : allByStore) {
OrdersGetResponse newOrdersGetResponse = new OrdersGetResponse();
List<Cart> carts = cartRepository.findAllByOrders(orders);

newOrdersGetResponse.setId(orders.getId());
newOrdersGetResponse.setMenuNames(carts.size() == 1 ? carts.get(0).getMenu().getName() :
carts.get(0).getMenu().getName() + " 외 " + (carts.size() - 1) + "개");
newOrdersGetResponse.setCount(carts.size());
newOrdersGetResponse.setDate(orders.getUpdatedAt());
int price = 0;
for (Cart cart : carts) {
price += (cart.getMenu().getPrice() * cart.getQuantity());
}
newOrdersGetResponse.setPrice(price);
totalPrice += price;
ordersGetResponses.add(newOrdersGetResponse);
}

OrdersTodayResponse ordersTodayResponse = new OrdersTodayResponse();
ordersTodayResponse.setOrdersGetResponses(ordersGetResponses);
ordersTodayResponse.setTotalPrice(totalPrice);

return ordersTodayResponse;
}

public OrdersDetailResponse getOrderDetails(String userId, Long orderId) {
OrdersDetailResponse ordersDetailResponse = new OrdersDetailResponse();

Expand Down

0 comments on commit a1457fe

Please sign in to comment.