From 254719f22f5564e0455570d27c82bbde390bcbc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Tue, 26 Mar 2024 19:02:35 +0100 Subject: [PATCH] Remove classId from semester query when eduOne (#2509) --- app/build.gradle | 2 +- .../wulkanowy/data/db/dao/SemesterDao.kt | 12 +++++++ .../data/repositories/SemesterRepository.kt | 6 ++-- .../data/repositories/StudentRepository.kt | 4 +-- .../repositories/SemesterRepositoryTest.kt | 33 +++++++------------ 5 files changed, 30 insertions(+), 27 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 92633dd3e..1a74e198e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -195,7 +195,7 @@ ext { } dependencies { - implementation 'io.github.wulkanowy:sdk:2.5.4' + implementation 'io.github.wulkanowy:sdk:2.5.5-SNAPSHOT' coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4' diff --git a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt index 4d171907c..f50d95593 100644 --- a/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt +++ b/app/src/main/java/io/github/wulkanowy/data/db/dao/SemesterDao.kt @@ -5,6 +5,7 @@ import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query import io.github.wulkanowy.data.db.entities.Semester +import io.github.wulkanowy.data.db.entities.Student import javax.inject.Singleton @Singleton @@ -16,4 +17,15 @@ interface SemesterDao : BaseDao { @Query("SELECT * FROM Semesters WHERE student_id = :studentId AND class_id = :classId") suspend fun loadAll(studentId: Int, classId: Int): List + + @Query("SELECT * FROM Semesters WHERE student_id = :studentId") + suspend fun loadAll(studentId: Int): List + + suspend fun loadAll(student: Student): List { + return if (student.isEduOne == true) { + loadAll(student.studentId) + } else { + loadAll(student.studentId, student.classId) + } + } } diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/SemesterRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/SemesterRepository.kt index da21f59ac..e15f93099 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/SemesterRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/SemesterRepository.kt @@ -27,11 +27,11 @@ class SemesterRepository @Inject constructor( forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false ) = withContext(dispatchers.io) { - val semesters = semesterDb.loadAll(student.studentId, student.classId) + val semesters = semesterDb.loadAll(student) if (isShouldFetch(student, semesters, forceRefresh, refreshOnNoCurrent)) { refreshSemesters(student) - semesterDb.loadAll(student.studentId, student.classId) + semesterDb.loadAll(student) } else semesters } @@ -66,7 +66,7 @@ class SemesterRepository @Inject constructor( if (new.isEmpty()) return Timber.i("Empty semester list!") - val old = semesterDb.loadAll(student.studentId, student.classId) + val old = semesterDb.loadAll(student) semesterDb.removeOldAndSaveNew( oldItems = old uniqueSubtract new, newItems = new uniqueSubtract old, diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/StudentRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/StudentRepository.kt index df47d7a63..4fedcae96 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/StudentRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/StudentRepository.kt @@ -123,7 +123,7 @@ class StudentRepository @Inject constructor( return } - val currentStudentSemesters = semesterDb.loadAll(student.studentId, student.classId) + val currentStudentSemesters = semesterDb.loadAll(student) if (currentStudentSemesters.isEmpty()) { Timber.d("Check isAuthorized: apply empty semesters workaround") semesterDb.insertSemesters( @@ -209,7 +209,7 @@ class StudentRepository @Inject constructor( studentDb.update(studentName) semesterDb.removeOldAndSaveNew( - oldItems = semesterDb.loadAll(student.studentId, semester.classId), + oldItems = semesterDb.loadAll(student), newItems = newCurrentApiStudent.semesters.mapToEntities(newCurrentApiStudent.studentId) ) } diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/SemesterRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/SemesterRepositoryTest.kt index 3a18ac48a..57ace3e1d 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/SemesterRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/SemesterRepositoryTest.kt @@ -3,6 +3,7 @@ package io.github.wulkanowy.data.repositories import io.github.wulkanowy.TestDispatchersProvider import io.github.wulkanowy.createWulkanowySdkFactoryMock import io.github.wulkanowy.data.db.dao.SemesterDao +import io.github.wulkanowy.data.db.entities.Student import io.github.wulkanowy.data.mappers.mapToEntities import io.github.wulkanowy.getSemesterEntity import io.github.wulkanowy.getSemesterPojo @@ -50,7 +51,7 @@ class SemesterRepositoryTest { getSemesterPojo(1, 2, now().minusMonths(3), now()) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList() + coEvery { semesterDb.loadAll(student) } returns emptyList() coEvery { sdk.getSemesters() } returns semesters coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs @@ -76,12 +77,7 @@ class SemesterRepositoryTest { getSemesterPojo(123, 2, now().minusMonths(3), now()) ) - coEvery { - semesterDb.loadAll( - student.studentId, - student.classId - ) - } returns badSemesters.mapToEntities(student.studentId) + coEvery { semesterDb.loadAll(student) } returns badSemesters.mapToEntities(student.studentId) coEvery { sdk.getSemesters() } returns goodSemesters coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs @@ -103,7 +99,7 @@ class SemesterRepositoryTest { getSemesterPojo(2, 3, now(), now().plusMonths(6)), ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returnsMany listOf( + coEvery { semesterDb.loadAll(any()) } returnsMany listOf( badSemesters.mapToEntities(student.studentId), badSemesters.mapToEntities(student.studentId), goodSemesters.mapToEntities(student.studentId) @@ -125,7 +121,7 @@ class SemesterRepositoryTest { getSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1)) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters + coEvery { semesterDb.loadAll(student) } returns semesters val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) @@ -138,7 +134,7 @@ class SemesterRepositoryTest { getSemesterEntity(1, 2, now().minusMonths(3), now()) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters + coEvery { semesterDb.loadAll(student) } returns semesters val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) @@ -151,7 +147,7 @@ class SemesterRepositoryTest { getSemesterEntity(1, 2, now(), now()) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters + coEvery { semesterDb.loadAll(student) } returns semesters val items = runBlocking { semesterRepository.getSemesters(student) } assertEquals(2, items.size) @@ -164,7 +160,7 @@ class SemesterRepositoryTest { getSemesterPojo(1, 2, now().minusMonths(3), now()) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList() + coEvery { semesterDb.loadAll(student) } returns emptyList() coEvery { sdk.getSemesters() } returns semesters coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs @@ -193,12 +189,7 @@ class SemesterRepositoryTest { getSemesterPojo(2, 2, now().plusMonths(5), now().plusMonths(11)), ) - coEvery { - semesterDb.loadAll( - student.studentId, - student.classId - ) - } returns semestersWithNoCurrent + coEvery { semesterDb.loadAll(student) } returns semestersWithNoCurrent coEvery { sdk.getSemesters() } returns newSemesters coEvery { semesterDb.removeOldAndSaveNew(any(), any()) } just Runs @@ -214,7 +205,7 @@ class SemesterRepositoryTest { getSemesterEntity(1, 2, now().minusMonths(1), now().plusMonths(1)) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters + coEvery { semesterDb.loadAll(student) } returns semesters val items = semesterRepository.getSemesters(student, refreshOnNoCurrent = true) assertEquals(2, items.size) @@ -227,14 +218,14 @@ class SemesterRepositoryTest { getSemesterEntity(1, 1, now(), now()) ) - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns semesters + coEvery { semesterDb.loadAll(student) } returns semesters runBlocking { semesterRepository.getCurrentSemester(student) } } @Test(expected = RuntimeException::class) fun getCurrentSemester_emptyList() { - coEvery { semesterDb.loadAll(student.studentId, student.classId) } returns emptyList() + coEvery { semesterDb.loadAll(student) } returns emptyList() coEvery { sdk.getSemesters() } returns emptyList() runBlocking { semesterRepository.getCurrentSemester(student) }