source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/kotlin/mk/ukim/finki/busngobackend/service/ControlService.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: 2.5 KB
Line 
1package mk.ukim.finki.busngobackend.service
2
3import mk.ukim.finki.busngobackend.api.responses.ControlResponse
4import mk.ukim.finki.busngobackend.api.responses.FineResponse
5import mk.ukim.finki.busngobackend.domain.entities.Kontrola
6import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
7import mk.ukim.finki.busngobackend.repository.*
8import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
9import org.springframework.data.repository.findByIdOrNull
10import org.springframework.stereotype.Service
11import java.sql.Timestamp
12import java.time.LocalDateTime
13
14@Service
15class 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}
Note: See TracBrowser for help on using the repository browser.