2022-09-05 15:55:08 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-11-04 20:15:38 +00:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-09-05 15:55:08 +00:00
|
|
|
"net/http"
|
2022-09-24 13:01:33 +00:00
|
|
|
"officeHours/templating"
|
2022-09-05 15:55:08 +00:00
|
|
|
)
|
|
|
|
|
2022-11-04 20:15:38 +00:00
|
|
|
// Check the secret token for requests and execute the request for correct tokens
|
2022-09-05 15:55:08 +00:00
|
|
|
func (b *BaseHandler) ConfirmRequestHandler(w http.ResponseWriter, req *http.Request) {
|
2023-01-04 11:35:24 +00:00
|
|
|
SendSecurityHeaders(w, req)
|
2022-09-05 15:55:08 +00:00
|
|
|
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)
|
2022-09-24 13:01:33 +00:00
|
|
|
templating.ServeTemplate(w, "requestNotFound", nil)
|
2022-09-13 05:28:13 +00:00
|
|
|
return
|
2022-09-05 15:55:08 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
2022-09-05 15:55:08 +00:00
|
|
|
|
|
|
|
err = b.requestRepo.Execute(request)
|
|
|
|
if err != nil {
|
2022-09-26 10:42:08 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2022-09-24 13:01:33 +00:00
|
|
|
templating.ServeTemplate(w, "executeFailure", err.Error())
|
2022-09-07 19:48:40 +00:00
|
|
|
return
|
2022-09-05 15:55:08 +00:00
|
|
|
}
|
2022-09-24 13:01:33 +00:00
|
|
|
templating.ServeTemplate(w, "executeSuccess", nil)
|
2022-09-05 15:55:08 +00:00
|
|
|
}
|