Add info about three switches to Semester

This commit is contained in:
Mikołaj Pich 2020-02-07 00:44:56 +01:00
parent a8c46cca01
commit e5540814bb
No known key found for this signature in database
GPG key ID: F62B26E36D4C4BAA
8 changed files with 98 additions and 52 deletions

View file

@ -13,5 +13,8 @@ data class Semester(
val start: LocalDate = now(),
val end: LocalDate = now(),
val classId: Int = 0,
val unitId: Int = 0
val unitId: Int = 0,
val feesEnabled: Boolean,
val menuEnabled: Boolean,
val completedLessonsEnabled: Boolean
)

View file

@ -22,12 +22,17 @@ class StudentAndParentStartRepository(
api.getDiaryInfo(diary.id, "/$symbol/$schoolSymbol/Oceny.mvc/Wszystkie").map { res ->
if (!res.title.endsWith("Oceny")) throw VulcanException("Unknow page with title: ${res.title}")
res.semesters.map {
Semester(diary.id,
diary.name,
diary.name.substringAfter(" ").toInt(),
it.semesterId,
it.semesterNumber,
"selected" == it.current && "selected" == diary.current)
Semester(
diaryId = diary.id,
diaryName = diary.name,
schoolYear = diary.name.substringAfter(" ").toInt(),
semesterId = it.semesterId,
semesterNumber = it.semesterNumber,
current = "selected" == it.current && "selected" == diary.current,
feesEnabled = false,
menuEnabled = false,
completedLessonsEnabled = false
)
}
}
}.toList().map { it.flatten() }

View file

@ -1,5 +1,6 @@
package io.github.wulkanowy.sdk.scrapper.repository
import io.github.wulkanowy.sdk.scrapper.getScriptParam
import io.github.wulkanowy.sdk.scrapper.interceptor.ErrorHandlerTransformer
import io.github.wulkanowy.sdk.scrapper.register.Semester
import io.github.wulkanowy.sdk.scrapper.service.StudentService
@ -15,44 +16,56 @@ class StudentStartRepository(
) {
companion object {
@JvmStatic private val logger = LoggerFactory.getLogger(this::class.java)
@JvmStatic
private val logger = LoggerFactory.getLogger(this::class.java)
}
fun getSemesters(): Single<List<Semester>> {
return api.getDiaries()
.compose(ErrorHandlerTransformer())
.map { it.data.orEmpty() }
.map { diaries ->
diaries.asSequence()
.filter { diary -> diary.semesters?.isNotEmpty() ?: false }
.filter { diary -> diary.studentId == studentId }
.filter { diary -> diary.semesters!![0].classId == classId }
.map { diary ->
diary.semesters!!.map {
Semester(
diaryId = diary.diaryId,
diaryName = "${diary.level}${diary.symbol}",
schoolYear = diary.year,
semesterId = it.id,
semesterNumber = it.number,
start = it.start.toLocalDate(),
end = it.end.toLocalDate(),
current = it.start.toLocalDate() <= now() && it.end.toLocalDate() >= now(),
classId = it.classId,
unitId = it.unitId
)
return api.getStart("Start").flatMap {
api.getUserCache(
getScriptParam("antiForgeryToken", it),
getScriptParam("appGuid", it),
getScriptParam("version", it)
)
}.compose(ErrorHandlerTransformer()).map { it.data }.flatMap { cache ->
api.getDiaries()
.compose(ErrorHandlerTransformer())
.map { it.data.orEmpty() }
.map { diaries ->
diaries.asSequence()
.filter { diary -> diary.semesters?.isNotEmpty() ?: false }
.filter { diary -> diary.studentId == studentId }
.filter { diary -> diary.semesters!![0].classId == classId }
.map { diary ->
diary.semesters!!.map {
Semester(
diaryId = diary.diaryId,
diaryName = "${diary.level}${diary.symbol}",
schoolYear = diary.year,
semesterId = it.id,
semesterNumber = it.number,
start = it.start.toLocalDate(),
end = it.end.toLocalDate(),
current = it.start.toLocalDate() <= now() && it.end.toLocalDate() >= now(),
classId = it.classId,
unitId = it.unitId,
menuEnabled = cache.isMenu,
feesEnabled = cache.isFees,
completedLessonsEnabled = cache.showCompletedLessons
)
}
}
}
.flatten()
.sortedByDescending { it.semesterId }
.toList()
.ifEmpty {
logger.debug("Diaries size: $diaries")
emptyList()
}
}.map {
if (it.isNotEmpty() && it.singleOrNull { semester -> semester.current } == null) it.apply { first().current = true }
else it
}
.flatten()
.sortedByDescending { it.semesterId }
.toList()
.ifEmpty {
logger.debug("Diaries size: $diaries")
emptyList()
}
}
}.map {
if (it.isNotEmpty() && it.singleOrNull { semester -> semester.current } == null) it.apply { first().current = true }
else it
}
}
}

View file

@ -8,6 +8,15 @@ class CacheResponse {
@SerializedName("czyOpiekun")
var isParent: Boolean = false
@SerializedName("czyJadlospis")
var isMenu: Boolean = false
@SerializedName("czyOplaty")
var isFees: Boolean = false
@SerializedName("pokazLekcjeZrealizowane")
var showCompletedLessons: Boolean = false
@SerializedName("poryLekcji")
lateinit var times: List<Time>

View file

@ -5,7 +5,6 @@ import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
import io.github.wulkanowy.sdk.scrapper.register.RegisterTest
import io.github.wulkanowy.sdk.scrapper.register.Semester
import io.reactivex.observers.TestObserver
import okhttp3.mockwebserver.MockResponse
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
@ -28,7 +27,9 @@ class StudentStartRepositoryTest : BaseLocalTest() {
@Test
fun getSemesters() {
server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("UczenDziennik.json").readText()))
server.enqueue("WitrynaUcznia.html", RegisterTest::class.java)
server.enqueue("UczenCache.json", RegisterTest::class.java)
server.enqueue("UczenDziennik.json", RegisterTest::class.java)
server.start(3000) //
api.studentId = 1
@ -56,7 +57,9 @@ class StudentStartRepositoryTest : BaseLocalTest() {
@Test
fun getSemesters_empty() {
server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("UczenDziennik.json").readText()))
server.enqueue("WitrynaUcznia.html", RegisterTest::class.java)
server.enqueue("UczenCache.json", RegisterTest::class.java)
server.enqueue("UczenDziennik.json", RegisterTest::class.java)
server.start(3000) //
api.studentId = 1
@ -74,7 +77,9 @@ class StudentStartRepositoryTest : BaseLocalTest() {
@Test
fun getSemesters_studentWithMultiClasses() {
server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("UczenDziennik-multi.json").readText()))
server.enqueue("WitrynaUcznia.html", RegisterTest::class.java)
server.enqueue("UczenCache.json", RegisterTest::class.java)
server.enqueue("UczenDziennik-multi.json", RegisterTest::class.java)
server.start(3000) //
api.studentId = 3881
@ -96,7 +101,9 @@ class StudentStartRepositoryTest : BaseLocalTest() {
@Test
fun getSemesters_graduate() {
server.enqueue(MockResponse().setBody(RegisterTest::class.java.getResource("UczenDziennik.json").readText()))
server.enqueue("WitrynaUcznia.html", RegisterTest::class.java)
server.enqueue("UczenCache.json", RegisterTest::class.java)
server.enqueue("UczenDziennik.json", RegisterTest::class.java)
server.start(3000) //
api.studentId = 2

View file

@ -23,7 +23,10 @@ fun List<ScrapperSemester>.mapSemesters(): List<Semester> {
start = it.start,
end = it.end,
classId = it.classId,
unitId = it.unitId
unitId = it.unitId,
feesEnabled = it.feesEnabled,
menuEnabled = it.menuEnabled,
completedLessonsEnabled = it.completedLessonsEnabled
)
}
}
@ -40,7 +43,10 @@ fun List<Student>.mapSemesters(studentId: Int, now: LocalDate = now()): List<Sem
start = it.periodDateFrom.toLocalDate(),
end = it.periodDateTo.toLocalDate(),
classId = it.classId,
unitId = it.reportingUnitId
unitId = it.reportingUnitId,
feesEnabled = false,
menuEnabled = false,
completedLessonsEnabled = false
)
}.mockSecondSemester(now)
}

View file

@ -13,5 +13,8 @@ data class Semester(
val start: LocalDate,
val end: LocalDate,
val classId: Int,
val unitId: Int
val unitId: Int,
val feesEnabled: Boolean,
val menuEnabled: Boolean,
val completedLessonsEnabled: Boolean
)

View file

@ -161,7 +161,7 @@ class SdkRemoteTest {
}
val subjects = sdk.getSubjects().blockingGet()
assertEquals(14, subjects.size)
assertEquals(15, subjects.size)
}
@Test
@ -237,7 +237,7 @@ class SdkRemoteTest {
}
val timetable = sdk.getTimetable(of(2018, 1, 1), of(2018, 1, 2)).blockingGet()
assertEquals(24, timetable.size)
assertEquals(28, timetable.size)
}
@Test