95 lines
2.4 KiB
Go
95 lines
2.4 KiB
Go
package publicreport
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
"github.com/Gleipnir-Technology/nidus-sync/htmlpage"
|
|
"github.com/rs/zerolog/log"
|
|
"github.com/stephenafamo/bob/dialect/psql"
|
|
"github.com/stephenafamo/bob/dialect/psql/um"
|
|
)
|
|
|
|
func getRoot(w http.ResponseWriter, r *http.Request) {
|
|
htmlpage.RenderOrError(
|
|
w,
|
|
Root,
|
|
ContextRoot{},
|
|
)
|
|
}
|
|
|
|
func getRegisterNotificationsComplete(w http.ResponseWriter, r *http.Request) {
|
|
report := r.URL.Query().Get("report")
|
|
htmlpage.RenderOrError(
|
|
w,
|
|
RegisterNotificationsComplete,
|
|
ContextRegisterNotificationsComplete{
|
|
ReportID: report,
|
|
},
|
|
)
|
|
}
|
|
func getStatus(w http.ResponseWriter, r *http.Request) {
|
|
htmlpage.RenderOrError(
|
|
w,
|
|
Status,
|
|
ContextStatus{},
|
|
)
|
|
}
|
|
func postRegisterNotifications(w http.ResponseWriter, r *http.Request) {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
respondError(w, "Failed to parse form", err, http.StatusBadRequest)
|
|
return
|
|
}
|
|
consent := r.PostFormValue("consent")
|
|
email := r.PostFormValue("email")
|
|
phone := r.PostFormValue("phone")
|
|
report_id := r.PostFormValue("report_id")
|
|
if consent != "on" {
|
|
respondError(w, "You must consent", nil, http.StatusBadRequest)
|
|
return
|
|
}
|
|
result, err := psql.Update(
|
|
um.Table("publicreport.quick"),
|
|
um.SetCol("reporter_email").ToArg(email),
|
|
um.SetCol("reporter_phone").ToArg(phone),
|
|
um.Where(psql.Quote("public_id").EQ(psql.Arg(report_id))),
|
|
).Exec(r.Context(), db.PGInstance.BobDB)
|
|
if err != nil {
|
|
respondError(w, "Failed to update report", err, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
rowcount, err := result.RowsAffected()
|
|
if err != nil {
|
|
respondError(w, "Failed to get rows affected", err, http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if rowcount == 0 {
|
|
http.Redirect(w, r, fmt.Sprintf("/error?code=no-rows-affected&report=%s", report_id), http.StatusFound)
|
|
} else {
|
|
http.Redirect(w, r, fmt.Sprintf("/register-notifications-complete?report=%s", report_id), http.StatusFound)
|
|
}
|
|
}
|
|
|
|
// Respond with an error that is visible to the user
|
|
func respondError(w http.ResponseWriter, m string, e error, s int) {
|
|
log.Warn().Int("status", s).Err(e).Str("user message", m).Msg("Responding with an error")
|
|
http.Error(w, m, s)
|
|
}
|
|
|
|
func boolFromForm(r *http.Request, k string) bool {
|
|
s := r.PostFormValue(k)
|
|
if s == "on" {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func postFormValueOrNone(r *http.Request, k string) string {
|
|
v := r.PostFormValue(k)
|
|
if v == "" {
|
|
return "none"
|
|
}
|
|
return v
|
|
}
|