1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional
|
---|
4 | import mk.ukim.finki.busngobackend.api.requests.StartCommuteRequest
|
---|
5 | import mk.ukim.finki.busngobackend.api.responses.CommuteResponse
|
---|
6 | import mk.ukim.finki.busngobackend.domain.entities.Vozenje
|
---|
7 | import mk.ukim.finki.busngobackend.domain.enums.BiletEnum
|
---|
8 | import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
|
---|
9 | import mk.ukim.finki.busngobackend.domain.enums.VozenjeStatus
|
---|
10 | import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
|
---|
11 | import mk.ukim.finki.busngobackend.repository.*
|
---|
12 | import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
|
---|
13 | import mk.ukim.finki.busngobackend.service.exceptions.UnauthorizedAccessException
|
---|
14 | import org.springframework.data.repository.findByIdOrNull
|
---|
15 | import org.springframework.stereotype.Service
|
---|
16 | import java.sql.Timestamp
|
---|
17 | import java.time.LocalDateTime
|
---|
18 |
|
---|
19 | @Service
|
---|
20 | class CommuteService(
|
---|
21 | private val linijaRepository: LinijaRepository,
|
---|
22 | private val postojkaRepository: PostojkaRepository,
|
---|
23 | private val postojkaNaLinijaRepository: PostojkaNaLinijaRepository,
|
---|
24 | private val biletRepository: BiletRepository,
|
---|
25 | private val authService: AuthService,
|
---|
26 | private val patnikRepository: PatnikRepository,
|
---|
27 | private val instancaNaLinijaRepository: InstancaNaLinijaRepository,
|
---|
28 | private val vozenjeRepository: VozenjeRepository,
|
---|
29 | private val dtoMapper: ClassToDtoMapper,
|
---|
30 | ) {
|
---|
31 | @Transactional
|
---|
32 | fun start(request: StartCommuteRequest): CommuteResponse {
|
---|
33 | if (!this.authService.hasAuthority(RoleEnum.ROLE_PASSENGER)) throw UnauthorizedAccessException("Unauthorised role")
|
---|
34 | val korisnik = this.authService.getAuthenticatedUser()
|
---|
35 | val patnik = patnikRepository.findByKorisnik(korisnik) ?: throw NotFoundException("Korisnik")
|
---|
36 |
|
---|
37 | var bilet = biletRepository.findByIdAndPatnik(request.ticketId, patnik) ?: throw NotFoundException("Bilet")
|
---|
38 |
|
---|
39 | val instancaNaLinija =
|
---|
40 | instancaNaLinijaRepository.findByIdOrNull(request.routeInstanceId) ?: throw NotFoundException("RouteInstanceId")
|
---|
41 | val postojka = postojkaRepository.findByIdOrNull(request.stationId) ?: throw NotFoundException("StationId")
|
---|
42 |
|
---|
43 | val postojkaNaLinijaStart =
|
---|
44 | postojkaNaLinijaRepository.findByLinijaAndPostojkaAndPravec(instancaNaLinija.linija, postojka, instancaNaLinija.pravec)
|
---|
45 | ?: throw NotFoundException("Station not found")
|
---|
46 |
|
---|
47 | if (bilet.status == BiletEnum.INACTIVE) {
|
---|
48 | bilet.status = BiletEnum.ACTIVE
|
---|
49 | bilet.datumAktivacija = Timestamp.valueOf(LocalDateTime.now())
|
---|
50 | bilet = biletRepository.save(bilet)
|
---|
51 | }
|
---|
52 |
|
---|
53 | val vozenje =
|
---|
54 | Vozenje(
|
---|
55 | id = 0L,
|
---|
56 | startDate = Timestamp.valueOf(LocalDateTime.now()),
|
---|
57 | endDate = null,
|
---|
58 | status = VozenjeStatus.ACTIVE,
|
---|
59 | patnik = patnik,
|
---|
60 | postojkaNaLinijaStart = postojkaNaLinijaStart,
|
---|
61 | instancaNaLinija = instancaNaLinija,
|
---|
62 | bilet = bilet,
|
---|
63 | )
|
---|
64 | return vozenjeRepository.save(vozenje).let { dtoMapper.toCommuteResponse(it) }
|
---|
65 | }
|
---|
66 | }
|
---|