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 <ShoppingListItemModel> Post([FromBody] ShoppingListItemModel value) { var shoppingList = await _context.ShoppingList.SingleOrDefaultAsync(m => m.Id == value.Id); if (shoppingList != null) { shoppingList.Item = value.Item; shoppingList.Quantity = value.Quantity; shoppingList.CategoryId = value.Category.Id; _context.Update(shoppingList); await _context.SaveChangesAsync(); } else { shoppingList = new ShoppingListItem() { Item = value.Item, Quantity = value.Quantity, CategoryId = value.Category.Id, WeekId = 1 }; _context.Add(shoppingList); await _context.SaveChangesAsync(); } var category = await _context.Category.SingleAsync(u => u.Id == shoppingList.CategoryId); return(Map(shoppingList, category)); }
public async Task <IActionResult> PutShoppingListItem(int id, ShoppingListItem shoppingListItem) { if (id != shoppingListItem.ItemID) { return(BadRequest()); } _context.Entry(shoppingListItem).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShoppingListItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutUserProfile(int id, UserProfile userProfile) { if (id != userProfile.UserID) { return(BadRequest()); } _context.Entry(userProfile).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserProfileExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutShoppingList([FromRoute] int id, [FromBody] ShoppingList shoppingList) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != shoppingList.Id) { return(BadRequest()); } _context.Entry(shoppingList).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShoppingListExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Food> InsertFood(Food newFood) { _context.Foods.Add(newFood); await _context.SaveChangesAsync(); return(newFood); }
public async Task <int> AddAsync(ShoppingList entity) { await _context.ShoppingLists.AddAsync(entity); await _context.SaveChangesAsync(); return(entity.ShoppingListId); }
public async Task <BuyList> InsertBuyListAsync(BuyList newBuyList) { _context.BuyList.Add(newBuyList); await _context.SaveChangesAsync(); return(newBuyList); }
public async Task <FoodCounter> InsertFoodCounterAsync(FoodCounter newFood) { _context.FoodCounters.Add(newFood); await _context.SaveChangesAsync(); return(newFood); }
public async Task <IActionResult> Create([Bind("Id,Name,Quantity,Price,AmountProduct")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("Id,ListName,CreatingDate,Price,UserId")] ShopList shopList) { if (ModelState.IsValid) { _context.Add(shopList); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(shopList)); }
public async Task <IActionResult> Create([Bind("iD,name")] Items items) { if (ModelState.IsValid) { _context.Add(items); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(items)); }
public async Task <IActionResult> Create([Bind("Id,Name,Price,ShelfId,Warranty")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["ShelfId"] = new SelectList(_context.Shelfs, "Id", "Id", product.ShelfId); return(View(product)); }
public async void Should_ReturnOneShoppingList() { var options = ReturnDbContextOptions("Should_ReturnOneShoppingList"); // Arrange using (var context = new ShoppingListContext(options)) { await context.ShoppingList.AddAsync(new ShoppingList { Name = "Test", BudgetSum = 20 }); await context.SaveChangesAsync(); } using (var context = new ShoppingListContext(options)) { var sut = new ShoppingListController(new Repository(context)); // Act var result = await sut.GetAllShoppingListsAsync(); // Assert Assert.Single(result.Value); } }
public async void Should_ReturnMoreThanOneShoppingList() { var options = ReturnDbContextOptions("Should_ReturnMoreThanOneShoppingList"); // Arrange using (var context = new ShoppingListContext(options)) { await context.ShoppingList.AddRangeAsync( new ShoppingList { Name = "Test1", BudgetSum = 10 }, new ShoppingList { Name = "Test2", BudgetSum = 15 }); await context.SaveChangesAsync(); } using (var context = new ShoppingListContext(options)) { var sut = new ShoppingListController(new Repository(context)); // Act var result = await sut.GetAllShoppingListsAsync(); // Assert Assert.True(result.Value.Count > 1); Assert.Equal(2, result.Value.Count); } }
public async Task <FoodMessageRating> InsertRatingMessageSingleAsync(FoodMessageRating newFoodMessageRating) { _context.FoodMessageRating.Add(newFoodMessageRating); await _context.SaveChangesAsync(); return(newFoodMessageRating); }
public async Task <User> CreateUserAsync(User user) { user.CreatedOn = DateTime.UtcNow; await _context.Users.AddAsync(user); await _context.SaveChangesAsync(); return(user); }
public async Task <Group> CreateGroupAsync(string name, User owner) { var groupToCreate = new Group { Id = Guid.NewGuid().ToString(), Name = name, CreatedOn = DateTime.UtcNow, Users = new List <User> { owner } }; await _context.Groups.AddAsync(groupToCreate); await _context.SaveChangesAsync(); return(groupToCreate); }
public async Task <IActionResult> Products(int quantity, int orderProduct, int?orderId) { if (!orderId.HasValue) { var orders = await this._context.Orders.MaxAsync(o => o.OrderId); orderId = orders + 1; } var orderTableId = await this._context.Orders.MaxAsync(o => o.Id); await _context.Orders.AddAsync(new Orders() { OrderId = orderId, ProductId = orderProduct, Quantity = quantity, Id = orderTableId + 1 }); await _context.SaveChangesAsync(); var orderItem = await GetOrderItem(); orderItem.Orderid = orderId; return(View(orderItem)); }