Verbessere Dokumentation

This commit is contained in:
Gonne 2022-11-04 21:15:38 +01:00
parent ec24c6c4dc
commit fe54d76ab2
14 changed files with 75 additions and 34 deletions

View file

@ -28,6 +28,7 @@ type maskData struct {
Config config.Config
}
// Offer a form to add office hours and validate its input on receiving.
func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Request) {
var errors []string
courses, err := b.courseRepo.GetAll()
@ -123,6 +124,9 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
} else if !allowed {
errors = append(errors, "In dem Raum muss noch Platz für weitere Sprechstunden sein.")
}
// If there were errors in the data for the new office hour,
// answer with the form prefilled with the sent data.
if len(errors) != 0 {
var data maskData = maskData{
courses,
@ -141,6 +145,8 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
}
b.writeAddOfficeHourMask(w, req, data)
} else {
// if the data for a new office hour was sent correctly, save it.
officeHour := models.OfficeHour{Id: 0,
Tutor: models.Tutor{Id: 0, Name: name, Email: email.Address},
Date: date,
@ -166,12 +172,11 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ
log.Printf("Error adding request: %s", err.Error())
}
templating.ServeTemplate(w, "addSuccess", nil)
}
}
func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Request, data maskData) {
if req.Method == http.MethodGet {
if req.Method == http.MethodGet { // if the current request is GET, we assume no office hour addition was tried so far and reset the errors.
data.Errors = []string{}
}
if len(data.Errors) != 0 {

View file

@ -1,19 +1,27 @@
package controllers
import (
"database/sql"
"errors"
"net/http"
"officeHours/templating"
)
// Check the secret token for requests and execute the request for correct tokens
func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Request) {
secret := req.FormValue("code")
request, err := b.requestRepo.FindBySecret(secret)
if err != nil {
if errors.Is(err, sql.ErrNoRows) { // There was no request with this secret
w.WriteHeader(http.StatusNotFound)
templating.ServeTemplate(w, "requestNotFound", nil)
return
}
if err != nil { // Some other error happened finding the request with this secret
w.WriteHeader(http.StatusInternalServerError)
templating.ServeTemplate(w, "executeFailure", err.Error())
return
}
err = b.requestRepo.Execute(request)
if err != nil {
@ -22,5 +30,4 @@ func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Req
return
}
templating.ServeTemplate(w, "executeSuccess", nil)
}

View file

@ -10,6 +10,9 @@ import (
"strconv"
)
// Offer a table of all office hours to delete,
// verify the corresponding mail address and
// then send a confirmation mail.
func (b *BaseHandler) DeleteOfficeHourHandler(w http.ResponseWriter, req *http.Request) {
if req.FormValue("id") != "" {
id, err := strconv.Atoi(req.FormValue("id"))