source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/kotlin/mk/ukim/finki/busngobackend/service/BiletService.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.6 KB
Line 
1package mk.ukim.finki.busngobackend.service
2
3import mk.ukim.finki.busngobackend.api.requests.TicketBuyRequest
4import mk.ukim.finki.busngobackend.api.responses.TicketResponse
5import mk.ukim.finki.busngobackend.domain.entities.Bilet
6import mk.ukim.finki.busngobackend.domain.enums.BiletEnum
7import mk.ukim.finki.busngobackend.mapper.ClassToDtoMapper
8import mk.ukim.finki.busngobackend.repository.BiletRepository
9import mk.ukim.finki.busngobackend.repository.PatnikRepository
10import mk.ukim.finki.busngobackend.repository.TipbiletRepository
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 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}
Note: See TracBrowser for help on using the repository browser.