2022-09-05 18:10:35 +00:00
|
|
|
// deleteOfficeHourHandler
|
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-10-30 17:07:40 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-09-05 18:10:35 +00:00
|
|
|
"net/http"
|
2022-10-30 17:07:40 +00:00
|
|
|
"officeHours/models"
|
2022-09-24 13:01:33 +00:00
|
|
|
"officeHours/templating"
|
2022-09-05 18:10:35 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
// Offer a table of all office hours to delete,
|
|
|
|
// verify the corresponding mail address and
|
|
|
|
// then send a confirmation mail.
|
2022-09-05 18:10:35 +00:00
|
|
|
func (b *BaseHandler) DeleteOfficeHourHandler(w http.ResponseWriter, req *http.Request) {
|
2023-01-04 11:35:24 +00:00
|
|
|
SendSecurityHeaders(w, req)
|
2022-09-05 18:10:35 +00:00
|
|
|
if req.FormValue("id") != "" {
|
2022-10-30 17:07:40 +00:00
|
|
|
id, err := strconv.Atoi(req.FormValue("id"))
|
2022-09-05 18:10:35 +00:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2022-10-30 17:07:40 +00:00
|
|
|
templating.ServeTemplate(w, "deleteFailure", struct{ Error string }{"Id konnte nicht gelesen werden."})
|
|
|
|
return
|
2022-09-05 18:10:35 +00:00
|
|
|
}
|
2022-10-30 17:07:40 +00:00
|
|
|
officeHour, err := b.officeHourRepo.FindById(id)
|
2022-09-05 18:10:35 +00:00
|
|
|
if err != nil {
|
2022-10-30 17:07:40 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
templating.ServeTemplate(w, "deleteFailure", struct{ Error string }{"Sprechstunde wurde nicht gefunden."})
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
templating.ServeTemplate(w, "deleteFailure", struct{ Error error }{err})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.FormValue("email") == "" {
|
|
|
|
templating.ServeTemplate(w, "deleteMailForm", struct {
|
|
|
|
Error string
|
|
|
|
OfficeHour models.OfficeHour
|
|
|
|
}{"", officeHour})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.FormValue("email") != officeHour.Tutor.Email {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
templating.ServeTemplate(w, "deleteMailForm", struct {
|
|
|
|
Error string
|
|
|
|
OfficeHour models.OfficeHour
|
|
|
|
}{"Die Sprechstunde wurde nicht mit dieser Mailadresse angegeben.", officeHour})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = b.requestRepo.Add(officeHour, models.RequestDelete)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
templating.ServeTemplate(w, "deleteFailure", struct{ Error error }{err})
|
|
|
|
return
|
2022-09-05 18:10:35 +00:00
|
|
|
}
|
2022-10-30 17:07:40 +00:00
|
|
|
templating.ServeTemplate(w, "deleteSuccess", nil)
|
2022-09-05 18:10:35 +00:00
|
|
|
} else {
|
|
|
|
officeHours, _ := b.officeHourRepo.GetAll(true)
|
2022-09-19 12:46:16 +00:00
|
|
|
timetable, slots := b.GetTimetable(officeHours)
|
|
|
|
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, true))
|
2022-09-05 18:10:35 +00:00
|
|
|
}
|
|
|
|
}
|