Skip to content
Snippets Groups Projects
Commit ad96e662 authored by gbe's avatar gbe
Browse files

Redirect to newly created entries

parent 5defd672
No related branches found
No related tags found
No related merge requests found
......@@ -94,5 +94,5 @@ func (h Handler) index(w http.ResponseWriter, r *http.Request) {
return
}
http.Redirect(w, r, "/", http.StatusSeeOther) // TODO: Is this the correct status?
http.Redirect(w, r, "/details?id="+strconv.Itoa(vino.ID), http.StatusSeeOther) // TODO: Is this the correct status?
}
......@@ -107,7 +107,12 @@ func (v Vino) String() string {
return fmt.Sprintf("{Name: %q, Rating: %d}", v.Name, v.Rating)
}
func (v Vino) Store(ctx context.Context, db *sqlx.DB, op storeOperation) (err error) {
func (v *Vino) Store(ctx context.Context, db *sqlx.DB, op storeOperation) (err error) {
if op == Add && v.ID != 0 {
// If we already have an ID, the op needs to be an Update
return errors.New("add with already-set ID")
}
values := map[string]interface{}{
"name": v.Name,
"rating": v.Rating,
......@@ -170,12 +175,25 @@ func (v Vino) Store(ctx context.Context, db *sqlx.DB, op storeOperation) (err er
return err
}
_, err = squirrel.Insert("wines").
res, err := squirrel.Insert("wines").
SetMap(values).
RunWith(tx).
ExecContext(ctx)
if err != nil {
return err
}
id, err := res.LastInsertId()
if err != nil {
return err
}
return err
// If it's an add (i.e. initial creation of an entry) fill in the ID
log.Println("insert result:", res, "id:", id)
v.ID = int(id)
return nil
}
func ListWines(ctx context.Context, db *sqlx.DB) ([]Vino, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment