1 | package mk.ukim.finki.busngo.service.impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.*;
|
---|
4 | import mk.ukim.finki.busngo.model.exceptions.UserShouldNotBeTicketedException;
|
---|
5 | import mk.ukim.finki.busngo.repository.KaznaZaRegistriranRepository;
|
---|
6 | import mk.ukim.finki.busngo.service.KaznaZaRegistriranService;
|
---|
7 | import mk.ukim.finki.busngo.service.KondukterService;
|
---|
8 | import mk.ukim.finki.busngo.service.KontrolaService;
|
---|
9 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import java.sql.Timestamp;
|
---|
13 | import java.time.LocalDateTime;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.stream.Collectors;
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | public class KaznaZaRegistriranServiceImpl implements KaznaZaRegistriranService {
|
---|
19 | private final PatnikService patnikService;
|
---|
20 | private final KaznaZaRegistriranRepository kaznaZaRegistriranRepository;
|
---|
21 | private final KontrolaService kontrolaService;
|
---|
22 | private final KondukterService kondukterService;
|
---|
23 |
|
---|
24 | public KaznaZaRegistriranServiceImpl(PatnikService patnikService, KaznaZaRegistriranRepository kaznaZaRegistriranRepository, KontrolaService kontrolaService, KondukterService kondukterService) {
|
---|
25 | this.patnikService = patnikService;
|
---|
26 | this.kaznaZaRegistriranRepository = kaznaZaRegistriranRepository;
|
---|
27 | this.kontrolaService = kontrolaService;
|
---|
28 | this.kondukterService = kondukterService;
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public List<Kaznazaregistriran> findAllByPatnik(String email) {
|
---|
33 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
34 | return kaznaZaRegistriranRepository.findAllByKorisnikByPatnikKId(patnik);
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public Kaznazaregistriran create(Long kontrolaId, String dokument, Double iznos, Long patnik, String email) {
|
---|
39 | Kontroli kontroli = kontrolaService.findById(kontrolaId);
|
---|
40 | Kondukter kondukter = kondukterService.findByEmail(email);
|
---|
41 | Patnik patnik1 = patnikService.findById(patnik);
|
---|
42 | Kaznazaregistriran kaznazaregistriran = new Kaznazaregistriran();
|
---|
43 | kaznazaregistriran.setKzDatum(Timestamp.valueOf(LocalDateTime.now()));
|
---|
44 | kaznazaregistriran.setKzDokument(dokument);
|
---|
45 | kaznazaregistriran.setKzIznos(iznos);
|
---|
46 | kaznazaregistriran.setKzPlateno(false);
|
---|
47 | kaznazaregistriran.setKontroliByKontrolaId(kontroli);
|
---|
48 | kaznazaregistriran.setKorisnikByPatnikKId(patnik1);
|
---|
49 | kaznazaregistriran.setKorisnikByKondukterKId(kondukter);
|
---|
50 | List<Vozenje> vozenjeList = patnik1.getVozenjesByKId().stream()
|
---|
51 | .filter(v -> v.getInstancanalinijaByInlId().getInlId().equals(kontroli.getInstancanalinijaByInlId().getInlId()))
|
---|
52 | .toList();
|
---|
53 | if(vozenjeList.isEmpty())
|
---|
54 | return kaznaZaRegistriranRepository.save(kaznazaregistriran);
|
---|
55 |
|
---|
56 | throw new UserShouldNotBeTicketedException(String.format("Patnik with id: %d and email: %s has valid vozenje for the %d instance!",
|
---|
57 | patnik1.getKId(),
|
---|
58 | patnik1.getKEmail(),
|
---|
59 | kontroli.getInstancanalinijaByInlId().getInlId()));
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Kaznazaregistriran pay(Long kaznaId) {
|
---|
64 | Kaznazaregistriran kazna = this.findById(kaznaId);
|
---|
65 | if(!kazna.getKzPlateno()){
|
---|
66 | kazna.setKzPlateno(true);
|
---|
67 | kazna.setKzDatumPlateno(Timestamp.valueOf(LocalDateTime.now()));
|
---|
68 | }
|
---|
69 | return kaznaZaRegistriranRepository.save(kazna);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public Kaznazaregistriran findById(Long id) {
|
---|
74 | return kaznaZaRegistriranRepository.findById(id).orElseThrow(RuntimeException::new);
|
---|
75 | }
|
---|
76 | }
|
---|