public void CreateOrUpdate(KomlectConcreteBindingModel model) { Komlect tempKomlect = model.Id.HasValue ? null : new Komlect { Id = 1 }; foreach (var Komlect in source.Komlects) { if (Komlect.KomlectName == model.KomlectName && Komlect.Id != model.Id) { throw new Exception("Уже есть изделие с таким названием"); } if (!model.Id.HasValue && Komlect.Id >= tempKomlect.Id) { tempKomlect.Id = Komlect.Id + 1; } else if (model.Id.HasValue && Komlect.Id == model.Id) { tempKomlect = Komlect; } } if (model.Id.HasValue) { if (tempKomlect == null) { throw new Exception("Элемент не найден"); } CreateModel(model, tempKomlect); } else { source.Komlects.Add(CreateModel(model, tempKomlect)); } }
public void Delete(KomlectConcreteBindingModel model) { using (var context = new SecureShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { // удаяем записи по компонентам при удалении изделия context.KomlectComponents.RemoveRange(context.KomlectComponents.Where(rec => rec.KomlectId == model.Id)); Komlect element = context.Komlects.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Komlects.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
private KomlectViewModel CreateViewModel(Komlect Komlect) { // требуется дополнительно получить список компонентов для изделия с // названиями и их количество Dictionary <int, (string, int)> KomlectComponents = new Dictionary <int, (string, int)>(); foreach (var pc in source.KomlectComponents) { if (pc.KomlectId == Komlect.Id) { string componentName = string.Empty; foreach (var component in source.Components) { if (pc.ComponentId == component.Id) { componentName = component.ComponentName; break; } } KomlectComponents.Add(pc.ComponentId, (componentName, pc.Count)); } } return(new KomlectViewModel { Id = Komlect.Id, KomlectName = Komlect.KomlectName, Price = Komlect.Price, KomlectComponents = KomlectComponents }); }
public void CreateOrUpdate(KomlectConcreteBindingModel model) { Komlect element = source.Komlects.FirstOrDefault(rec => rec.KomlectName == model.KomlectName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть изделие с таким названием"); } if (model.Id.HasValue) { element = source.Komlects.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { int maxId = source.Komlects.Count > 0 ? source.Components.Max(rec => rec.Id) : 0; element = new Komlect { Id = maxId + 1 }; source.Komlects.Add(element); } element.KomlectName = model.KomlectName; element.Price = model.Price; // удалили те, которых нет в модели source.KomlectComponents.RemoveAll(rec => rec.KomlectId == model.Id && !model.KomlectComponents.ContainsKey(rec.ComponentId)); // обновили количество у существующих записей var updateComponents = source.KomlectComponents.Where(rec => rec.KomlectId == model.Id && model.KomlectComponents.ContainsKey(rec.ComponentId)); foreach (var updateComponent in updateComponents) { updateComponent.Count = model.KomlectComponents[updateComponent.ComponentId].Item2; model.KomlectComponents.Remove(updateComponent.ComponentId); } // добавили новые int maxPCId = source.KomlectComponents.Count > 0 ? source.KomlectComponents.Max(rec => rec.Id) : 0; foreach (var pc in model.KomlectComponents) { source.KomlectComponents.Add(new KomlectComponent { Id = ++maxPCId, KomlectId = element.Id, ComponentId = pc.Key, Count = pc.Value.Item2 }); } }
public void Delete(KomlectConcreteBindingModel model) { // удаяем записи по компонентам при удалении изделия source.KomlectComponents.RemoveAll(rec => rec.KomlectId == model.Id); Komlect element = source.Komlects.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { source.Komlects.Remove(element); } else { throw new Exception("Элемент не найден"); } }
private Komlect CreateModel(KomlectConcreteBindingModel model, Komlect Komlect) { Komlect.KomlectName = model.KomlectName; Komlect.Price = model.Price; //обновляем существуюущие компоненты и ищем максимальный идентификатор int maxPCId = 0; for (int i = 0; i < source.KomlectComponents.Count; ++i) { if (source.KomlectComponents[i].Id > maxPCId) { maxPCId = source.KomlectComponents[i].Id; } if (source.KomlectComponents[i].KomlectId == Komlect.Id) { // если в модели пришла запись компонента с таким id if (model.KomlectComponents.ContainsKey(source.KomlectComponents[i].ComponentId)) { // обновляем количество source.KomlectComponents[i].Count = model.KomlectComponents[source.KomlectComponents[i].ComponentId].Item2; // из модели убираем эту запись, чтобы остались только не // просмотренные model.KomlectComponents.Remove(source.KomlectComponents[i].ComponentId); } else { source.KomlectComponents.RemoveAt(i--); } } } // новые записи foreach (var pc in model.KomlectComponents) { source.KomlectComponents.Add(new KomlectComponent { Id = ++maxPCId, KomlectId = Komlect.Id, ComponentId = pc.Key, Count = pc.Value.Item2 }); } return(Komlect); }
public void CreateOrUpdate(KomlectConcreteBindingModel model) { using (var context = new SecureShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Komlect element = context.Komlects.FirstOrDefault(rec => rec.KomlectName == model.KomlectName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть изделие с таким названием"); } if (model.Id.HasValue) { element = context.Komlects.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Komlect(); context.Komlects.Add(element); } element.KomlectName = model.KomlectName; element.Price = model.Price; context.SaveChanges(); if (model.Id.HasValue) { var KomlectComponents = context.KomlectComponents.Where(rec => rec.KomlectId == model.Id.Value).ToList(); // удалили те, которых нет в модели context.KomlectComponents.RemoveRange(KomlectComponents.Where(rec => !model.KomlectComponents.ContainsKey(rec.ComponentId)).ToList()); context.SaveChanges(); // обновили количество у существующих записей foreach (var updateComponent in KomlectComponents) { updateComponent.Count = model.KomlectComponents[updateComponent.ComponentId].Item2; model.KomlectComponents.Remove(updateComponent.ComponentId); } context.SaveChanges(); } // добавили новые foreach (var pc in model.KomlectComponents) { context.KomlectComponents.Add(new KomlectComponent { KomlectId = element.Id, ComponentId = pc.Key, Count = pc.Value.Item2 }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }