private static POSContext AddItem(POSContext context, POSItems item) { if (!context.Items.Any(e => e.Id == item.Id)) { context.Add(item); context.SaveChanges(); } return(context); }
public static void CreateProductData(this POSContext context, int total = 1) { for (int i = 0; i < total; i++) { context.Add(new Product() { Name = $"Prod{(i + 1)}" }); } context.SaveChanges(); }
public void TestUpsertProductModifiers() { TestHandler.Handle((DbContextOptions <POSContext> options) => { var productId = 1; using (var context = new POSContext(options)) { context.CreateProductData(); context.CreateModifierData(total: 2, totalItemModifier: 2); context.SaveChanges(); context.Add(new ProductModifier { ModifierId = 1, ProductId = 1 }); context.SaveChanges(); var dto = new List <ProductModifier>() { new ProductModifier { ProductId = productId, ModifierId = 1 }, new ProductModifier { ProductId = productId, ModifierId = 2 }, }; //assert add one more var productService = new ProductService(context); var product = productService.GetProduct(productId).Result; productService.UpsertDeleteProductModifiers(product, dto); context.SaveChanges(); Assert.AreEqual(2, context.Product.FirstOrDefault().ProductModifier.Count()); } using (var context = new POSContext(options)) { //asssert remove all var dto = new List <ProductModifier>() { }; var productService = new ProductService(context); var product2 = productService.GetProduct(productId).Result; productService.UpsertDeleteProductModifiers(product2, dto); context.SaveChanges(); Assert.AreEqual(0, context.Product.FirstOrDefault().ProductModifier.Count()); } }); }
public async Task <ActionResult> Pay([FromBody] OrderForPayDTO vm) { //save to order, orderDetail, paymentOrder var order = _mapper.Map <Order>(vm); //check that products are for rent if (TryValidateModel(order)) { _context.Add(order); _context.SaveChanges(); return(Ok()); } return(BadRequest(ModelState)); }
/// <summary> /// Add or update modifier and Item modifier /// </summary> /// <param name="modClient"></param> public void UpsertDeleteModiferAndItemModifier(Modifier modClient) { //load the modifier var modDb = _context.Modifier .Include(_ => _.ItemModifier) .FirstOrDefault(_ => _.Id == modClient.Id); if (modDb == null) { _context.Add(modClient); } else { //set values _context.Entry(modDb).CurrentValues.SetValues(modClient); //check what are in the db and update it foreach (var item in modClient.ItemModifier) { var itemDb = modDb.ItemModifier.FirstOrDefault(_ => _.Id == item.Id); if (itemDb == null) { item.ModifierId = modClient.Id; _context.ItemModifier.Add(item); } else { _context.Entry <ItemModifier>(itemDb).CurrentValues.SetValues(item); } } foreach (var item in modDb.ItemModifier) { if (!modClient.ItemModifier.Any(_ => _.Id == item.Id)) { _context.ItemModifier.Remove(item); } } } }
/// <summary> /// Assing a modifiers to a product /// </summary> /// <param name="productId"></param> /// <param name="productModifierClient"></param> /// <returns></returns> public void UpsertDeleteProductModifiers(Product product, List <ProductModifier> productModifierClient) { var productId = product.Id; var productModifierDb = product.ProductModifier; var noRecordsInDatabase = productModifierDb.Count() == 0; if (noRecordsInDatabase) { foreach (var item in productModifierClient) { item.ProductId = productId; productModifierDb.Add(item); } } else { foreach (var item in productModifierClient) { var noRecordMatchInDatabase = !productModifierDb.Any(_ => _.ProductId == productId && _.ModifierId == item.ModifierId); if (noRecordMatchInDatabase) { item.ProductId = productId; _context.Add(item); } } foreach (var item in productModifierDb) { var noRecordMatchInClient = !productModifierClient.Any(_ => _.ProductId == productId && _.ModifierId == item.ModifierId); if (noRecordMatchInClient) { _context.Remove(item); } } } }