Newer
Older
"git.c3pb.de/gbe/invinoveritas/storage"
"git.c3pb.de/gbe/invinoveritas/storage/query"
var (
once sync.Once
tpl *template.Template
)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
once.Do(func() {
tpl = template.Must(template.ParseFS(templateFS, "templates/base.tpl", "templates/index.tpl"))
})
httpError(w, r, "can't list wines", err, http.StatusInternalServerError)
return
}
w.Header().Add("content-type", "text/html")
Wines: wines,
User: auth.Get(r),
}
err = tpl.ExecuteTemplate(w, "index.tpl", data)
if err != nil {
level.Error(log.Get(r)).
Log("error", err,
"msg", "can't execute index template")
httpError(w, r, "invalid method", errors.New(r.Method), http.StatusMethodNotAllowed)
if len(name) > 80 || len(name) == 0 {
httpError(w, r, "bad name", errors.New("name must be between 1 and 80 chars"), http.StatusBadRequest)
if ratingVal != "" {
rating, err = strconv.Atoi(ratingVal)
if err != nil {
httpError(w, r, "can't convert rating", err, http.StatusBadRequest)
err = r.ParseMultipartForm(16 * 1024 * 1024)
level.Warn(log.Get(r)).Log("error", err, "msg", "can't parse multipart form data")
err = h.Q.RunTx(r.Context(), func(ctx context.Context, q *query.Queries) error {
params := query.InsertWineParams{
Name: name,
}
if ratingVal != "" {
params.Rating = sql.NullInt32{
Int32: int32(rating),
Valid: true,
}
}
if err != nil {
httpError(w, r, "can't store wine", err, http.StatusInternalServerError)
return err
}
id, err = res.LastInsertId()
if err != nil {
httpError(w, r, "can't store wine", err, http.StatusInternalServerError)
return err
}
level.Debug(log.Get(r)).Log("msg", "inserted new wine", "id", id)
// See if there's an image file uploaded and persist it if that's the case.
if r.MultipartForm == nil || len(r.MultipartForm.File["picture"]) == 0 {
return nil
}
picFile, picHdr, err := r.FormFile("picture")
if err != nil {
httpError(w, r, "can't open picture file", err, http.StatusInternalServerError)
err = storage.AddPicture(ctx, q, int(id), picFile, picHdr.Header.Get("Content-Type"))
httpError(w, r, "can't load picture", err, http.StatusBadRequest)
http.Redirect(w, r, "/details?id="+strconv.Itoa(int(id)), http.StatusSeeOther) // TODO: Is this the correct status?