Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdminRepo: UserService: Returns single AdminUserDTO with multiple cid #8

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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