From cc2019ed052b6ef59497a882a8815d283a62a2dc Mon Sep 17 00:00:00 2001 From: Dustin Pianalto Date: Tue, 12 Apr 2022 23:38:39 -0800 Subject: [PATCH] Fix bug with existing items --- internal/postgres/items.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/postgres/items.go b/internal/postgres/items.go index 4c3fbeb..fcf988c 100644 --- a/internal/postgres/items.go +++ b/internal/postgres/items.go @@ -42,7 +42,8 @@ func (s itemService) Item(id int, user *quartermaster.User) (*quartermaster.Item func (s itemService) AddItem(i *quartermaster.Item, l *quartermaster.Location, user *quartermaster.User) (*quartermaster.Item, error) { var err error - if ib, _ := s.GetItemByBarcode(i.Barcode, user); ib == nil { + ib, _ := s.GetItemByBarcode(i.Barcode, user) + if ib == nil { if i.Nutrition != nil { i.Nutrition, err = NutritionService.AddNutrition(i.Nutrition) if err != nil { @@ -57,6 +58,8 @@ func (s itemService) AddItem(i *quartermaster.Item, l *quartermaster.Location, u if err != nil { return nil, err } + } else { + i.ID = ib.ID } queryString := "INSERT INTO x_items_locations (item_id, location_id, count) VALUES ($1, $2, 1) ON CONFLICT (item_id, location_id) DO UPDATE SET count = x_items_locations.count + 1" _, err = s.db.Exec(queryString, i.ID, l.ID)