source: bus-n-go-pavel-216049/bus-n-go-backend/src/main/kotlin/mk/ukim/finki/busngobackend/service/AdminService.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.5 KB
Line 
1package mk.ukim.finki.busngobackend.service
2
3import mk.ukim.finki.busngobackend.api.responses.AdminUsersResponse
4import mk.ukim.finki.busngobackend.domain.entities.KorisnikRole
5import mk.ukim.finki.busngobackend.domain.enums.RoleEnum
6import mk.ukim.finki.busngobackend.domain.views.*
7import mk.ukim.finki.busngobackend.repository.*
8import mk.ukim.finki.busngobackend.service.exceptions.NotFoundException
9import org.springframework.data.repository.findByIdOrNull
10import org.springframework.stereotype.Service
11
12@Service
13class 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}
Note: See TracBrowser for help on using the repository browser.