1 | package tech.techharbor.Service.Impl;
|
---|
2 |
|
---|
3 | import org.springframework.stereotype.Service;
|
---|
4 | import tech.techharbor.Model.Exceptions.OrderNotFoundException;
|
---|
5 | import tech.techharbor.Model.OrderTableModel;
|
---|
6 | import tech.techharbor.Repository.OrderTableRepository;
|
---|
7 | import tech.techharbor.Service.OrderTableService;
|
---|
8 |
|
---|
9 | import java.util.Date;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class OrderTableServiceImpl implements OrderTableService {
|
---|
14 |
|
---|
15 | private final OrderTableRepository orderTableRepository;
|
---|
16 |
|
---|
17 | public OrderTableServiceImpl(OrderTableRepository orderTableRepository) {
|
---|
18 | this.orderTableRepository = orderTableRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public OrderTableModel findById(Integer id) {
|
---|
23 | return this.orderTableRepository.findById(id).orElseThrow(() -> new OrderNotFoundException(id));
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public OrderTableModel create(String orderStatus, Date date, Integer customerId) {
|
---|
28 | OrderTableModel order = new OrderTableModel(orderStatus, (java.sql.Date) date, customerId);
|
---|
29 | return this.orderTableRepository.save(order);
|
---|
30 | }
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public OrderTableModel update(Integer id, String orderStatus, Date date) {
|
---|
34 | OrderTableModel order = this.orderTableRepository.findById(id).orElseThrow(() -> new OrderNotFoundException(id));
|
---|
35 | order.setOrderStatus(orderStatus);
|
---|
36 | order.setOrderDate((java.sql.Date) date);
|
---|
37 | return this.orderTableRepository.save(order);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | public List<OrderTableModel> findByCustomerId(Integer customerId) {
|
---|
42 | return this.orderTableRepository.findByCustomerId(customerId);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void delete(Integer id) {
|
---|
47 | this.orderTableRepository.deleteById(id);
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | public void updateOrderStatus(Integer orderId, String newStatus) {
|
---|
52 | OrderTableModel order = orderTableRepository.findById(orderId).orElse(null);
|
---|
53 | if (order != null) {
|
---|
54 | order.setOrderStatus(newStatus);
|
---|
55 | orderTableRepository.save(order);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|