1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngobackend.api.requests.StartRouteInstanceRequest
|
---|
4 | import mk.ukim.finki.busngobackend.api.responses.RouteInstanceResponse
|
---|
5 | import mk.ukim.finki.busngobackend.domain.entities.InstancaNaLinija
|
---|
6 | import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
|
---|
7 | import mk.ukim.finki.busngobackend.events.StartRouteEvent
|
---|
8 | import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
|
---|
9 | import mk.ukim.finki.busngobackend.repository.*
|
---|
10 | import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
|
---|
11 | import mk.ukim.finki.busngobackend.service.exceptions.UnauthorizedAccessException
|
---|
12 | import org.springframework.context.ApplicationEventPublisher
|
---|
13 | import org.springframework.data.repository.findByIdOrNull
|
---|
14 | import org.springframework.stereotype.Service
|
---|
15 | import java.sql.Timestamp
|
---|
16 | import java.time.LocalDateTime
|
---|
17 |
|
---|
18 | @Service
|
---|
19 | class RouteInstanceService(
|
---|
20 | private val instancaNaLinijaRepository: InstancaNaLinijaRepository,
|
---|
21 | private val linijaRepository: LinijaRepository,
|
---|
22 | private val pravecRepository: PravecRepository,
|
---|
23 | private val linijaPravecRepository: LinijaPravecRepository,
|
---|
24 | private val avtobusRepository: AvtobusRepository,
|
---|
25 | private val authService: AuthService,
|
---|
26 | private val vrabotenRepository: VrabotenRepository,
|
---|
27 | private val vozacRepository: VozacRepository,
|
---|
28 | private val dtoMapper: ClassToDtoMapper,
|
---|
29 | private val publisher: ApplicationEventPublisher,
|
---|
30 | private val postojkaRepository: PostojkaRepository,
|
---|
31 | ) {
|
---|
32 | fun start(request: StartRouteInstanceRequest): RouteInstanceResponse {
|
---|
33 | if (!authService.isAuthenticated() || !authService.hasAuthority(RoleEnum.ROLE_DRIVER)) {
|
---|
34 | throw UnauthorizedAccessException("Unauthorized access")
|
---|
35 | }
|
---|
36 |
|
---|
37 | val korisnik = authService.getAuthenticatedUser()
|
---|
38 | val vraboten = vrabotenRepository.findByKorisnik(korisnik) ?: throw NotFoundException("Korisnik")
|
---|
39 | val vozac = vozacRepository.findByVraboten(vraboten) ?: throw NotFoundException("Vraboten")
|
---|
40 |
|
---|
41 | val linija = linijaRepository.findByIdOrNull(request.lineId) ?: throw NotFoundException("Line not found")
|
---|
42 | val pravec =
|
---|
43 | pravecRepository.findByIdOrNull(request.directionId) ?: throw NotFoundException("Direction not found")
|
---|
44 |
|
---|
45 | if (!linijaPravecRepository.existsByLinijaAndPravec(linija, pravec)) {
|
---|
46 | throw NotFoundException("Line does not has the specified direction")
|
---|
47 | }
|
---|
48 |
|
---|
49 | val avtobus = avtobusRepository.findByIdOrNull(request.busId) ?: throw NotFoundException("Bus Not Found")
|
---|
50 |
|
---|
51 | val instanca =
|
---|
52 | InstancaNaLinija(
|
---|
53 | id = 0L,
|
---|
54 | startDate = Timestamp.valueOf(LocalDateTime.now()),
|
---|
55 | linija = linija,
|
---|
56 | pravec = pravec,
|
---|
57 | avtobus = avtobus,
|
---|
58 | vozac = vozac,
|
---|
59 | endDate = null,
|
---|
60 | )
|
---|
61 |
|
---|
62 | return instancaNaLinijaRepository
|
---|
63 | .save(instanca)
|
---|
64 | .let {
|
---|
65 | publisher.publishEvent(StartRouteEvent(it))
|
---|
66 | dtoMapper.toRouteInstanceResponse(it)
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | fun findById(id: Long): InstancaNaLinija = instancaNaLinijaRepository.findByIdOrNull(id) ?: throw NotFoundException("id not found")
|
---|
71 |
|
---|
72 | fun stop(id: Long): RouteInstanceResponse {
|
---|
73 | val instance = this.findById(id)
|
---|
74 | if (instance.endDate != null) throw UnauthorizedAccessException("Unauthorized access")
|
---|
75 | instance.endDate = Timestamp.valueOf(LocalDateTime.now())
|
---|
76 | return instancaNaLinijaRepository.save(instance).let { dtoMapper.toRouteInstanceResponse(it) }
|
---|
77 | }
|
---|
78 |
|
---|
79 | fun getById(id: Long): RouteInstanceResponse {
|
---|
80 | val ri = this.findById(id)
|
---|
81 | val korisnik = this.authService.getAuthenticatedUser()
|
---|
82 | if (korisnik.id != ri.vozac.vraboten.korisnik.id) throw UnauthorizedAccessException("Unauthorized access")
|
---|
83 | return dtoMapper.toRouteInstanceResponse(this.findById(id))
|
---|
84 | }
|
---|
85 |
|
---|
86 | fun getForStation(stationId: Long): List<RouteInstanceResponse> {
|
---|
87 | val station = postojkaRepository.findByIdOrNull(stationId) ?: throw NotFoundException("Station not found")
|
---|
88 | // find all route instances heading to the given station
|
---|
89 | val routeInstanceIds = instancaNaLinijaRepository.findIncomingRouteInstancesForStation(station.id)
|
---|
90 | val routeInstances = instancaNaLinijaRepository.findAllById(routeInstanceIds)
|
---|
91 | return routeInstances.map { dtoMapper.toRouteInstanceResponse(it) }
|
---|
92 | }
|
---|
93 |
|
---|
94 | fun getAll(): List<RouteInstanceResponse> =
|
---|
95 | instancaNaLinijaRepository.findAllByEndDateIsNull().map { dtoMapper.toRouteInstanceResponse(it) }
|
---|
96 | }
|
---|