sprechstunden-go/controllers/deleteOfficeHourHandler.go

64 lines
2 KiB
Go
Raw Permalink Normal View History

2022-09-05 18:10:35 +00:00
// deleteOfficeHourHandler
package controllers
import (
"database/sql"
"errors"
2022-09-05 18:10:35 +00:00
"net/http"
"officeHours/models"
"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) {
SendSecurityHeaders(w, req)
2022-09-05 18:10:35 +00:00
if req.FormValue("id") != "" {
id, err := strconv.Atoi(req.FormValue("id"))
2022-09-05 18:10:35 +00:00
if err != nil {
w.WriteHeader(http.StatusBadRequest)
templating.ServeTemplate(w, "deleteFailure", struct{ Error string }{"Id konnte nicht gelesen werden."})
return
2022-09-05 18:10:35 +00:00
}
officeHour, err := b.officeHourRepo.FindById(id)
2022-09-05 18:10:35 +00:00
if err != nil {
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
2023-07-09 13:52:09 +00:00
}{"Die Sprechstunde wurde nicht mit dieser Mailadresse angelegt.", 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
}
templating.ServeTemplate(w, "deleteSuccess", nil)
2022-09-05 18:10:35 +00:00
} else {
officeHours, _ := b.officeHourRepo.GetAll(true)
timetable, slots := b.GetTimetable(officeHours)
b.writeTimetablePage(w, req, b.printTimetable(timetable, slots, true))
2022-09-05 18:10:35 +00:00
}
}