public void Insert(MebelBindingModel model) { using (var context = new MebelDatabase()) { context.Mebel.Add(CreateModel(model, new Mebel())); context.SaveChanges(); } }
public void Update(MebelBindingModel model) { using (var context = new MebelDatabase()) { var element = context.Mebel.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Мебель не найдена"); } CreateModel(model, element); context.SaveChanges(); } }
public void Delete(MebelBindingModel model) { var mebel = _mebelStorage.GetElement(new MebelBindingModel { Id = model.Id }); if (mebel == null) { throw new Exception("Лечение не найдено"); } _mebelStorage.Delete(model); }
public List <MebelViewModel> Read(MebelBindingModel model) { if (model == null) { return(_mebelStorage.GetFullList()); } if (model.Id.HasValue) { return(new List <MebelViewModel> { _mebelStorage.GetElement(model) }); } return(_mebelStorage.GetFilteredList(model)); }
public void Delete(MebelBindingModel model) { using (var context = new MebelDatabase()) { Mebel element = context.Mebel.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Mebel.Remove(element); context.SaveChanges(); } else { throw new Exception("Мебель не найдена"); } } }
public List <MebelViewModel> GetFilteredList(MebelBindingModel model) { if (model == null) { return(null); } using (var context = new MebelDatabase()) { return(context.Mebel .Where(rec => rec.Name.Contains(model.Name)) .Select(rec => new MebelViewModel { Id = rec.Id, Name = rec.Name, Price = rec.Price }) .ToList()); } }
public MebelViewModel GetElement(MebelBindingModel model) { if (model == null) { return(null); } using (var context = new MebelDatabase()) { var component = context.Mebel .FirstOrDefault(rec => rec.Name == model.Name || rec.Id == model.Id); return(component != null ? new MebelViewModel { Id = component.Id, Name = component.Name, Price = component.Price } : null); } }
public void CreateOrUpdate(MebelBindingModel model) { var mebel = _mebelStorage.GetElement(new MebelBindingModel { Name = model.Name }); if (mebel != null && mebel.Id != model.Id) { throw new Exception("Уже есть такое лечение"); } if (model.Id.HasValue) { _mebelStorage.Update(model); } else { _mebelStorage.Insert(model); } }
private Mebel CreateModel(MebelBindingModel model, Mebel mebel) { mebel.Name = model.Name; mebel.Price = model.Price; return(mebel); }