1 | package mk.ukim.finki.busngo.service.impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.*;
|
---|
4 | import mk.ukim.finki.busngo.model.enums.BILET_STATUS;
|
---|
5 | import mk.ukim.finki.busngo.model.enums.VOZENJE_STATUS;
|
---|
6 | import mk.ukim.finki.busngo.model.exceptions.InvalidVozenjeIdException;
|
---|
7 | import mk.ukim.finki.busngo.repository.VozenjeRepository;
|
---|
8 | import mk.ukim.finki.busngo.service.*;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.sql.Timestamp;
|
---|
12 | import java.time.LocalDateTime;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class VozenjeServiceImpl implements VozenjeService {
|
---|
17 | private final VozenjeRepository vozenjeRepository;
|
---|
18 | private final PatnikService patnikService;
|
---|
19 | private final BiletService biletService;
|
---|
20 | private final PostojkaNaLinijaService postojkaNaLinijaService;
|
---|
21 | private final InstancaNaLinijaService instancaNaLinijaService;
|
---|
22 |
|
---|
23 | public VozenjeServiceImpl(VozenjeRepository vozenjeRepository, PatnikService patnikService, BiletService biletService, PostojkaNaLinijaService postojkaNaLinijaService, InstancaNaLinijaService instancaNaLinijaService) {
|
---|
24 | this.vozenjeRepository = vozenjeRepository;
|
---|
25 | this.patnikService = patnikService;
|
---|
26 | this.biletService = biletService;
|
---|
27 | this.postojkaNaLinijaService = postojkaNaLinijaService;
|
---|
28 | this.instancaNaLinijaService = instancaNaLinijaService;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public List<Vozenje> findVozenjaByPatnik(String email) {
|
---|
33 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
34 | return vozenjeRepository.findAllByKorisnikByPatnikKId(patnik);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public List<Vozenje> findVozenjaByPatnikAndStatus(String email, VOZENJE_STATUS status) {
|
---|
39 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
40 | return vozenjeRepository.findAllByKorisnikByPatnikKIdAndAndVozenjeStatus(patnik, status);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public Vozenje start(String email, Long bId, Long pnlId, Long inlId) {
|
---|
45 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
46 | Bilet bilet = biletService.findBybIdAndPatnikEmail(bId, email);
|
---|
47 | Postojkanalinija postojkanalinija = postojkaNaLinijaService.findById(pnlId);
|
---|
48 | Instancanalinija instancanalinija = instancaNaLinijaService.findById(inlId);
|
---|
49 |
|
---|
50 | Vozenje vozenje = new Vozenje();
|
---|
51 | vozenje.setVozenjeStart(Timestamp.valueOf(LocalDateTime.now()));
|
---|
52 | vozenje.setVozenjeStatus(VOZENJE_STATUS.ACTIVE);
|
---|
53 | vozenje.setInstancanalinijaByInlId(instancanalinija);
|
---|
54 | vozenje.setKorisnikByPatnikKId(patnik);
|
---|
55 | vozenje.setPostojkanalinijaByKacuvaPnlId(postojkanalinija);
|
---|
56 |
|
---|
57 | if(bilet.getBStatus().equals(BILET_STATUS.INACTIVE))
|
---|
58 | bilet = biletService.activate(bId);
|
---|
59 | else if(biletService.checkExpiration(bId))
|
---|
60 | throw new RuntimeException("Bilet is expired!");
|
---|
61 | else if(bilet.getBStatus().equals(BILET_STATUS.EXPIRED))
|
---|
62 | throw new RuntimeException("Bilet is expired!");
|
---|
63 |
|
---|
64 | vozenje.setBiletByBId(bilet);
|
---|
65 | return vozenjeRepository.save(vozenje);
|
---|
66 | }
|
---|
67 |
|
---|
68 | @Override
|
---|
69 | public Vozenje end(Long vozenjeId, Long pnlId) {
|
---|
70 | // TODO : Relacija se simnuva na pnlId
|
---|
71 | Vozenje vozenje = this.findById(vozenjeId);
|
---|
72 | vozenje.setVozenjeEnd(Timestamp.valueOf(LocalDateTime.now()));
|
---|
73 | vozenje.setVozenjeStatus(VOZENJE_STATUS.FINISHED);
|
---|
74 | return vozenjeRepository.save(vozenje);
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public Vozenje findById(Long id) {
|
---|
79 | return vozenjeRepository.findById(id).orElseThrow(InvalidVozenjeIdException::new);
|
---|
80 | }
|
---|
81 | }
|
---|