1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import jakarta.transaction.Transactional
|
---|
4 | import mk.ukim.finki.busngobackend.api.requests.FineRequest
|
---|
5 | import mk.ukim.finki.busngobackend.api.responses.FineResponse
|
---|
6 | import mk.ukim.finki.busngobackend.domain.entities.Kazna
|
---|
7 | import mk.ukim.finki.busngobackend.domain.entities.KaznaZaNeregistriran
|
---|
8 | import mk.ukim.finki.busngobackend.domain.entities.KaznaZaRegistriran
|
---|
9 | import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
|
---|
10 | import mk.ukim.finki.busngobackend.repository.*
|
---|
11 | import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
|
---|
12 | import org.springframework.data.repository.findByIdOrNull
|
---|
13 | import org.springframework.stereotype.Service
|
---|
14 | import java.sql.Timestamp
|
---|
15 | import java.time.LocalDateTime
|
---|
16 |
|
---|
17 | @Service
|
---|
18 | class FineService(
|
---|
19 | private val kaznaRepository: KaznaRepository,
|
---|
20 | private val authService: AuthService,
|
---|
21 | private val kontrolaRepository: KontrolaRepository,
|
---|
22 | private val dtoMapper: ClassToDtoMapper,
|
---|
23 | private val kaznaZaRegistriranRepository: KaznaZaRegistriranRepository,
|
---|
24 | private val kaznaZaNeregistriranRepository: KaznaZaNeregistriranRepository,
|
---|
25 | private val korisnikRepository: KorisnikRepository,
|
---|
26 | private val patnikRepository: PatnikRepository,
|
---|
27 | ) {
|
---|
28 | @Transactional
|
---|
29 | fun createFine(request: FineRequest): FineResponse {
|
---|
30 | val kontrola = kontrolaRepository.findByIdOrNull(request.kontrolaId) ?: throw NotFoundException("Kontrola")
|
---|
31 | val now = Timestamp.valueOf(LocalDateTime.now())
|
---|
32 | val kazna =
|
---|
33 | kaznaRepository.save(
|
---|
34 | Kazna(
|
---|
35 | dateCreated = now,
|
---|
36 | id = 0L,
|
---|
37 | kondukter = authService.getConductor(),
|
---|
38 | datePayed =
|
---|
39 | if (request.plateno) {
|
---|
40 | now
|
---|
41 | } else {
|
---|
42 | null
|
---|
43 | },
|
---|
44 | iznos = request.iznos,
|
---|
45 | dokument = request.dokument,
|
---|
46 | kontrola = kontrola,
|
---|
47 | plateno = request.plateno,
|
---|
48 | ),
|
---|
49 | )
|
---|
50 |
|
---|
51 | if (request.patnikId != null) {
|
---|
52 | val korisnik = korisnikRepository.findByIdOrNull(request.patnikId!!) ?: throw NotFoundException("Korisnik")
|
---|
53 | val patnik = patnikRepository.findByKorisnik(korisnik) ?: throw NotFoundException("Patnik")
|
---|
54 | val kzr =
|
---|
55 | kaznaZaRegistriranRepository.save(
|
---|
56 | KaznaZaRegistriran(
|
---|
57 | patnik = patnik,
|
---|
58 | id = 0L,
|
---|
59 | kazna = kazna,
|
---|
60 | ),
|
---|
61 | )
|
---|
62 | return kzr.let { dtoMapper.toFineResponse(kazna = kazna, kzr = kzr, kzn = null) }
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (request.adresa != null && request.ime != null && request.telefon != null) {
|
---|
66 | val kzn =
|
---|
67 | kaznaZaNeregistriranRepository.save(
|
---|
68 | KaznaZaNeregistriran(
|
---|
69 | id = 0L,
|
---|
70 | kazna = kazna,
|
---|
71 | ime = request.ime!!,
|
---|
72 | telefon = request.telefon!!,
|
---|
73 | adresa = request.adresa!!,
|
---|
74 | ),
|
---|
75 | )
|
---|
76 |
|
---|
77 | return kzn.let { dtoMapper.toFineResponse(kazna = kazna, kzn = kzn, kzr = null) }
|
---|
78 | }
|
---|
79 | return dtoMapper.toFineResponse(kazna = kazna, kzr = null, kzn = null)
|
---|
80 | }
|
---|
81 | }
|
---|