Datenbanken, erster Versuch
This commit is contained in:
parent
b26544756a
commit
766aedf22d
21 changed files with 678 additions and 320 deletions
73
main.go
73
main.go
|
@ -1,68 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sprechstundentool/controllers"
|
||||
"sprechstundentool/repositories"
|
||||
"sprechstundentool/sqldb"
|
||||
)
|
||||
|
||||
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)
|
||||
db := sqldb.ConnectDB()
|
||||
|
||||
http.HandleFunc("/", root)
|
||||
// Create repos
|
||||
roomRepo := repositories.NewRoomRepo(db)
|
||||
courseRepo := repositories.NewCourseRepo(db)
|
||||
tutorRepo := repositories.NewTutorRepo(db)
|
||||
officeHourRepo := repositories.NewOfficeHourRepo(db, roomRepo, tutorRepo, courseRepo)
|
||||
|
||||
h := controllers.NewBaseHandler(roomRepo, officeHourRepo, courseRepo, tutorRepo)
|
||||
|
||||
http.HandleFunc("/getByRoom", h.GetByRoomHandler)
|
||||
http.HandleFunc("/getByCourse", h.GetByCourseHandler)
|
||||
|
||||
http.HandleFunc("/", h.RootHandler)
|
||||
|
||||
http.ListenAndServe(":8080", nil)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue