sprechstunden-go/controllers/getHandlers.go
Gonne 8603087ed5 Füge Security-Header hinzu
Das 'unsafe-inline' in der Content-Security-Policy wäre lieber nicht da,
aber dazu müsste man erst die Templates umbauen.
2023-01-04 12:35:24 +01:00

66 lines
2 KiB
Go

package controllers
import (
"html/template"
"log"
"net/http"
"officeHours/models"
"officeHours/templating"
"strconv"
)
func (b *BaseHandler) RootHandler(w http.ResponseWriter, req *http.Request) {
SendSecurityHeaders(w, req)
b.writeTimetablePage(w, req, template.HTML(""))
}
func (b *BaseHandler) GetByRoomHandler(w http.ResponseWriter, req *http.Request) {
SendSecurityHeaders(w, req)
roomId, _ := strconv.Atoi(req.FormValue("raum"))
room, err := b.roomRepo.FindById(roomId)
if err != nil {
b.RootHandler(w, req)
return
}
officeHours, err := b.officeHourRepo.FindByRoom(room, true)
if err != nil {
log.Printf("Error getting office hours for room %s: %s", room.Name, err.Error())
}
timetable, slots := b.GetTimetable(officeHours)
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, false))
}
func (b *BaseHandler) GetByCourseHandler(w http.ResponseWriter, req *http.Request) {
SendSecurityHeaders(w, req)
courseid, err := strconv.Atoi(req.FormValue("veranstaltung"))
if err != nil {
b.RootHandler(w, req)
return
}
course, err := b.courseRepo.FindById(courseid)
if err != nil {
b.RootHandler(w, req)
return
}
officeHours, err := b.officeHourRepo.FindByCourse(course, true)
if err != nil {
log.Printf("Error getting office hours for course %s: %s", course.Name, err.Error())
}
timetable, slots := b.GetTimetable(officeHours)
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, false))
}
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}
templating.ServeTemplate(w, "index", data)
}