Footer in eigenes Template verschoben und Logging verbessert

This commit is contained in:
Gonne 2022-09-19 17:00:19 +02:00
parent c38286bcc5
commit 4f5dc053a0
14 changed files with 40 additions and 44 deletions

View file

@ -2,8 +2,10 @@ package controllers
import (
"fmt"
"log"
"net/http"
"net/mail"
"sprechstundentool/config"
"sprechstundentool/models"
"strconv"
"strings"
@ -22,6 +24,7 @@ type maskData struct {
Email string
Info string
Errors []string
Config config.Config
}
func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Request) {
@ -105,8 +108,8 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
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.")
} else if !strings.HasSuffix(email.Address, b.config.Tutor.MailSuffix) {
errors = append(errors, fmt.Sprintf("Mailaddresse muss auf „%s“ enden.", b.config.Tutor.MailSuffix))
}
info := req.FormValue("info")
@ -130,6 +133,7 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
email.Address,
info,
errors,
b.config,
}
b.writeAddOfficeHourMask(w, req, data)
} else {
@ -155,14 +159,15 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
}
func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Request, data maskData) {
if len(data.Errors) != 0 {
w.WriteHeader(http.StatusBadRequest)
}
if req.Method == http.MethodGet {
data.Errors = []string{}
}
if len(data.Errors) != 0 {
w.WriteHeader(http.StatusBadRequest)
}
err := Templates.ExecuteTemplate(w, "addMask.html", data)
if err != nil {
log.Printf("Template addMask.html could not be parsed: %s", err.Error())
w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error())))
return
}