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)