55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package controllers
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"officeHours/models"
|
|
"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
|
|
}
|
|
officeHours, _ := b.officeHourRepo.FindByRoom(room, true)
|
|
timetable, slots := b.GetTimetable(officeHours)
|
|
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, false))
|
|
}
|
|
|
|
func (b *BaseHandler) GetByCourseHandler(w http.ResponseWriter, req *http.Request) {
|
|
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, _ := b.officeHourRepo.FindByCourse(course, true)
|
|
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}
|
|
b.serveTemplate(w, "index", data)
|
|
}
|