source: src/main/java/tech/techharbor/Service/Impl/DeliveryServiceImpl.java

main
Last change on this file was f4b4afa, checked in by Nikola Todoroski <nikola.todoroski@…>, 6 months ago

Pushed whole project, original project location on github:https://github.com/hehxd/Tech-Harbor

  • Property mode set to 100644
File size: 2.2 KB
Line 
1package tech.techharbor.Service.Impl;
2
3import jakarta.transaction.Transactional;
4import org.springframework.stereotype.Service;
5import tech.techharbor.Model.DeliveryModel;
6import tech.techharbor.Model.Exceptions.DeliveryNotFoundException;
7import tech.techharbor.Repository.DeliveryRepository;
8import tech.techharbor.Service.DeliveryService;
9
10import java.util.List;
11
12@Service
13public 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}
Note: See TracBrowser for help on using the repository browser.