public void AddItemToList(string listName, long itemId, int quantity) { // Add a ListItem entry for the specified ID to the specified list using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { // If there is already an item with the same identity in the list then simply update its quantity ListItem itemToUpdate = targetList.ListItems.Where(item => item.ItemId == itemId).FirstOrDefault(); if (itemToUpdate != null) { itemToUpdate.Quantity += quantity; } else { context.ListItems.Add(new ListItem { ItemId = itemId, ListId = targetList.Id, Quantity = quantity }); } context.SaveChanges(); } } }
public void DeleteListItem(string listName, long itemId, int quantity) { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { List targetList = context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .FirstOrDefault(); if (targetList != null) { ListItem itemToDelete = targetList.ListItems.Where(item => item.Id == itemId).FirstOrDefault(); if (itemToDelete != null) { if (itemToDelete.Quantity <= quantity) { context.ListItems.Remove(itemToDelete); } else { itemToDelete.Quantity -= quantity; } context.SaveChanges(); } } } }
public List <Group> GetGroups() { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { return(context.Groups.ToList()); } }
public List GetList(string listName) { using (ShoppingListContext context = ShoppingListContextFactory.Create()) { return(context.Lists.Where(list => list.Name == listName) .Include(list => list.ListItems) .ThenInclude(itemInList => itemInList.Item) .ThenInclude(item => item.Group) .FirstOrDefault()); } }
public List <Item> GetItems(bool ordered) { List <Item> returnedList = new List <Item>(); using (ShoppingListContext context = ShoppingListContextFactory.Create()) { foreach (Group itemGroup in context.Groups.Include(group => group.Items).OrderBy(group => group.Name)) { foreach (Item iterItem in itemGroup.Items) { returnedList.Add(iterItem); } } } return((ordered == true) ? returnedList.OrderBy(item => item.Name).ToList() : returnedList); }
public List <object> GetGroupsAndItems() { List <object> returnedList = new List <object>(); using (ShoppingListContext context = ShoppingListContextFactory.Create()) { foreach (Group iteeGroup in context.Groups.Include(group => group.Items).OrderBy(group => group.Name)) { returnedList.Add(iteeGroup); foreach (Item iterItem in iteeGroup.Items) { returnedList.Add(iterItem); } } } return(returnedList); }