func (cw *CustomerWatchLists) ListAddItem(listId string, id string, quantity float64) error { if quantity < 1 { return errors.New("Quantity must not be smaller than 1") } listExists := false itemExists := false for _, list := range cw.Lists { if list.Id == listId { listExists = true for _, item := range list.Items { if item.Id == id { itemExists = true // Item already exists in list => increase Quantity item.Quantity = item.Quantity + quantity } } if !itemExists { list.Items = append(list.Items, &WatchListItem{ Id: id, DateAdded: utils.GetDateYYYY_MM_DD(), Quantity: quantity, }) } } } if !listExists { return errors.New("List with Id " + listId + " not found") } return cw.Upsert() }
func (cw *CustomerWatchLists) AddList(watchlistType string, name string, public bool, recipient string, targetDate string, description string) (*WatchList, error) { watchList := &WatchList{ Id: unique.GetNewID(), Type: watchlistType, Name: name, CreatedAt: utils.GetDateYYYY_MM_DD(), Recipient: recipient, TargetDate: targetDate, Description: description, PublicURIHash: "", Items: []*WatchListItem{}, } if public { watchList.PublicURIHash = unique.GetNewID() } else { watchList.PublicURIHash = "" } cw.Lists = append(cw.Lists, watchList) err := cw.Upsert() if err != nil { return nil, err } return watchList, nil }
func TestWatchListsManipulate(t *testing.T) { test_utils.DropAllCollections() customerID := unique.GetNewID() _, err := NewCustomerWatchListsFromCustomerID(customerID) if err != nil { t.Fatal(err) } cw, err := GetCustomerWatchListsByCustomerID(customerID) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } // Create List listA, err := cw.AddList("TypeX", "ListA", true, "me", utils.GetDateYYYY_MM_DD(), "My awesome list") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } // Add item to list err = cw.ListAddItem(listA.Id, "item1", 2) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } // Increase Quantity of item err = cw.ListAddItem(listA.Id, "item1", 3) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } // Add another item err = cw.ListAddItem(listA.Id, "item2", 2) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } item, err := cw.GetItem(listA.Id, "item1") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if item.Quantity != 5 { utils.PrintJSON(cw) t.Fatal("Wrong Quantity, expected 5") } // Reduce Quantity of item by 1 err = cw.ListRemoveItem(listA.Id, "item2", 1) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } item, err = cw.GetItem(listA.Id, "item2") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if item.Quantity != 1 { t.Fatal("Wrong Quantity, expected 1") } // Remove last of item2 err = cw.ListRemoveItem(listA.Id, "item2", 1) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if len(listA.Items) != 1 { utils.PrintJSON(cw) t.Fatal("Expected 1 item in ListA") } newDescription := "new description" newName := "newName" // Edit list _, err = cw.EditList(listA.Id, newName, false, "", "", newDescription) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if listA.Name != newName || listA.Description != newDescription || listA.PublicURIHash != "" { utils.PrintJSON(cw) t.Fatal("EditList failed") } // Set item fulfilled cw.ListSetItemFulfilled(listA.Id, "item1", 2) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } item, err = cw.GetItem(listA.Id, "item1") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if item.QtyFulfilled != 2 { utils.PrintJSON(cw) t.Fatal("Expected QtyFulfilled == 2") } // Create second CustomerWatchLists and merge sessionID := unique.GetNewID() _, err = NewCustomerWatchListsFromSessionID(sessionID) if err != nil { t.Fatal(err) } cw2, err := GetCustomerWatchListsBySessionID(sessionID) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } listB, err := cw2.AddList("TypeX", "ListB", false, "recipient string", "targetDate string", "watchlist from session") if err != nil { t.Fatal(err) } err = cw2.ListAddItem(listB.Id, "item1b", 2) if err != nil { utils.PrintJSON(cw2) t.Fatal(err) } err = cw2.ListAddItem(listB.Id, "item1", 2) if err != nil { utils.PrintJSON(cw2) t.Fatal(err) } // Merge ListB from cw2 into ListA from cw err = MergeLists(cw2, listB.Id, cw, listA.Id) if err != nil { t.Fatal(err) } item, err = cw.GetItem(listA.Id, "item1") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if item.Quantity != 7 { utils.PrintJSON(cw) t.Fatal("Expected Quantity == 7") } item, err = cw2.GetItem(listB.Id, "item1b") if err != nil { utils.PrintJSON(cw) t.Fatal(err) } if item.Quantity != 2 { utils.PrintJSON(cw) t.Fatal("Expected Quantity == 2") } utils.PrintJSON(cw) // Test Getter cw, err = GetCustomerWatchListsByCustomerID(customerID) if err != nil { utils.PrintJSON(cw) t.Fatal(err) } // Test for non existant Id _, err = GetCustomerWatchListsByCustomerID("InvalidID") if err == nil { t.Fatal("Expected error not found", err) } watchList, err := cw2.EditList(listB.Id, "", true, "", "", "") if err != nil { t.Fatal("Edit List failed", err) } cw, err = GetCustomerWatchListsByURIHash(watchList.PublicURIHash) if err != nil { t.Fatal(err) } watchList, err = cw.GetListByURIHash(watchList.PublicURIHash) if err != nil { t.Fatal(err) } utils.PrintJSON(watchList) }