sprechstunden-go/controllers/confirmRequestHandler.go

34 lines
968 B
Go
Raw Normal View History

package controllers
import (
2022-11-04 20:15:38 +00:00
"database/sql"
"errors"
"net/http"
"officeHours/templating"
)
2022-11-04 20:15:38 +00:00
// 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)
2022-11-04 20:15:38 +00:00
if errors.Is(err, sql.ErrNoRows) { // There was no request with this secret
2022-09-26 10:42:08 +00:00
w.WriteHeader(http.StatusNotFound)
templating.ServeTemplate(w, "requestNotFound", nil)
return
}
2022-11-04 20:15:38 +00:00
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 {
2022-09-26 10:42:08 +00:00
w.WriteHeader(http.StatusInternalServerError)
templating.ServeTemplate(w, "executeFailure", err.Error())
2022-09-07 19:48:40 +00:00
return
}
templating.ServeTemplate(w, "executeSuccess", nil)
}