1 | package tech.techharbor.Service.Impl;
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional;
|
---|
4 | import org.springframework.stereotype.Service;
|
---|
5 | import tech.techharbor.Model.DeliveryModel;
|
---|
6 | import tech.techharbor.Model.Exceptions.DeliveryNotFoundException;
|
---|
7 | import tech.techharbor.Repository.DeliveryRepository;
|
---|
8 | import tech.techharbor.Service.DeliveryService;
|
---|
9 |
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | public class DeliveryServiceImpl implements DeliveryService {
|
---|
14 |
|
---|
15 | private final DeliveryRepository deliveryRepository;
|
---|
16 |
|
---|
17 | public DeliveryServiceImpl(DeliveryRepository deliveryRepository) {
|
---|
18 | this.deliveryRepository = deliveryRepository;
|
---|
19 | }
|
---|
20 |
|
---|
21 | @Override
|
---|
22 | public List<DeliveryModel> listDeliveries() {
|
---|
23 | return this.deliveryRepository.findAll();
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public DeliveryModel findById(Integer id) {
|
---|
28 | return this.deliveryRepository.findById(id).orElseThrow(() -> new DeliveryNotFoundException(id));
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public DeliveryModel create(String deliveryStatus, String deliveryAddress, Integer deliveryManId, Integer orderId) {
|
---|
33 | DeliveryModel delivery = new DeliveryModel(deliveryStatus, deliveryAddress, deliveryManId, orderId);
|
---|
34 | return this.deliveryRepository.save(delivery);
|
---|
35 | }
|
---|
36 |
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public DeliveryModel update(Integer id, String deliveryStatus, String deliveryAddress, Integer deliveryManId, Integer orderId) {
|
---|
40 | DeliveryModel delivery = this.deliveryRepository.findById(id).orElseThrow(() -> new DeliveryNotFoundException(id));
|
---|
41 | delivery.setDeliveryStatus(deliveryStatus);
|
---|
42 | delivery.setDeliveryAddress(deliveryAddress);
|
---|
43 | delivery.setDeliveryManId(deliveryManId);
|
---|
44 | delivery.setOrderId(orderId);
|
---|
45 | return this.deliveryRepository.save(delivery);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public List<DeliveryModel> findByDeliveryManId(Integer deliveryManId) {
|
---|
50 | return this.deliveryRepository.findByDeliveryManId(deliveryManId);
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Transactional
|
---|
54 | public void updateDeliveryStatus(Integer deliveryId, String newStatus) {
|
---|
55 | DeliveryModel delivery = deliveryRepository.findById(deliveryId).orElse(null);
|
---|
56 | if (delivery != null) {
|
---|
57 | delivery.setDeliveryStatus(newStatus);
|
---|
58 | deliveryRepository.save(delivery);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|