source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/kotlin/mk/ukim/finki/busngobackend/service/CommuteService.kt

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.1 KB
Line 
1package mk.ukim.finki.busngobackend.service
2
3import jakarta.transaction.Transactional
4import mk.ukim.finki.busngobackend.api.requests.StartCommuteRequest
5import mk.ukim.finki.busngobackend.api.responses.CommuteResponse
6import mk.ukim.finki.busngobackend.domain.entities.Vozenje
7import mk.ukim.finki.busngobackend.domain.enums.BiletEnum
8import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
9import mk.ukim.finki.busngobackend.domain.enums.VozenjeStatus
10import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
11import mk.ukim.finki.busngobackend.repository.*
12import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
13import mk.ukim.finki.busngobackend.service.exceptions.UnauthorizedAccessException
14import org.springframework.data.repository.findByIdOrNull
15import org.springframework.stereotype.Service
16import java.sql.Timestamp
17import java.time.LocalDateTime
18
19@Service
20class 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}
Note: See TracBrowser for help on using the repository browser.