2019-03-12 22:06:04 +01:00
/ * *
* TheCitadelofRicks
*
2020-01-15 13:42:40 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2019-03-12 22:06:04 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
package org.mosad.thecitadelofricks
2019-10-20 11:52:44 +02:00
import org.mosad.thecitadelofricks.controller.CacheController
import org.mosad.thecitadelofricks.controller.CacheController.Companion.courseList
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLesson
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getLessonSubjectList
import org.mosad.thecitadelofricks.controller.CacheController.Companion.getTimetable
import org.mosad.thecitadelofricks.controller.CacheController.Companion.mensaMenu
2019-10-28 18:39:44 +01:00
import org.mosad.thecitadelofricks.controller.StartupController
2019-10-21 18:46:21 +02:00
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getStatus
2020-02-27 17:42:19 +01:00
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateCourseListRequests
2019-10-21 18:46:21 +02:00
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateMensaMenuRequests
import org.mosad.thecitadelofricks.controller.StatusController.Companion.updateTimetableRequests
2019-03-13 19:12:05 +01:00
import org.slf4j.Logger
import org.slf4j.LoggerFactory
2019-03-12 22:06:04 +01:00
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
2019-03-13 19:12:05 +01:00
import java.time.LocalDateTime
2019-03-13 15:13:53 +01:00
import java.util.*
2020-06-11 17:22:31 +02:00
import kotlin.collections.ArrayList
2019-03-12 22:06:04 +01:00
@RestController
class APIController {
2019-05-28 12:49:25 +02:00
private val logger : Logger = LoggerFactory . getLogger ( APIController :: class . java )
2019-03-12 22:06:04 +01:00
2019-10-21 18:46:21 +02:00
companion object {
2020-06-05 18:23:15 +02:00
const val apiVersion = " 1.2.0 "
2021-10-13 20:03:14 +02:00
const val softwareVersion = " 1.2.8 "
2019-10-21 18:46:21 +02:00
val startTime = System . currentTimeMillis ( ) / 1000
}
2019-03-12 22:06:04 +01:00
2019-10-20 11:52:44 +02:00
init {
2019-10-28 18:39:44 +01:00
StartupController ( )
CacheController ( )
2019-10-20 11:52:44 +02:00
}
2019-05-23 14:56:16 +02:00
@RequestMapping ( " /courseList " )
2020-06-11 17:22:31 +02:00
fun courseList ( ) : CoursesListRet {
2019-05-23 14:56:16 +02:00
logger . info ( " courseList request at ${LocalDateTime.now()} ! " )
2020-02-27 17:42:19 +01:00
updateCourseListRequests ( )
2020-06-11 17:22:31 +02:00
return CoursesListRet ( courseList . meta , ArrayList ( courseList . courses . values ) )
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /mensamenu " )
2019-05-28 12:49:25 +02:00
fun mensamenu ( ) : MensaMenu {
2019-03-13 19:12:05 +01:00
logger . info ( " mensamenu request at ${LocalDateTime.now()} ! " )
2019-10-21 18:46:21 +02:00
updateMensaMenuRequests ( )
2019-10-05 17:17:26 +02:00
return mensaMenu
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /timetable " )
2019-03-15 23:57:31 +01:00
fun timetable (
2020-06-05 18:23:15 +02:00
@RequestParam ( value = " course " , defaultValue = " AI4 " ) courseName : String ,
2019-03-15 23:57:31 +01:00
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : TimetableCourseWeek {
2019-03-13 19:12:05 +01:00
logger . info ( " timetable request at ${LocalDateTime.now()} ! " )
2019-10-05 17:17:26 +02:00
updateTimetableRequests ( courseName )
2019-10-20 11:52:44 +02:00
return getTimetable ( courseName , week )
2019-05-23 14:56:16 +02:00
}
2020-06-05 18:23:15 +02:00
@RequestMapping ( " /subjectList " )
2019-05-23 14:56:16 +02:00
fun lessonSubjectList (
2020-06-05 18:23:15 +02:00
@RequestParam ( value = " course " , defaultValue = " AI4 " ) courseName : String ,
2019-05-23 14:56:16 +02:00
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : HashSet < String > {
2020-06-05 18:23:15 +02:00
logger . info ( " subjectList request at ${LocalDateTime.now()} ! " )
2020-02-27 17:42:19 +01:00
updateTimetableRequests ( courseName )
2019-06-02 16:55:36 +02:00
return getLessonSubjectList ( courseName , week )
}
2019-10-05 17:17:26 +02:00
@RequestMapping ( " /lessons " )
2019-06-02 16:55:36 +02:00
fun lesson (
2020-06-05 18:23:15 +02:00
@RequestParam ( value = " course " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " subject " , defaultValue = " Mathematik 4 " ) lessonSubject : String ,
2019-06-02 16:55:36 +02:00
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : ArrayList < Lesson > {
logger . info ( " lesson request at ${LocalDateTime.now()} ! " )
2020-02-27 17:42:19 +01:00
updateTimetableRequests ( courseName )
2019-06-02 16:55:36 +02:00
return getLesson ( courseName , lessonSubject , week )
}
@RequestMapping ( " /status " )
fun status ( ) : Status {
2019-10-21 18:46:21 +02:00
logger . info ( " status request at ${LocalDateTime.now()} ! " )
2019-06-02 16:55:36 +02:00
return getStatus ( )
}
2019-10-21 18:46:21 +02:00
@RequestMapping ( " /health " )
fun health ( ) : Int {
logger . info ( " health request at ${LocalDateTime.now()} ! " )
return 200
2019-03-13 15:13:53 +01:00
}
2020-06-05 18:23:15 +02:00
/ * *
* Deprecated section
* /
// TODO remove this with API version 2.0.0
@Deprecated ( " courses is replaced by courseList " , replaceWith = ReplaceWith ( " courseList() " ) )
@RequestMapping ( " /courses " )
2020-06-11 17:22:31 +02:00
fun courses ( ) : CoursesListRet {
2020-06-05 18:23:15 +02:00
return courseList ( )
}
// TODO remove this with API version 2.0.0
@Deprecated ( " the parameter courseName is deprecated please use course " , ReplaceWith ( " timetable(courseName, week) " ) )
@RequestMapping ( " /timetable " , params = [ " courseName " , " week " ] )
fun timetableDep (
@RequestParam ( value = " courseName " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : TimetableCourseWeek {
return timetable ( courseName , week )
}
2019-03-12 22:06:04 +01:00
}