private Shipment CreateModel(ShipmentBindingModel model, Shipment shipment, MebelDatabase context)
        {
            shipment.Date  = model.Date;
            shipment.Name  = model.Name;
            shipment.Price = model.Price;

            if (shipment.Id == 0)
            {
                context.Shipments.Add(shipment);
                context.SaveChanges();
            }

            if (model.Id.HasValue)
            {
                var shipmentGarnitures = context.ShipmentGarnitures
                                         .Where(rec => rec.ShipmentId == model.Id.Value)
                                         .ToList();

                context.ShipmentGarnitures.RemoveRange(shipmentGarnitures.ToList());

                context.SaveChanges();
            }

            foreach (var shipmentGarniture in model.ShipmentGarnitures)
            {
                context.ShipmentGarnitures.Add(new ShipmentGarniture
                {
                    ShipmentId  = shipment.Id,
                    GarnitureId = shipmentGarniture.Key,
                    Count       = shipmentGarniture.Value.Item2
                });
                context.SaveChanges();
            }
            return(shipment);
        }
        public void Update(ShipmentBindingModel model)
        {
            using (var context = new MebelDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var shipment = context.Shipments.FirstOrDefault(rec => rec.Id == model.Id);

                        if (shipment == null)
                        {
                            throw new Exception("Отгрузка не найдена");
                        }

                        CreateModel(model, shipment, context);
                        context.SaveChanges();

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Esempio n. 3
0
 public void CreateOrUpdate(ShipmentBindingModel model)
 {
     if (model.Id.HasValue)
     {
         _supplyStorage.Update(model);
     }
     else
     {
         _supplyStorage.Insert(model);
     }
 }
Esempio n. 4
0
        public void Delete(ShipmentBindingModel model)
        {
            var shipment = _supplyStorage.GetElement(new ShipmentBindingModel
            {
                Id = model.Id
            });

            if (shipment == null)
            {
                throw new Exception("Поступление не найдено");
            }
            _supplyStorage.Delete(model);
        }
Esempio n. 5
0
 public List <ShipmentViewModel> Read(ShipmentBindingModel model)
 {
     if (model == null)
     {
         return(_supplyStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <ShipmentViewModel> {
             _supplyStorage.GetElement(model)
         });
     }
     return(_supplyStorage.GetFilteredList(model));
 }
 public List <ShipmentViewModel> GetFilteredList(ShipmentBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new MebelDatabase())
     {
         return(context.Shipments
                .Include(rec => rec.ShipmentGarnitures)
                .ThenInclude(rec => rec.Garniture)
                .Where(rec => rec.Date >= model.DateFrom && rec.Date <= model.DateTo)
                .Select(CreateModel).ToList());
     }
 }
 public ShipmentViewModel GetElement(ShipmentBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new MebelDatabase())
     {
         var shipment = context.Shipments
                        .Include(rec => rec.ShipmentGarnitures)
                        .ThenInclude(rec => rec.Garniture)
                        .FirstOrDefault(rec => rec.Id == model.Id);
         return(shipment != null?
                CreateModel(shipment) : null);
     }
 }
 public void Delete(ShipmentBindingModel model)
 {
     using (var context = new MebelDatabase())
     {
         Shipment element = context.Shipments.FirstOrDefault(rec => rec.Id == model.Id);
         if (element != null)
         {
             context.Shipments.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Отгрузка не найдено");
         }
     }
 }
 public void Insert(ShipmentBindingModel model)
 {
     using (var context = new MebelDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 CreateModel(model, new Shipment(), context);
                 context.SaveChanges();
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }