Skip to content

Commit

Permalink
Feat: 주문 정보 전체 조회(#15)
Browse files Browse the repository at this point in the history
close: #15
  • Loading branch information
mummhy0811 committed Apr 30, 2024
1 parent 9e733e1 commit 914b478
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/main/java/com/hanaro/starbucks/controller/OrderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.hanaro.starbucks.controller;

import com.hanaro.starbucks.dto.orders.OrderResDto;
import com.hanaro.starbucks.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/orders")
public class OrderController {
private final OrderService orderService;

@GetMapping("")
public List<OrderResDto> getOrders() {
return orderService.getOrders();
}


}
22 changes: 22 additions & 0 deletions src/main/java/com/hanaro/starbucks/dto/orders/OrderResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.hanaro.starbucks.dto.orders;


import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;


@Builder
@Getter
public class OrderResDto {
private int orderIdx;

private String orderId;
private Integer totalPrice;

private String orderStatus;

private LocalDate orderDate;

}
2 changes: 2 additions & 0 deletions src/main/java/com/hanaro/starbucks/entity/OrderDetail.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.hanaro.starbucks.entity;

import jakarta.persistence.*;
import lombok.Getter;
import org.hibernate.annotations.ColumnDefault;

@Entity
@Getter
@Table(name = "order_detail")
public class OrderDetail {

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/hanaro/starbucks/entity/Orders.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hanaro.starbucks.entity;

import jakarta.persistence.*;
import lombok.Getter;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;

@Entity
@Getter
@Table(name = "orders")
public class Orders {

Expand All @@ -21,7 +23,7 @@ public class Orders {
private String orderStatus;

@Column(name = "order_date")
private LocalDateTime orderDate;
private LocalDate orderDate;

@ManyToOne
@JoinColumn(name = "user_idx")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.hanaro.starbucks.repository;

import com.hanaro.starbucks.entity.OrderDetail;
import com.hanaro.starbucks.entity.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderDetailRepository extends JpaRepository<OrderDetail, Integer> {
List<OrderDetail> findAllByOrders(Orders orders);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hanaro.starbucks.repository;

import com.hanaro.starbucks.entity.Orders;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface OrderRepository extends JpaRepository<Orders, Integer> {
}
53 changes: 53 additions & 0 deletions src/main/java/com/hanaro/starbucks/service/OrderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.hanaro.starbucks.service;

import com.hanaro.starbucks.dto.orders.OrderResDto;
import com.hanaro.starbucks.entity.OrderDetail;
import com.hanaro.starbucks.entity.Orders;
import com.hanaro.starbucks.repository.OrderDetailRepository;
import com.hanaro.starbucks.repository.OrderRepository;
import com.hanaro.starbucks.util.Constant;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class OrderService {
private final OrderRepository orderRepository;
private final OrderDetailRepository orderDetailRepository;

public List<OrderResDto> getOrders() {
List<Orders> orders = orderRepository.findAll();
List<OrderResDto> orderResDtos = orders.stream()
.map(order -> {
List<OrderDetail> orderDetails = orderDetailRepository.findAllByOrders(order);
int totalPrice = calculateTotalPrice(orderDetails);
return OrderResDto.builder()
.orderIdx(order.getOrderIdx())
.orderId(order.getOrderId())
.totalPrice(totalPrice)
.orderStatus(order.getOrderStatus())
.orderDate(order.getOrderDate())
.build();
})
.collect(Collectors.toList());
return orderResDtos;
}

private int calculateTotalPrice(List<OrderDetail> orderDetails) {
int totalPrice = 0;
for (OrderDetail orderDetail : orderDetails) {
int menuPrice = orderDetail.getMenu().getMenuPrice();
if (orderDetail.getMenuSize().equals(Constant.GRANDE_SIZE)) {
menuPrice += Constant.GRANDE;
} else if (orderDetail.getMenuSize().equals(Constant.VENTI_SIZE)) {
menuPrice += Constant.VENTI;
}
totalPrice += menuPrice * orderDetail.getOrderDetailCount();
}
return totalPrice;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/hanaro/starbucks/util/Constant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hanaro.starbucks.util;

public class Constant {
public static final String GRANDE_SIZE="grande";
public static final String VENTI_SIZE="venti";
public static final int GRANDE = 500;
public static final int VENTI = 1000;

}

0 comments on commit 914b478

Please sign in to comment.