Skip to content

Commit

Permalink
AdminRepo: UserService: Returns single AdminUserDTO with multiple cid
Browse files Browse the repository at this point in the history
Solves the problem where same user entity is returned with different
cid in case the user is admin of multiple clubs.

Signed-off-by: Shashank Verma <[email protected]>
  • Loading branch information
shank03 committed Oct 2, 2023
1 parent 19f5ba5 commit 5e131ee
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ data class AdminUserDTO(
val uid: Long,

@JsonProperty("cid")
val clubId: Long,
val clubId: List<Long>,

@JsonProperty("regNo")
val regno: String,
Expand All @@ -31,7 +31,7 @@ data class AdminUserDTO(
@JsonProperty("contact")
val contact: String,
) {
constructor(clubId: Long, user: User) : this(
constructor(clubId: List<Long>, user: User) : this(
uid = user.uid,
clubId = clubId,
regno = user.regno,
Expand Down
21 changes: 12 additions & 9 deletions src/main/kotlin/com/mnnit/moticlubs/repository/AdminRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.mnnit.moticlubs.repository

import com.mnnit.moticlubs.dao.Admin
import com.mnnit.moticlubs.dao.User
import com.mnnit.moticlubs.dto.response.AdminUserDTO
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate
import org.springframework.data.relational.core.query.Criteria
import org.springframework.data.relational.core.query.Query
Expand All @@ -21,17 +20,21 @@ class AdminRepository(
.flatMap { if (it) Mono.just(admin) else db.insert(admin) }

@Transactional
fun findAll(): Flux<AdminUserDTO> = db
fun findAllAdmins(): Flux<User> = db
.databaseClient
.sql("SELECT admin.cid, user.* FROM admin INNER JOIN user ON admin.uid = user.uid ORDER BY user.name")
.sql("SELECT user.* FROM user INNER JOIN (SELECT DISTINCT (admin.uid) FROM admin) as ad ON ad.uid = user.uid;")
.fetch()
.all()
.map {
AdminUserDTO(
clubId = it[Admin::cid.name].toString().toLong(),
user = User(it),
)
}
.map { User(it) }

@Transactional
fun findAllByUid(uid: Long): Mono<List<Long>> = db
.select(
Query.query(Criteria.where(Admin::uid.name).`is`(uid)),
Admin::class.java,
)
.map { it.cid }
.collectList()

@Transactional
fun findAllByCid(cid: Long): Flux<Admin> = db
Expand Down
6 changes: 4 additions & 2 deletions src/main/kotlin/com/mnnit/moticlubs/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ class UserService(

@Cacheable("admins")
fun getAllAdminUsers(): Mono<List<AdminUserDTO>> = adminRepository
.findAll()
.findAllAdmins()
.flatMap { user ->
adminRepository.findAllByUid(user.uid).map { AdminUserDTO(it, user) }
}
.collectList()
.storeCache()

@CacheEvict(cacheNames = ["user", "admins", "all_users", "members"], allEntries = true)
fun updateAvatar(uid: Long, avatar: String): Mono<User> = userRepository.updateAvatar(uid, avatar)
Expand Down

0 comments on commit 5e131ee

Please sign in to comment.