1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngobackend.api.responses.ControlResponse
|
---|
4 | import mk.ukim.finki.busngobackend.api.responses.FineResponse
|
---|
5 | import mk.ukim.finki.busngobackend.domain.entities.Kontrola
|
---|
6 | import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
|
---|
7 | import mk.ukim.finki.busngobackend.repository.*
|
---|
8 | import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
|
---|
9 | import org.springframework.data.repository.findByIdOrNull
|
---|
10 | import org.springframework.stereotype.Service
|
---|
11 | import java.sql.Timestamp
|
---|
12 | import java.time.LocalDateTime
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | class ControlService(
|
---|
16 | private val kontrolaRepository: KontrolaRepository,
|
---|
17 | private val dtoMapper: ClassToDtoMapper,
|
---|
18 | private val authService: AuthService,
|
---|
19 | private val instancaNaLinijaRepository: InstancaNaLinijaRepository,
|
---|
20 | private val kaznaRepository: KaznaRepository,
|
---|
21 | private val kaznaZaRegistriranRepository: KaznaZaRegistriranRepository,
|
---|
22 | private val kaznaZaNeregistriranRepository: KaznaZaNeregistriranRepository,
|
---|
23 | ) {
|
---|
24 | fun getAllByConductor(): List<ControlResponse> {
|
---|
25 | val kondukter = authService.getConductor()
|
---|
26 |
|
---|
27 | return kontrolaRepository
|
---|
28 | .findAllByKondukterOrderByDateCreatedDesc(
|
---|
29 | kondukter,
|
---|
30 | ).map { kontrola -> dtoMapper.toControlResponse(kontrola) }
|
---|
31 | }
|
---|
32 |
|
---|
33 | fun start(routeInstanceId: Long): ControlResponse {
|
---|
34 | val instancaNaLinija =
|
---|
35 | instancaNaLinijaRepository.findByIdOrNull(routeInstanceId) ?: throw NotFoundException("Route instance")
|
---|
36 | val kontrola =
|
---|
37 | kontrolaRepository.save(
|
---|
38 | Kontrola(
|
---|
39 | id = 0L,
|
---|
40 | kondukter = authService.getConductor(),
|
---|
41 | dateCreated = Timestamp.valueOf(LocalDateTime.now()),
|
---|
42 | instancaNaLinija = instancaNaLinija,
|
---|
43 | ),
|
---|
44 | )
|
---|
45 | return kontrola.let { dtoMapper.toControlResponse(it) }
|
---|
46 | }
|
---|
47 |
|
---|
48 | fun getFines(controlId: Long): List<FineResponse> {
|
---|
49 | val kondukter = authService.getConductor()
|
---|
50 | val kontrola = kontrolaRepository.findByIdOrNull(controlId) ?: throw NotFoundException("Control not found")
|
---|
51 |
|
---|
52 | val kazni = kaznaRepository.findAllByKontrola(kontrola)
|
---|
53 |
|
---|
54 | val kzrs = kaznaZaRegistriranRepository.findAllByKaznaIn(kazni)
|
---|
55 | val kzns = kaznaZaNeregistriranRepository.findAllByKaznaIn(kazni)
|
---|
56 |
|
---|
57 | return kzrs
|
---|
58 | .map { dtoMapper.toFineResponse(kazna = it.kazna, kzr = it, kzn = null) }
|
---|
59 | .plus(kzns.map { dtoMapper.toFineResponse(kazna = it.kazna, kzr = null, kzn = it) })
|
---|
60 | }
|
---|
61 | }
|
---|