public async Task MapDataAndSave(ProductAddViewModel viewModel) { if (viewModel != null) { var product = new Product(); var supplier = await db.Suppliers.Include(x => x.Products).Where(x => x.Id == viewModel.SupplierId).FirstOrDefaultAsync(); var manufacturer = await db.Manufacturers.SingleOrDefaultAsync(x => x.Id == viewModel.ManufacturerId); var category = await db.Categories.Include(x => x.Products).SingleOrDefaultAsync(x => x.Id == viewModel.CategoryId); product.Name = viewModel.Name; product.Price = viewModel.Price; product.Supplier = supplier; product.Manufacturer = manufacturer; product.Category = category; product.Description = viewModel.Description; Create(product); await db.SaveChangesAsync(); JsonCRUD.AddToJson(product, JsonPath.PathToProducts); return; } throw new System.ArgumentException(); }
public async Task UpdateMapAndSave(ProductUpdateViewModel viewModel) { var currentProduct = await db.Products.Include(x => x.Manufacturer) .Include(x => x.Supplier).Include(x => x.Category) .SingleOrDefaultAsync(x => x.Id == viewModel.Id); currentProduct.CategoryId = viewModel.CategoryId; currentProduct.ManufacturerId = viewModel.ManufacturerId; currentProduct.SupplierId = viewModel.SupplierId; currentProduct.Price = viewModel.Price; currentProduct.Description = viewModel.Description; currentProduct.Name = viewModel.Name; Update(currentProduct); await SaveAsync(); JsonCRUD.UpdateJson(currentProduct, JsonPath.PathToProducts); }