From 62ad7a3b3620e271ff1f4ec9412c37e672dcd597 Mon Sep 17 00:00:00 2001 From: Seil0 Date: Thu, 23 May 2019 14:56:16 +0200 Subject: [PATCH] courses is now CourseList, added lessonSubjectList & lesson request closes #1, #2 --- .../mosad/thecitadelofricks/APIController.kt | 58 ++++++++++++++++++- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt index 433a308..d9d6d99 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt @@ -39,6 +39,7 @@ import java.net.URL import java.time.LocalDateTime import java.util.* import kotlin.collections.ArrayList +import kotlin.collections.HashSet import kotlin.concurrent.scheduleAtFixedRate @RestController @@ -88,9 +89,15 @@ class APIController { } } + @Deprecated("courses is replaced by courseList") @RequestMapping("/courses") fun courses(): CoursesList { - logger.info("courses request at ${LocalDateTime.now()}!") + return courseList() + } + + @RequestMapping("/courseList") + fun courseList(): CoursesList { + logger.info("courseList request at ${LocalDateTime.now()}!") requestCount++ return CoursesList(CoursesMeta(coursesLastUpdate, coursesLinkList.size), coursesLinkList) } @@ -109,8 +116,48 @@ class APIController { ): TimetableCourseWeek { logger.info("timetable request at ${LocalDateTime.now()}!") requestCount++ - checkTimetableCourse(courseName, week) // check if we need to update and perform the update if so - return timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null) + return getTimetable(courseName, week) + } + + @RequestMapping("/lessonSubjectList") + fun lessonSubjectList( + @RequestParam(value = "courseName", defaultValue = "AI4") courseName: String, + @RequestParam(value = "week", defaultValue = "0") week: Int + ): HashSet { + val lessonSubjectList = ArrayList() + logger.info("lessonSubjectList request at ${LocalDateTime.now()}!") + requestCount++ + + // get every lesson subject for the given week + val flatMap = getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() } + flatMap.forEach { + it.stream().filter { x -> x.lessonSubject.isNotEmpty() }.findAny().ifPresent { x -> lessonSubjectList.add(x.lessonSubject) } + } + + return HashSet(lessonSubjectList) + } + + @RequestMapping("/lesson") + fun lesson( + @RequestParam(value = "courseName", defaultValue = "AI4") courseName: String, + @RequestParam(value = "lessonSubject", defaultValue = "Mathematik 4") lessonSubject: String, + @RequestParam(value = "week", defaultValue = "0") week: Int + ): ArrayList { + val lessonList = ArrayList() + + // get all lessons from the weeks timetable + val flatMap = getTimetable(courseName, week).timetable.days.flatMap { it.timeslots.asIterable() } + flatMap.forEach { + it.forEach { lesson -> + if(lesson.lessonSubject.contains(lessonSubject)) { + lessonList.add(lesson) + } + } + + //it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> println("${x.lessonSubject}, ${x.lessonTeacher}") } + } + + return lessonList } @RequestMapping("/status") @@ -213,6 +260,11 @@ class APIController { } } + private fun getTimetable(courseName: String, week: Int): TimetableCourseWeek { + checkTimetableCourse(courseName, week) // check if we need to update and perform the update if so + return timetableList.stream().filter { x -> x.meta.courseName == courseName && x.meta.week == week }.findAny().orElse(null) + } + private fun initUpdates() = runBlocking { // get all courses on startup val jobCourseUpdate = GlobalScope.async{