public async Task <IActionResult> Put(int id, [FromBody] Entities.ShoppingList value) { if (value == null || value.Id != id) { return(BadRequest()); } var list = await db.ShoppingLists.Include(l => l.Items).SingleOrDefaultAsync(i => i.Id == id); if (list == null) { return(NotFound()); } list.Name = value.Name; list.Items = value.Items; try { db.ShoppingLists.Update(list); db.SaveChanges(); } catch (Exception e) { } await notifications.NotifyRefreshList(); return(NoContent()); }
private async Task StoreModifiedListAsync(Entities.ShoppingList existingShoppingListEntity, IShoppingList shoppingList, CancellationToken cancellationToken) { var shoppingListEntityToStore = toEntityConverter.ToEntity(shoppingList); var onListMappings = existingShoppingListEntity.ItemsOnList.ToDictionary(map => map.ItemId); dbContext.Entry(shoppingListEntityToStore).State = EntityState.Modified; foreach (var map in shoppingListEntityToStore.ItemsOnList) { cancellationToken.ThrowIfCancellationRequested(); if (onListMappings.TryGetValue(map.ItemId, out var existingMapping)) { // mapping was modified map.Id = existingMapping.Id; dbContext.Entry(map).State = EntityState.Modified; onListMappings.Remove(map.ItemId); } else { // mapping was added dbContext.Entry(map).State = EntityState.Added; } } // mapping was deleted foreach (var map in onListMappings.Values) { dbContext.Entry(map).State = EntityState.Deleted; } cancellationToken.ThrowIfCancellationRequested(); await dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Post([FromBody] Entities.ShoppingList value) { if (value == null) { return(BadRequest()); } await db.ShoppingLists.AddAsync(value); await db.SaveChangesAsync(); await notifications.NotifyRefreshList(); return(CreatedAtRoute("GetShoppingList", new { id = value.Id }, value)); }
public async Task NotifyShoppingListUpdated(Entities.ShoppingList item) { var group = hub.Clients.Group($"{ShoppingListHub.SHOPPINGLIST_GROUP}{item.Id}"); await group.SendAsync("ShoppingList_Updated", item); }