1 | package mk.ukim.finki.busngo.service.impl;
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngo.model.entities.Bilet;
|
---|
4 | import mk.ukim.finki.busngo.model.entities.Korisnik;
|
---|
5 | import mk.ukim.finki.busngo.model.entities.Patnik;
|
---|
6 | import mk.ukim.finki.busngo.model.entities.Tipbilet;
|
---|
7 | import mk.ukim.finki.busngo.model.enums.BILET_STATUS;
|
---|
8 | import mk.ukim.finki.busngo.model.exceptions.InvalidBiletIdException;
|
---|
9 | import mk.ukim.finki.busngo.model.exceptions.InvalidTipBiletIdException;
|
---|
10 | import mk.ukim.finki.busngo.repository.BiletRepository;
|
---|
11 | import mk.ukim.finki.busngo.repository.KorisnikRepository;
|
---|
12 | import mk.ukim.finki.busngo.repository.TipBIletRepository;
|
---|
13 | import mk.ukim.finki.busngo.service.BiletService;
|
---|
14 | import mk.ukim.finki.busngo.service.KorisnikService;
|
---|
15 | import mk.ukim.finki.busngo.service.PatnikService;
|
---|
16 | import org.springframework.security.crypto.password.PasswordEncoder;
|
---|
17 | import org.springframework.stereotype.Service;
|
---|
18 | import org.thymeleaf.expression.Lists;
|
---|
19 |
|
---|
20 | import java.sql.Timestamp;
|
---|
21 | import java.time.LocalDateTime;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.List;
|
---|
24 |
|
---|
25 | @Service
|
---|
26 | public class BiletServiceImpl implements BiletService {
|
---|
27 | private final BiletRepository biletRepository;
|
---|
28 | private final KorisnikService korisnikService;
|
---|
29 | private final TipBIletRepository tipBIletRepository;
|
---|
30 | private final PatnikService patnikService;
|
---|
31 | private final PasswordEncoder passwordEncoder;
|
---|
32 | private final KorisnikRepository korisnikRepository;
|
---|
33 |
|
---|
34 | public BiletServiceImpl(BiletRepository biletRepository, KorisnikService korisnikService, TipBIletRepository tipBIletRepository, PatnikService patnikService, PasswordEncoder passwordEncoder, KorisnikRepository korisnikRepository) {
|
---|
35 | this.biletRepository = biletRepository;
|
---|
36 | this.korisnikService = korisnikService;
|
---|
37 | this.tipBIletRepository = tipBIletRepository;
|
---|
38 | this.patnikService = patnikService;
|
---|
39 | this.passwordEncoder = passwordEncoder;
|
---|
40 | this.korisnikRepository = korisnikRepository;
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public List<Bilet> listAll() {
|
---|
45 | return biletRepository.findAll();
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public List<Bilet> findAllByPatnikId(Long id) {
|
---|
50 | Patnik patnik = patnikService.findById(id);
|
---|
51 | return patnik.getBiletsByKId();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public List<Bilet> findAllByPatnikIdAndStatus(Long id, BILET_STATUS status) {
|
---|
56 | Korisnik korisnik = korisnikService.findById(id);
|
---|
57 | return new ArrayList<>();
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public Bilet buy(Long tipbilet, LocalDateTime now, BILET_STATUS biletStatus, String name) {
|
---|
62 | Tipbilet tipbilet1 = tipBIletRepository.findById(tipbilet).orElseThrow(InvalidTipBiletIdException::new);
|
---|
63 | Patnik korisnik = patnikService.loadUserByEmail(name);
|
---|
64 | Bilet bilet = new Bilet();
|
---|
65 | bilet.setBStatus(biletStatus);
|
---|
66 | bilet.setTipbiletByTbId(tipbilet1);
|
---|
67 | bilet.setBDatumNaKupuvanje(Timestamp.valueOf(now));
|
---|
68 | bilet.setKorisnikByPatnikKId(korisnik);
|
---|
69 | return biletRepository.save(bilet);
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public List<Bilet> findAllByPatnikEmail(String email) {
|
---|
74 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
75 | return patnik.getBiletsByKId();
|
---|
76 | }
|
---|
77 |
|
---|
78 | // @Override
|
---|
79 | // public List<Bilet> findAllByPatnikEmailACTIVE(String email) {
|
---|
80 | // Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
81 | // List<Bilet> allByKorisnikByPatnikKIdAndBStatus = biletRepository.findAllByKorisnikByPatnikKIdAndBStatus(patnik, BILET_STATUS.ACTIVE);
|
---|
82 | // List<Bilet> allByKorisnikByPatnikKIdAndBStatus1 = biletRepository.findAllByKorisnikByPatnikKIdAndBStatus(patnik, BILET_STATUS.INACTIVE);
|
---|
83 | // allByKorisnikByPatnikKIdAndBStatus1.addAll(allByKorisnikByPatnikKIdAndBStatus);
|
---|
84 | // return allByKorisnikByPatnikKIdAndBStatus1;
|
---|
85 | // }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public Bilet findBybIdAndPatnikEmail(Long bId, String email) {
|
---|
89 | Patnik patnik = patnikService.loadUserByEmail(email);
|
---|
90 | return biletRepository.findByBIdAndAndKorisnikByPatnikKId(bId, patnik).orElseThrow(InvalidBiletIdException::new);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public Bilet activate(Long bId) {
|
---|
95 | Bilet bilet = this.findById(bId);
|
---|
96 | bilet.setBStatus(BILET_STATUS.ACTIVE);
|
---|
97 | return biletRepository.save(bilet);
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public Bilet findById(Long bId) {
|
---|
102 | return biletRepository.findById(bId).orElseThrow(RuntimeException::new);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public Bilet finish(Long bId) {
|
---|
107 | Bilet bilet = this.findById(bId);
|
---|
108 | bilet.setBStatus(BILET_STATUS.EXPIRED);
|
---|
109 | return biletRepository.save(bilet);
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public boolean checkExpiration(Long bId) {
|
---|
114 | Bilet bilet = this.findById(bId);
|
---|
115 | LocalDateTime timeBought = bilet.getBDatumNaKupuvanje().toLocalDateTime();
|
---|
116 |
|
---|
117 | if(timeBought.plusSeconds(bilet.getTipbiletByTbId().getTbTrajnost()).isBefore(LocalDateTime.now())){
|
---|
118 | bilet.setBStatus(BILET_STATUS.EXPIRED);
|
---|
119 | biletRepository.save(bilet);
|
---|
120 | return true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 |
|
---|
126 | // @Override
|
---|
127 | // public void encode(){
|
---|
128 | // List<Korisnik> all = korisnikRepository.findAll();
|
---|
129 | // all.forEach(p -> p.setKLozinka(passwordEncoder.encode(p.getKLozinka())));
|
---|
130 | // korisnikRepository.saveAll(all);
|
---|
131 | // System.out.println("DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
---|
132 | // }
|
---|
133 |
|
---|
134 |
|
---|
135 | }
|
---|