コード例 #1
0
        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;
                    }
                }
            }
        }
コード例 #2
0
        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);
        }
コード例 #3
0
 public void CreateOrUpdate(SupplyBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _supplyStorage.Update(model);
     }
     else
     {
         _supplyStorage.Insert(model);
     }
 }
コード例 #4
0
        public void Delete(SupplyBindingModel model)
        {
            var element = _supplyStorage.GetElement(new SupplyBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Поставка не найдена");
            }
            _supplyStorage.Delete(model);
        }
コード例 #5
0
        public void Delete(SupplyBindingModel model)
        {
            var supply = _supplyStorage.GetElement(new SupplyBindingModel
            {
                Id = model.Id
            });

            if (supply == null)
            {
                throw new Exception("Поступление не найдено");
            }
            _supplyStorage.Delete(model);
        }
コード例 #6
0
 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));
 }
コード例 #7
0
 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());
     }
 }
コード例 #8
0
 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);
     }
 }
コード例 #9
0
 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("Поступление не найдено");
         }
     }
 }
コード例 #10
0
 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;
             }
         }
     }
 }
コード例 #11
0
        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);
            }
        }