2022-08-31 20:49:14 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"net/mail"
|
|
|
|
"sprechstundentool/models"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type maskData struct {
|
|
|
|
Courses []models.Course
|
|
|
|
Rooms []models.Room
|
|
|
|
MinuteGranularity int
|
|
|
|
SelectedCourse int
|
|
|
|
SelectedRoom int
|
2022-09-07 19:48:40 +00:00
|
|
|
Date models.Date
|
2022-08-31 20:49:14 +00:00
|
|
|
Duration int
|
|
|
|
Roomname string
|
|
|
|
Name string
|
|
|
|
Email string
|
|
|
|
Info string
|
|
|
|
Errors []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Request) {
|
|
|
|
var errors []string
|
|
|
|
courses, err := b.courseRepo.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
rooms, err := b.roomRepo.GetAll()
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err.Error())
|
|
|
|
}
|
|
|
|
//Parse course
|
|
|
|
courseid, err := strconv.Atoi(req.FormValue("veranstaltung"))
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Veranstaltung muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
course, err := b.courseRepo.FindById(courseid)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Veranstaltung muss existieren.")
|
|
|
|
}
|
|
|
|
//Parse room
|
|
|
|
roomid, err := strconv.Atoi(req.FormValue("raum"))
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Der Raum muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
room, err := b.roomRepo.FindById(roomid)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Der Raum muss existieren.")
|
|
|
|
}
|
|
|
|
//Parse date
|
|
|
|
week, err := strconv.Atoi(req.FormValue("woche"))
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Woche muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
if !(week >= 0 && week <= 2) {
|
|
|
|
errors = append(errors, "Sprechstunden müssen jede, jede gerade oder jede ungerade Woche stattfinden.")
|
|
|
|
}
|
|
|
|
day, err := strconv.Atoi(req.FormValue("tag"))
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Der Tag muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
if !(day >= 0 && day <= 4) {
|
|
|
|
errors = append(errors, "Sprechstunden müssen von Montag bis Freitag stattfinden.")
|
|
|
|
}
|
|
|
|
time := strings.SplitN(req.FormValue("startzeit"), ":", 2)
|
|
|
|
var hour, minute int
|
|
|
|
if len(time) != 2 {
|
|
|
|
errors = append(errors, "Die Zeit muss im Format HH:MM sein.")
|
|
|
|
} else {
|
|
|
|
hour, err = strconv.Atoi(time[0])
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Stunde muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
if !(hour >= 8 && hour <= 17) {
|
|
|
|
errors = append(errors, fmt.Sprintf("Sprechstunden müssen zwischen 08:00 Uhr und 17:%d starten.", 60-models.MinuteGranularity))
|
|
|
|
}
|
|
|
|
minute, err = strconv.Atoi(time[1])
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Minute muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
if !(minute >= 0 && minute <= 60-models.MinuteGranularity && minute%models.MinuteGranularity == 0) {
|
|
|
|
errors = append(errors, fmt.Sprintf("Sprechstunden dürfen nur alle %d Minuten starten.", models.MinuteGranularity))
|
|
|
|
}
|
|
|
|
}
|
2022-09-07 16:26:05 +00:00
|
|
|
date := models.Date{week, day, hour, minute}
|
2022-08-31 20:49:14 +00:00
|
|
|
duration, err := strconv.Atoi(req.FormValue("dauer"))
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, "Die Dauer muss eine ganze Zahl sein.")
|
|
|
|
}
|
|
|
|
if !(duration >= models.MinuteGranularity && duration <= 120 && duration%models.MinuteGranularity == 0) {
|
|
|
|
errors = append(errors, fmt.Sprintf("Sprechstunden müssen zwischen %d und 120 Minuten lang sein.", models.MinuteGranularity))
|
|
|
|
}
|
|
|
|
|
|
|
|
roomname := req.FormValue("raumname")
|
|
|
|
name := req.FormValue("name")
|
|
|
|
if name == "" {
|
|
|
|
errors = append(errors, "Der Name darf nicht leer sein.")
|
|
|
|
}
|
|
|
|
email, err := mail.ParseAddress(req.FormValue("email"))
|
|
|
|
if err != nil {
|
|
|
|
email = &mail.Address{"", req.FormValue("email")}
|
|
|
|
errors = append(errors, "Mailaddresse konnte nicht geparst werden.")
|
|
|
|
} else if !strings.HasSuffix(email.Address, "tu-darmstadt.de") {
|
|
|
|
errors = append(errors, "Mailaddresse muss auf „tu-darmstadt.de“ enden.")
|
|
|
|
}
|
|
|
|
info := req.FormValue("info")
|
|
|
|
|
2022-09-07 16:26:05 +00:00
|
|
|
allowed, err := b.officeHourRepo.AllowedAt(date, duration, room, true)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, fmt.Sprintf("Fehler beim Abfragen der Raumkapazität: %s", err.Error()))
|
|
|
|
} else if !allowed {
|
|
|
|
errors = append(errors, "In dem Raum muss noch Platz für weitere Sprechstunden sein.")
|
|
|
|
}
|
2022-08-31 20:49:14 +00:00
|
|
|
if len(errors) != 0 {
|
|
|
|
var data maskData = maskData{
|
|
|
|
courses,
|
|
|
|
rooms,
|
|
|
|
models.MinuteGranularity,
|
|
|
|
courseid,
|
|
|
|
roomid,
|
2022-09-07 19:48:40 +00:00
|
|
|
date,
|
2022-08-31 20:49:14 +00:00
|
|
|
duration,
|
|
|
|
roomname,
|
|
|
|
name,
|
|
|
|
email.Address,
|
|
|
|
info,
|
|
|
|
errors,
|
|
|
|
}
|
|
|
|
b.writeAddOfficeHourMask(w, req, data)
|
|
|
|
} else {
|
|
|
|
officeHour := models.OfficeHour{0,
|
|
|
|
models.Tutor{0, name, email.Address},
|
2022-09-07 16:26:05 +00:00
|
|
|
date,
|
2022-08-31 20:49:14 +00:00
|
|
|
room,
|
|
|
|
course,
|
|
|
|
info,
|
|
|
|
false,
|
|
|
|
duration,
|
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
id, err := b.officeHourRepo.Add(officeHour)
|
2022-08-31 20:49:14 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
failureTemplate, parseErr := template.ParseFiles("templates/addFailure.html")
|
|
|
|
if parseErr != nil {
|
2022-09-05 15:55:08 +00:00
|
|
|
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
2022-08-31 20:49:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
failureTemplate.Execute(w, err)
|
2022-09-05 15:55:08 +00:00
|
|
|
return
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
officeHour, _ = b.officeHourRepo.FindById(id)
|
|
|
|
b.requestRepo.Add(officeHour, models.RequestActivate)
|
2022-09-05 18:10:35 +00:00
|
|
|
tmpl, err := template.ParseFiles("templates/addSuccess.html")
|
2022-09-05 15:55:08 +00:00
|
|
|
tmpl.Execute(w, struct{}{})
|
2022-08-31 20:49:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Request, data maskData) {
|
|
|
|
tmpl, err := template.ParseFiles("templates/addMask.html")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-09-05 15:55:08 +00:00
|
|
|
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
2022-08-31 20:49:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(data.Errors) != 0 {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
if req.Method == http.MethodGet {
|
|
|
|
data.Errors = []string{}
|
|
|
|
}
|
2022-08-31 20:49:14 +00:00
|
|
|
err = tmpl.Execute(w, data)
|
|
|
|
if err != nil {
|
2022-09-05 15:55:08 +00:00
|
|
|
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
|
2022-08-31 20:49:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|