Skip to content

Commit

Permalink
Merge pull request #20 from Digital-Hana-Starbucks/feature/admin-orders
Browse files Browse the repository at this point in the history
Feat: 관리자 페이지 주문 관련 API
  • Loading branch information
mummhy0811 authored Apr 30, 2024
2 parents 9e733e1 + 5efb1c6 commit 8b51efc
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 2 deletions.
36 changes: 36 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,36 @@
package com.hanaro.starbucks.controller;

import com.hanaro.starbucks.dto.orders.OrderEditReqDto;
import com.hanaro.starbucks.dto.orders.OrderResDto;
import com.hanaro.starbucks.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

@GetMapping("")
public List<OrderResDto> getOrders() {
return orderService.getOrders();
}
@GetMapping("/{orderIdx}")
public OrderResDto getOrder(@PathVariable int orderIdx) {
return orderService.getOrder(orderIdx);
}

@PutMapping("/{orderIdx}")
public void updateOrder(@PathVariable int orderIdx, @RequestBody OrderEditReqDto orderEditReqDto) {
orderService.updateOrder(orderIdx, orderEditReqDto);
}

@DeleteMapping("/{orderIdx}")
public void deleteOrder(@PathVariable int orderIdx) {
orderService.deleteOrder(orderIdx);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hanaro.starbucks.dto.orders;

import lombok.Getter;

@Getter
public class OrderEditReqDto {
private String orderStatus;
}
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
10 changes: 8 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,12 +23,16 @@ public class Orders {
private String orderStatus;

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

@ManyToOne
@JoinColumn(name = "user_idx")
private User user;

@OneToMany(mappedBy = "orders", cascade = CascadeType.ALL)
private List<OrderDetail> orderDetails;

public void updateOrderStatus(String orderStatus){
this.orderStatus=orderStatus;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hanaro.starbucks.repository;

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

@Repository
public interface OrderDetailRepository extends JpaRepository<OrderDetail, Integer> {
}
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> {
}
81 changes: 81 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,81 @@
package com.hanaro.starbucks.service;

import com.hanaro.starbucks.dto.orders.OrderEditReqDto;
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.List;
import java.util.Optional;
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 = order.getOrderDetails();
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;
}

public OrderResDto getOrder(int orderIdx) {
Orders order = findOrderById(orderIdx);
return OrderResDto.builder()
.orderIdx(order.getOrderIdx())
.orderId(order.getOrderId())
.totalPrice(calculateTotalPrice(order.getOrderDetails()))
.orderStatus(order.getOrderStatus())
.orderDate(order.getOrderDate())
.build();
}

public void updateOrder(int orderIdx, OrderEditReqDto orderEditReqDto) {
Orders order = findOrderById(orderIdx);
order.updateOrderStatus(orderEditReqDto.getOrderStatus());
orderRepository.save(order);
}

public void deleteOrder(int orderIdx){
Orders order = findOrderById(orderIdx);
order.getOrderDetails().forEach(orderDetailRepository::delete);
orderRepository.deleteById(orderIdx);
}
private Orders findOrderById(int orderIdx) {
Optional<Orders> optionalOrders = orderRepository.findById(orderIdx);
return optionalOrders.orElseThrow(() -> new IllegalArgumentException("주문 내역이 존재하지 않습니다."));
}

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 8b51efc

Please sign in to comment.