public void Update(SupplyBindingModel model) { using (var context = new MebelDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { var supply = context.Supplys.FirstOrDefault(rec => rec.Id == model.Id); if (supply == null) { throw new Exception("Поставка не найдена"); } CreateModel(model, supply, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
private Supply CreateModel(SupplyBindingModel model, Supply supply, MebelDatabase context) { supply.Date = model.Date; supply.Name = model.Name; supply.Price = model.Price; if (supply.Id == 0) { context.Supplys.Add(supply); context.SaveChanges(); } if (model.Id.HasValue) { var supplyMaterials = context.SupplyMaterials .Where(rec => rec.SupplyId == model.Id.Value) .ToList(); context.SupplyMaterials.RemoveRange(supplyMaterials.ToList()); context.SaveChanges(); } foreach (var supplyMaterial in model.SupplyMaterials) { context.SupplyMaterials.Add(new SupplyMaterial { SupplyId = supply.Id, MaterialId = supplyMaterial.Key, Count = supplyMaterial.Value.Item2 }); context.SaveChanges(); } return(supply); }
public void CreateOrUpdate(SupplyBindingModel model) { if (model.Id.HasValue) { _supplyStorage.Update(model); } else { _supplyStorage.Insert(model); } }
public void Delete(SupplyBindingModel model) { var element = _supplyStorage.GetElement(new SupplyBindingModel { Id = model.Id }); if (element == null) { throw new Exception("Поставка не найдена"); } _supplyStorage.Delete(model); }
public void Delete(SupplyBindingModel model) { var supply = _supplyStorage.GetElement(new SupplyBindingModel { Id = model.Id }); if (supply == null) { throw new Exception("Поступление не найдено"); } _supplyStorage.Delete(model); }
public List <SupplyViewModel> Read(SupplyBindingModel model) { if (model == null) { return(_supplyStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <SupplyViewModel> { _supplyStorage.GetElement(model) }); } return(_supplyStorage.GetFilteredList(model)); }
public List <SupplyViewModel> GetFilteredList(SupplyBindingModel model) { if (model == null) { return(null); } using (var context = new MebelDatabase()) { return(context.Supplys .Include(rec => rec.SupplyMaterials) .ThenInclude(rec => rec.Material) .Where(rec => rec.Date >= model.DateFrom && rec.Date <= model.DateTo) .Select(CreateModel).ToList()); } }
public SupplyViewModel GetElement(SupplyBindingModel model) { if (model == null) { return(null); } using (var context = new MebelDatabase()) { var supply = context.Supplys .Include(rec => rec.SupplyMaterials) .ThenInclude(rec => rec.Material) .FirstOrDefault(rec => rec.Id == model.Id); return(supply != null? CreateModel(supply) : null); } }
public void Delete(SupplyBindingModel model) { using (var context = new MebelDatabase()) { Supply element = context.Supplys.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Supplys.Remove(element); context.SaveChanges(); } else { throw new Exception("Поступление не найдено"); } } }
public void Insert(SupplyBindingModel model) { using (var context = new MebelDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { CreateModel(model, new Supply(), context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(SupplyBindingModel model) { var element = _supplyStorage.GetElement(new SupplyBindingModel { SupplyName = model.SupplyName, Date = model.Date }); if (element != null && element.Id != model.Id) { throw new Exception("Такая поставка уже есть!"); } if (model.Id.HasValue) { _supplyStorage.Update(model); } else { _supplyStorage.Insert(model); } }