69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
func root(w http.ResponseWriter, req *http.Request) {
|
||
|
writeTimetablePage(w, req, template.HTML(""))
|
||
|
}
|
||
|
|
||
|
func getByRoom(w http.ResponseWriter, req *http.Request) {
|
||
|
room, err := strconv.Atoi(req.FormValue("raum"))
|
||
|
if err != nil || room == 0 {
|
||
|
root(w, req)
|
||
|
return
|
||
|
}
|
||
|
writeTimetablePage(w, req, printTimetable(getTimetable(getOfficeHoursByRoom(room))))
|
||
|
}
|
||
|
|
||
|
func getByCourse(w http.ResponseWriter, req *http.Request) {
|
||
|
course, err := strconv.Atoi(req.FormValue("veranstaltung"))
|
||
|
if err != nil || course == 0 {
|
||
|
root(w, req)
|
||
|
return
|
||
|
}
|
||
|
writeTimetablePage(w, req, printTimetable(getTimetable(getOfficeHoursByCourse(course))))
|
||
|
}
|
||
|
|
||
|
func writeTimetablePage(w http.ResponseWriter, req *http.Request, timetable template.HTML) {
|
||
|
room, err := strconv.Atoi(req.FormValue("raum"))
|
||
|
if err != nil {
|
||
|
room = 0
|
||
|
}
|
||
|
course, err := strconv.Atoi(req.FormValue("veranstaltung"))
|
||
|
if err != nil {
|
||
|
course = 0
|
||
|
}
|
||
|
data := struct {
|
||
|
Courses []Course
|
||
|
Rooms []Room
|
||
|
Timetable template.HTML
|
||
|
SelectedRoom int
|
||
|
SelectedCourse int
|
||
|
}{getCourses(), getRooms(), timetable, room, course}
|
||
|
tmpl, err := template.ParseFiles("templates/index.html")
|
||
|
if err != nil {
|
||
|
w.WriteHeader(http.StatusInternalServerError)
|
||
|
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", string(err.Error()))))
|
||
|
return
|
||
|
}
|
||
|
err = tmpl.Execute(w, data)
|
||
|
if err != nil {
|
||
|
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", string(err.Error()))))
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
http.HandleFunc("/getByRoom", getByRoom)
|
||
|
http.HandleFunc("/getByCourse", getByCourse)
|
||
|
|
||
|
http.HandleFunc("/", root)
|
||
|
|
||
|
http.ListenAndServe(":8080", nil)
|
||
|
}
|