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