2022-08-29 20:58:19 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2022-09-21 20:24:08 +00:00
|
|
|
"log"
|
2022-08-29 20:58:19 +00:00
|
|
|
"net/http"
|
2022-09-20 10:21:01 +00:00
|
|
|
"officeHours/models"
|
2022-09-24 13:01:33 +00:00
|
|
|
"officeHours/templating"
|
2022-08-29 20:58:19 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b *BaseHandler) RootHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
b.writeTimetablePage(w, req, template.HTML(""))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseHandler) GetByRoomHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
roomId, _ := strconv.Atoi(req.FormValue("raum"))
|
|
|
|
room, err := b.roomRepo.FindById(roomId)
|
|
|
|
if err != nil {
|
|
|
|
b.RootHandler(w, req)
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 20:24:08 +00:00
|
|
|
officeHours, err := b.officeHourRepo.FindByRoom(room, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting office hours for room %s: %s", room.Name, err.Error())
|
|
|
|
}
|
2022-09-19 12:46:16 +00:00
|
|
|
timetable, slots := b.GetTimetable(officeHours)
|
|
|
|
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, false))
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseHandler) GetByCourseHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
courseid, err := strconv.Atoi(req.FormValue("veranstaltung"))
|
|
|
|
if err != nil {
|
|
|
|
b.RootHandler(w, req)
|
2022-09-21 20:18:39 +00:00
|
|
|
return
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
course, err := b.courseRepo.FindById(courseid)
|
|
|
|
if err != nil {
|
|
|
|
b.RootHandler(w, req)
|
|
|
|
return
|
|
|
|
}
|
2022-09-21 20:24:08 +00:00
|
|
|
officeHours, err := b.officeHourRepo.FindByCourse(course, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error getting office hours for course %s: %s", course.Name, err.Error())
|
|
|
|
}
|
2022-09-19 12:46:16 +00:00
|
|
|
timetable, slots := b.GetTimetable(officeHours)
|
|
|
|
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, false))
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseHandler) writeTimetablePage(w http.ResponseWriter, req *http.Request, timetable template.HTML) {
|
|
|
|
courses, _ := b.courseRepo.GetAll()
|
|
|
|
rooms, _ := b.roomRepo.GetAll()
|
|
|
|
selectedRoom, _ := strconv.Atoi(req.FormValue("raum"))
|
|
|
|
selectedCourse, _ := strconv.Atoi(req.FormValue("veranstaltung"))
|
|
|
|
data := struct {
|
|
|
|
Courses []models.Course
|
|
|
|
Rooms []models.Room
|
|
|
|
Timetable template.HTML
|
|
|
|
SelectedRoom int
|
|
|
|
SelectedCourse int
|
|
|
|
}{courses, rooms, timetable, selectedRoom, selectedCourse}
|
2022-09-24 13:01:33 +00:00
|
|
|
templating.ServeTemplate(w, "index", data)
|
2022-08-29 20:58:19 +00:00
|
|
|
}
|