1 | package mk.ukim.finki.busngobackend.service
|
---|
2 |
|
---|
3 | import mk.ukim.finki.busngobackend.api.responses.AdminUsersResponse
|
---|
4 | import mk.ukim.finki.busngobackend.domain.entities.KorisnikRole
|
---|
5 | import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
|
---|
6 | import mk.ukim.finki.busngobackend.domain.views.*
|
---|
7 | import mk.ukim.finki.busngobackend.repository.*
|
---|
8 | import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
|
---|
9 | import org.springframework.data.repository.findByIdOrNull
|
---|
10 | import org.springframework.stereotype.Service
|
---|
11 |
|
---|
12 | @Service
|
---|
13 | class AdminService(
|
---|
14 | private val avgTimeDIffsRepository: AvgTimeDIffsRepository,
|
---|
15 | private val finesByLineRepository: FinesByLineRepository,
|
---|
16 | private val commutesByHourRepository: CommutesByHourRepository,
|
---|
17 | private val commutesByLineRepository: CommutesByLineRepository,
|
---|
18 | private val numberOfPassengersByLineAndStationRepository: NumberOfPassengersByLineAndStationRepository,
|
---|
19 | private val ticketSalesRepository: TicketSalesRepository,
|
---|
20 | private val totalIncomeRepository: TotalIncomeRepository,
|
---|
21 | private val korisnikService: KorisnikService,
|
---|
22 | private val korisnikRepository: KorisnikRepository,
|
---|
23 | private val roleRepository: RoleRepository,
|
---|
24 | private val korisnikRoleRepository: KorisnikRoleRepository,
|
---|
25 | private val authService: AuthService,
|
---|
26 | ) {
|
---|
27 | fun getAverageTimeBetweenStations(): List<AverageTimeBetweenStations> {
|
---|
28 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
29 | return avgTimeDIffsRepository.findAll()
|
---|
30 | }
|
---|
31 |
|
---|
32 | fun getFinesByLine(): List<FinesByLine> {
|
---|
33 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
34 | return finesByLineRepository.findAll()
|
---|
35 | }
|
---|
36 |
|
---|
37 | fun getCommutesByHour(): List<CommutesByHour> {
|
---|
38 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
39 | return commutesByHourRepository.findAll()
|
---|
40 | }
|
---|
41 |
|
---|
42 | fun getCommutesByLine(): List<CommutesByLine> {
|
---|
43 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
44 | return commutesByLineRepository.findAll()
|
---|
45 | }
|
---|
46 |
|
---|
47 | fun getNumberOfPassengersByLineAndStation(): List<NumberOfPassengersByLineAndStation> {
|
---|
48 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
49 | return numberOfPassengersByLineAndStationRepository.findAll()
|
---|
50 | }
|
---|
51 |
|
---|
52 | fun getTicketSales(): List<TicketSales> {
|
---|
53 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
54 | return ticketSalesRepository.findAll()
|
---|
55 | }
|
---|
56 |
|
---|
57 | fun getTotalIncome(): List<TotalIncome> {
|
---|
58 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
59 | return totalIncomeRepository.findAll()
|
---|
60 | }
|
---|
61 |
|
---|
62 | fun getAllUsers(): List<AdminUsersResponse> {
|
---|
63 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
64 | return korisnikService.getAllUsers()
|
---|
65 | }
|
---|
66 |
|
---|
67 | fun updateRolesForUser(
|
---|
68 | id: Long,
|
---|
69 | roleNames: List<String>,
|
---|
70 | ): List<AdminUsersResponse> {
|
---|
71 | authService.hasAuthority(RoleEnum.ROLE_ADMIN)
|
---|
72 | val user = korisnikRepository.findByIdOrNull(id) ?: throw NotFoundException("User not found")
|
---|
73 | val roles = roleNames.flatMap { role -> roleRepository.findAllByNameLikeIgnoreCase(role) }
|
---|
74 |
|
---|
75 | val allByKorisnik = korisnikRoleRepository.findAllByKorisnik(user)
|
---|
76 |
|
---|
77 | val rolesToDelete = allByKorisnik.filter { it.role !in roles }
|
---|
78 | val rolesToAdd =
|
---|
79 | roles.filter { it !in allByKorisnik.map { r -> r.role } }.map {
|
---|
80 | KorisnikRole(
|
---|
81 | id = 0L,
|
---|
82 | korisnik = user,
|
---|
83 | role = it,
|
---|
84 | )
|
---|
85 | }
|
---|
86 |
|
---|
87 | korisnikRoleRepository.deleteAll(rolesToDelete)
|
---|
88 | korisnikRoleRepository.saveAll(rolesToAdd)
|
---|
89 | return getAllUsers()
|
---|
90 | }
|
---|
91 | }
|
---|