public async Task <SimpleResultPoco> SaveMaterials(MaterialPoco[] input) { await _context.AddRangeAsync(input); await _context.SaveChangesAsync(); return(new SimpleResultPoco { IsSuccess = true }); }
public async Task <SimpleResult> SaveMaterials(UploadInventoryViewModel input) { try { var warehouse = await _context.Warehouses.FindAsync(input.WarehousesId); var existingInventory = warehouse.Inventory; if (existingInventory != null) { _context.Inventories.RemoveRange(existingInventory); await _context.SaveChangesAsync(); } var newInventory = new List <InventoryPoco>(); foreach (var inv in input.Inventory) { var mat = _context.Materials.Where(x => x.StockCode == inv.StockCode).FirstOrDefault(); if (mat == null) { var material = new MaterialPoco { Cost = Convert.ToDecimal(inv.Cost, CultureInfo.InvariantCulture), StockCode = inv.StockCode, StockDescription = inv.StockDescription }; _context.Materials.Add(material); _context.SaveChanges(); var inventory = new InventoryPoco { DateAdded = DateTime.Now, DateModified = DateTime.Now, Deleted = false, MaterialId = material.Id, ModifiedBy = "", Quantity = Convert.ToDecimal(inv.Quantity, CultureInfo.InvariantCulture), WarehouseId = input.WarehousesId }; newInventory.Add(inventory); } else { var inventory = new InventoryPoco { DateAdded = DateTime.Now, DateModified = DateTime.Now, Deleted = false, MaterialId = mat.Id, ModifiedBy = "", Quantity = Convert.ToDecimal(inv.Quantity, CultureInfo.InvariantCulture), WarehouseId = input.WarehousesId }; newInventory.Add(inventory); } } await _context.AddRangeAsync(newInventory); await _context.SaveChangesAsync(); return(new SimpleResult { IsSuccess = true }); } catch { return(new SimpleResult { IsSuccess = false }); } }