Ignore:
Timestamp:
01/09/24 01:26:08 (11 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
Location:
src/main/java/mk/ukim/finki/busngo/service/impl
Files:
11 added
3 edited

Legend:

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

    rb101b69 rbde8b13  
    22
    33import mk.ukim.finki.busngo.model.entities.Korisnik;
     4import mk.ukim.finki.busngo.model.entities.Patnik;
    45import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
    56import mk.ukim.finki.busngo.model.exceptions.UserAlreadyExistsException;
    67import mk.ukim.finki.busngo.repository.KorisnikRepository;
     8import mk.ukim.finki.busngo.repository.PatnikRepository;
    79import mk.ukim.finki.busngo.service.AuthService;
    810import org.springframework.security.crypto.password.PasswordEncoder;
    911import org.springframework.stereotype.Service;
    1012
    11 import java.util.List;
    12 
    1313@Service
    1414public class AuthServiceImpl implements AuthService {
    1515    private final KorisnikRepository korisnikRepository;
     16    private final PatnikRepository patnikRepository;
    1617    private final PasswordEncoder passwordEncoder;
    1718
    18     public AuthServiceImpl(KorisnikRepository korisnikRepository, PasswordEncoder passwordEncoder) {
     19    public AuthServiceImpl(KorisnikRepository korisnikRepository, PatnikRepository patnikRepository, PasswordEncoder passwordEncoder) {
    1920        this.korisnikRepository = korisnikRepository;
     21        this.patnikRepository = patnikRepository;
    2022        this.passwordEncoder = passwordEncoder;
    2123    }
     
    2931
    3032    @Override
    31     public Korisnik register(String ime, String email, String password, String confirmPassword, String address, String telefon) {
     33    public Korisnik registerPatnik(String ime, String email, String password, String confirmPassword, String address, String telefon) {
    3234        if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
    3335            throw new InvalidCredentialsException();
     
    4244        }
    4345
    44         Korisnik korisnik = new Korisnik();
     46        Patnik korisnik = new Patnik();
    4547        korisnik.setKIme(ime);
    4648        korisnik.setKAdresa(address);
     
    5052        korisnik.setKIsAdmin(false);
    5153
    52         return korisnikRepository.save(korisnik);
     54
     55        return patnikRepository.save(korisnik);
    5356    }
    5457
  • 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}
  • src/main/java/mk/ukim/finki/busngo/service/impl/PatnikServiceImpl.java

    rb101b69 rbde8b13  
    11package mk.ukim.finki.busngo.service.impl;
    22
     3import mk.ukim.finki.busngo.model.entities.Instancanalinija;
     4import mk.ukim.finki.busngo.model.entities.Korisnik;
    35import mk.ukim.finki.busngo.model.entities.Patnik;
    46import mk.ukim.finki.busngo.model.exceptions.InvalidPatnikIdException;
    57import mk.ukim.finki.busngo.repository.PatnikRepository;
     8import mk.ukim.finki.busngo.service.InstancaNaLinijaService;
    69import mk.ukim.finki.busngo.service.PatnikService;
    710import org.springframework.stereotype.Service;
    811
     12import java.util.Collections;
    913import java.util.List;
    1014
     
    1216public class PatnikServiceImpl implements PatnikService {
    1317    private final PatnikRepository patnikRepository;
     18    private final InstancaNaLinijaService instancaNaLinijaService;
    1419
    15     public PatnikServiceImpl(PatnikRepository patnikRepository) {
     20    public PatnikServiceImpl(PatnikRepository patnikRepository, InstancaNaLinijaService instancaNaLinijaService) {
    1621        this.patnikRepository = patnikRepository;
     22        this.instancaNaLinijaService = instancaNaLinijaService;
    1723    }
    1824
    1925    @Override
    2026    public List<Patnik> listAll() {
    21         return patnikRepository.findAll();
     27        return  patnikRepository.findAll();
    2228    }
    2329
     
    2632        return patnikRepository.findById(id).orElseThrow(InvalidPatnikIdException::new);
    2733    }
     34
     35    @Override
     36    public Patnik loadUserByEmail(String email) {
     37        return patnikRepository.findByKEmail(email).orElseThrow(InvalidPatnikIdException::new);
     38    }
     39
     40    @Override
     41    public Patnik save(Korisnik korisnik) {
     42        Patnik patnik = new Patnik();
     43        patnik.setKId(korisnik.getKId());
     44        patnik.setKEmail(korisnik.getKEmail());
     45        patnik.setKAdresa(korisnik.getKAdresa());
     46        patnik.setKIme(korisnik.getKIme());
     47        patnik.setKTelefon(korisnik.getKTelefon());
     48        patnik.setKIsAdmin(korisnik.getKIsAdmin());
     49        patnik.setKLozinka(korisnik.getKLozinka());
     50
     51        return patnikRepository.save((Patnik) korisnik);
     52    }
     53
     54
    2855}
Note: See TracChangeset for help on using the changeset viewer.