source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/kotlin/mk/ukim/finki/busngobackend/service/RouteInstanceService.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: 4.5 KB
Line 
1package mk.ukim.finki.busngobackend.service
2
3import mk.ukim.finki.busngobackend.api.requests.StartRouteInstanceRequest
4import mk.ukim.finki.busngobackend.api.responses.RouteInstanceResponse
5import mk.ukim.finki.busngobackend.domain.entities.InstancaNaLinija
6import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
7import mk.ukim.finki.busngobackend.events.StartRouteEvent
8import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
9import mk.ukim.finki.busngobackend.repository.*
10import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
11import mk.ukim.finki.busngobackend.service.exceptions.UnauthorizedAccessException
12import org.springframework.context.ApplicationEventPublisher
13import org.springframework.data.repository.findByIdOrNull
14import org.springframework.stereotype.Service
15import java.sql.Timestamp
16import java.time.LocalDateTime
17
18@Service
19class 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}
Note: See TracBrowser for help on using the repository browser.