1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngobackend.api.requests.TicketBuyRequest
|
---|
4 | import mk.ukim.finki.busngobackend.api.responses.TicketResponse
|
---|
5 | import mk.ukim.finki.busngobackend.domain.entities.Bilet
|
---|
6 | import mk.ukim.finki.busngobackend.domain.enums.BiletEnum
|
---|
7 | import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
|
---|
8 | import mk.ukim.finki.busngobackend.repository.BiletRepository
|
---|
9 | import mk.ukim.finki.busngobackend.repository.PatnikRepository
|
---|
10 | import mk.ukim.finki.busngobackend.repository.TipbiletRepository
|
---|
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 BiletService(
|
---|
19 | private val mapper: ClassToDtoMapper,
|
---|
20 | private val biletRepository: BiletRepository,
|
---|
21 | private val korisnikService: KorisnikService,
|
---|
22 | private val patnikRepository: PatnikRepository,
|
---|
23 | private val tipbiletRepository: TipbiletRepository,
|
---|
24 | ) {
|
---|
25 | fun getTicketsByUser(): List<TicketResponse> {
|
---|
26 | val user = korisnikService.getUser()
|
---|
27 | val patnik =
|
---|
28 | patnikRepository.findByKorisnik(user) ?: throw NotFoundException("Patnik not found")
|
---|
29 |
|
---|
30 | return biletRepository.findAllByPatnik(patnik).map { mapper.toTicketResponse(it) }
|
---|
31 | }
|
---|
32 |
|
---|
33 | fun buyTicket(request: TicketBuyRequest): TicketResponse {
|
---|
34 | val user = korisnikService.getUser()
|
---|
35 | val patnik =
|
---|
36 | patnikRepository.findByKorisnik(user) ?: throw NotFoundException("Patnik not found")
|
---|
37 | val type = tipbiletRepository.findByIdOrNull(request.type) ?: throw NotFoundException("Type not found")
|
---|
38 |
|
---|
39 | val ticket =
|
---|
40 | biletRepository.save(
|
---|
41 | Bilet(
|
---|
42 | id = 0L,
|
---|
43 | status = BiletEnum.INACTIVE,
|
---|
44 | tip = type,
|
---|
45 | datumKupuvanje = Timestamp.valueOf(LocalDateTime.now()),
|
---|
46 | patnik = patnik,
|
---|
47 | datumAktivacija = null,
|
---|
48 | ),
|
---|
49 | )
|
---|
50 |
|
---|
51 | return mapper.toTicketResponse(ticket)
|
---|
52 | }
|
---|
53 |
|
---|
54 | fun activateTicket(id: Long): TicketResponse {
|
---|
55 | val user = korisnikService.getUser()
|
---|
56 | val patnik =
|
---|
57 | patnikRepository.findByKorisnik(user) ?: throw NotFoundException("Patnik not found")
|
---|
58 |
|
---|
59 | val ticket = biletRepository.findByIdAndPatnik(id, patnik) ?: throw NotFoundException("Ticket not found")
|
---|
60 |
|
---|
61 | ticket.status = BiletEnum.ACTIVE
|
---|
62 | ticket.datumAktivacija = Timestamp.valueOf(LocalDateTime.now())
|
---|
63 | return mapper.toTicketResponse(biletRepository.save(ticket))
|
---|
64 | }
|
---|
65 | }
|
---|