mirror of
https://github.com/wulkanowy/wulkanowy.git
synced 2025-01-31 17:12:44 +01:00
Use db student id to distinguish reporting units and recipients (#1254)
This commit is contained in:
parent
464900d95b
commit
d0869b235a
@ -9,6 +9,6 @@ import javax.inject.Singleton
|
||||
@Dao
|
||||
interface RecipientDao : BaseDao<Recipient> {
|
||||
|
||||
@Query("SELECT * FROM Recipients WHERE student_id = :userLoginId AND unit_id = :unitId AND role = :role")
|
||||
suspend fun loadAll(userLoginId: Int, unitId: Int, role: Int): List<Recipient>
|
||||
@Query("SELECT * FROM Recipients WHERE student_id = :studentId AND unit_id = :unitId AND role = :role")
|
||||
suspend fun loadAll(studentId: Int, unitId: Int, role: Int): List<Recipient>
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import java.io.Serializable
|
||||
data class Recipient(
|
||||
|
||||
@ColumnInfo(name = "student_id")
|
||||
val userLoginId: Int,
|
||||
val studentId: Int,
|
||||
|
||||
@ColumnInfo(name = "real_id")
|
||||
val realId: String,
|
||||
|
@ -5,7 +5,7 @@ import io.github.wulkanowy.sdk.pojo.Recipient as SdkRecipient
|
||||
|
||||
fun List<SdkRecipient>.mapToEntities(userLoginId: Int) = map {
|
||||
Recipient(
|
||||
userLoginId = userLoginId,
|
||||
studentId = userLoginId,
|
||||
realId = it.id,
|
||||
realName = it.name,
|
||||
name = it.shortName,
|
||||
|
@ -6,7 +6,7 @@ import io.github.wulkanowy.sdk.pojo.ReportingUnit as SdkReportingUnit
|
||||
|
||||
fun List<SdkReportingUnit>.mapToEntities(student: Student) = map {
|
||||
ReportingUnit(
|
||||
studentId = student.studentId,
|
||||
studentId = student.id.toInt(),
|
||||
unitId = it.id,
|
||||
roles = it.roles,
|
||||
senderId = it.senderId,
|
||||
|
@ -19,22 +19,22 @@ class RecipientRepository @Inject constructor(
|
||||
) {
|
||||
|
||||
suspend fun refreshRecipients(student: Student, unit: ReportingUnit, role: Int) {
|
||||
val new = sdk.init(student).getRecipients(unit.unitId, role).mapToEntities(unit.senderId)
|
||||
val old = recipientDb.loadAll(unit.senderId, unit.unitId, role)
|
||||
val new = sdk.init(student).getRecipients(unit.unitId, role).mapToEntities(unit.studentId)
|
||||
val old = recipientDb.loadAll(unit.studentId, unit.unitId, role)
|
||||
|
||||
recipientDb.deleteAll(old uniqueSubtract new)
|
||||
recipientDb.insertAll(new uniqueSubtract old)
|
||||
}
|
||||
|
||||
suspend fun getRecipients(student: Student, unit: ReportingUnit, role: Int): List<Recipient> {
|
||||
return recipientDb.loadAll(unit.senderId, unit.unitId, role).ifEmpty {
|
||||
return recipientDb.loadAll(unit.studentId, unit.unitId, role).ifEmpty {
|
||||
refreshRecipients(student, unit, role)
|
||||
|
||||
recipientDb.loadAll(unit.senderId, unit.unitId, role)
|
||||
recipientDb.loadAll(unit.studentId, unit.unitId, role)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMessageRecipients(student: Student, message: Message): List<Recipient> {
|
||||
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student.userLoginId)
|
||||
return sdk.init(student).getMessageRecipients(message.messageId, message.senderId).mapToEntities(student.studentId)
|
||||
}
|
||||
}
|
||||
|
@ -18,25 +18,25 @@ class ReportingUnitRepository @Inject constructor(
|
||||
|
||||
suspend fun refreshReportingUnits(student: Student) {
|
||||
val new = sdk.init(student).getReportingUnits().mapToEntities(student)
|
||||
val old = reportingUnitDb.load(student.studentId)
|
||||
val old = reportingUnitDb.load(student.id.toInt())
|
||||
|
||||
reportingUnitDb.deleteAll(old.uniqueSubtract(new))
|
||||
reportingUnitDb.insertAll(new.uniqueSubtract(old))
|
||||
}
|
||||
|
||||
suspend fun getReportingUnits(student: Student): List<ReportingUnit> {
|
||||
return reportingUnitDb.load(student.studentId).ifEmpty {
|
||||
return reportingUnitDb.load(student.id.toInt()).ifEmpty {
|
||||
refreshReportingUnits(student)
|
||||
|
||||
reportingUnitDb.load(student.studentId)
|
||||
reportingUnitDb.load(student.id.toInt())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getReportingUnit(student: Student, unitId: Int): ReportingUnit? {
|
||||
return reportingUnitDb.loadOne(student.studentId, unitId) ?: run {
|
||||
return reportingUnitDb.loadOne(student.id.toInt(), unitId) ?: run {
|
||||
refreshReportingUnits(student)
|
||||
|
||||
return reportingUnitDb.loadOne(student.studentId, unitId)
|
||||
return reportingUnitDb.loadOne(student.id.toInt(), unitId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ class RecipientLocalTest {
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(1, 123, "", 4, "", listOf()), 7) }
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(4, 123, "", 4, "", listOf()), 7) }
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
@ -73,7 +73,7 @@ class RecipientLocalTest {
|
||||
coEvery { recipientDb.deleteAll(any()) } just Runs
|
||||
|
||||
// execute
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(1, 123, "", 4, "", listOf()), 7) }
|
||||
val res = runBlocking { recipientRepository.getRecipients(student, ReportingUnit(4, 123, "", 4, "", listOf()), 7) }
|
||||
|
||||
// verify
|
||||
assertEquals(3, res.size)
|
||||
|
Loading…
x
Reference in New Issue
Block a user