source: bus-n-go-do-5ta-faza/src/main/java/mk/ukim/finki/busngo/service/impl/KaznaZaRegistriranServiceImpl.java

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 3 months ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 3.5 KB
Line 
1package mk.ukim.finki.busngo.service.impl;
2
3import mk.ukim.finki.busngo.model.entities.*;
4import mk.ukim.finki.busngo.model.exceptions.UserShouldNotBeTicketedException;
5import mk.ukim.finki.busngo.repository.KaznaZaRegistriranRepository;
6import mk.ukim.finki.busngo.service.KaznaZaRegistriranService;
7import mk.ukim.finki.busngo.service.KondukterService;
8import mk.ukim.finki.busngo.service.KontrolaService;
9import mk.ukim.finki.busngo.service.PatnikService;
10import org.springframework.stereotype.Service;
11
12import java.sql.Timestamp;
13import java.time.LocalDateTime;
14import java.util.List;
15import java.util.stream.Collectors;
16
17@Service
18public 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}
Note: See TracBrowser for help on using the repository browser.