Ignore:
Timestamp:
01/09/24 01:26:08 (10 months ago)
Author:
ppaunovski <paunovskipavel@…>
Branches:
master
Children:
e272096
Parents:
b101b69
Message:

All 3 main use cases implemented.

  1. Starting a commute
  2. Writing a ticket
  3. Starting an instance of a Bus Line
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/mk/ukim/finki/busngo/service/impl/BiletServiceImpl.java

    rb101b69 rbde8b13  
    33import mk.ukim.finki.busngo.model.entities.Bilet;
    44import mk.ukim.finki.busngo.model.entities.Korisnik;
     5import mk.ukim.finki.busngo.model.entities.Patnik;
    56import mk.ukim.finki.busngo.model.entities.Tipbilet;
    67import mk.ukim.finki.busngo.model.enums.BILET_STATUS;
     8import mk.ukim.finki.busngo.model.exceptions.InvalidBiletIdException;
    79import mk.ukim.finki.busngo.model.exceptions.InvalidTipBiletIdException;
    810import mk.ukim.finki.busngo.repository.BiletRepository;
     11import mk.ukim.finki.busngo.repository.KorisnikRepository;
    912import mk.ukim.finki.busngo.repository.TipBIletRepository;
    1013import mk.ukim.finki.busngo.service.BiletService;
    1114import mk.ukim.finki.busngo.service.KorisnikService;
    1215import mk.ukim.finki.busngo.service.PatnikService;
     16import org.springframework.security.crypto.password.PasswordEncoder;
    1317import org.springframework.stereotype.Service;
    1418
     
    2428    private final TipBIletRepository tipBIletRepository;
    2529    private final PatnikService patnikService;
     30    private final PasswordEncoder passwordEncoder;
     31    private final KorisnikRepository korisnikRepository;
    2632
    27     public BiletServiceImpl(BiletRepository biletRepository, KorisnikService korisnikService, TipBIletRepository tipBIletRepository, PatnikService patnikService) {
     33    public BiletServiceImpl(BiletRepository biletRepository, KorisnikService korisnikService, TipBIletRepository tipBIletRepository, PatnikService patnikService, PasswordEncoder passwordEncoder, KorisnikRepository korisnikRepository) {
    2834        this.biletRepository = biletRepository;
    2935        this.korisnikService = korisnikService;
    3036        this.tipBIletRepository = tipBIletRepository;
    3137        this.patnikService = patnikService;
     38        this.passwordEncoder = passwordEncoder;
     39        this.korisnikRepository = korisnikRepository;
    3240    }
    3341
     
    3947    @Override
    4048    public List<Bilet> findAllByPatnikId(Long id) {
    41         Korisnik korisnik = korisnikService.findById(id);
    42         return new ArrayList<>();
     49        Patnik patnik = patnikService.findById(id);
     50        return patnik.getBiletsByKId();
    4351    }
    4452
     
    5260    public Bilet buy(Long tipbilet, LocalDateTime now, BILET_STATUS biletStatus, String name) {
    5361        Tipbilet tipbilet1 = tipBIletRepository.findById(tipbilet).orElseThrow(InvalidTipBiletIdException::new);
    54         Korisnik korisnik = korisnikService.loadUserByEmail(name);
     62        Patnik korisnik = patnikService.loadUserByEmail(name);
    5563        Bilet bilet = new Bilet();
    5664        bilet.setBStatus(biletStatus);
     
    6068        return biletRepository.save(bilet);
    6169    }
     70
     71    @Override
     72    public List<Bilet> findAllByPatnikEmail(String email) {
     73        Patnik patnik = patnikService.loadUserByEmail(email);
     74        return patnik.getBiletsByKId();
     75    }
     76
     77    @Override
     78    public Bilet findBybIdAndPatnikEmail(Long bId, String email) {
     79        Patnik patnik = patnikService.loadUserByEmail(email);
     80        return biletRepository.findByBIdAndAndKorisnikByPatnikKId(bId, patnik).orElseThrow(InvalidBiletIdException::new);
     81    }
     82
     83    @Override
     84    public Bilet activate(Long bId) {
     85        Bilet bilet = this.findById(bId);
     86        bilet.setBStatus(BILET_STATUS.ACTIVE);
     87        return biletRepository.save(bilet);
     88    }
     89
     90    @Override
     91    public Bilet findById(Long bId) {
     92        return biletRepository.findById(bId).orElseThrow(RuntimeException::new);
     93    }
     94
     95    @Override
     96    public Bilet finish(Long bId) {
     97        Bilet bilet = this.findById(bId);
     98        bilet.setBStatus(BILET_STATUS.EXPIRED);
     99        return biletRepository.save(bilet);
     100    }
     101
     102    @Override
     103    public boolean checkExpiration(Long bId) {
     104        Bilet bilet = this.findById(bId);
     105        LocalDateTime timeBought = bilet.getBDatumNaKupuvanje().toLocalDateTime();
     106
     107        if(timeBought.plusSeconds(bilet.getTipbiletByTbId().getTbTrajnost()).isBefore(LocalDateTime.now())){
     108            bilet.setBStatus(BILET_STATUS.EXPIRED);
     109            biletRepository.save(bilet);
     110            return true;
     111        }
     112
     113        return false;
     114    }
     115
     116//    @Override
     117//    public void encode(){
     118//        List<Korisnik> all = korisnikRepository.findAll();
     119//        all.forEach(p -> p.setKLozinka(passwordEncoder.encode(p.getKLozinka())));
     120//        korisnikRepository.saveAll(all);
     121//        System.out.println("DONE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     122//    }
     123
     124
    62125}
Note: See TracChangeset for help on using the changeset viewer.