Exemple #1
0
        public int AddElement(DeliveryBindingModel model)
        {
            Delivery element = context.Deliverys.FirstOrDefault(rec => rec.name == model.name);

            if (element != null)
            {
                throw new Exception("Уже есть доставка с таким названием");
            }
            element = new Delivery
            {
                Date = DateTime.Now,
                name = model.name
            };
            context.Deliverys.Add(element);
            try
            {
                context.SaveChanges();
                return(element.id);
            }
            catch (DbUpdateException e) {
                var sb = new StringBuilder();
                sb.AppendLine($"DbUpdateException error details - {e?.InnerException?.InnerException?.Message}");
                foreach (var eve in e.Entries)
                {
                    sb.AppendLine($"Entity of type {eve.Entity.GetType().Name} in state {eve.State} could not be updated");
                }
                Console.Write(sb.ToString());
                throw;
            }
        }
        public void Delete(DeliveryBindingModel model)
        {
            var element = _deliveryStorage.GetElement(new DeliveryBindingModel
            {
                Id = model.Id
            });

            if (element == null)
            {
                throw new Exception("Поставщик не найден");
            }
            _deliveryStorage.Delete(model);
        }
 public List <DeliveryViewModel> Read(DeliveryBindingModel model)
 {
     if (model == null)
     {
         return(_deliveryStorage.GetFullList());
     }
     if (model.Id.HasValue)
     {
         return(new List <DeliveryViewModel> {
             _deliveryStorage.GetElement(model)
         });
     }
     return(_deliveryStorage.GetFilteredList(model));
 }
Exemple #4
0
        public void UpdElement(DeliveryBindingModel model)
        {
            Delivery element = context.Deliverys.FirstOrDefault(rec => rec.name == model.name &&
                                                                rec.id != model.id);

            if (element != null)
            {
                throw new Exception("Уже есть доставка с таким id");
            }
            element = context.Deliverys.FirstOrDefault(rec => rec.id == model.id);
            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            element.Date = DateTime.Now;
            element.name = model.name;
            context.SaveChanges();
        }
        public void CreateOrUpdate(DeliveryBindingModel model)
        {
            var element = _deliveryStorage.GetElement(new DeliveryBindingModel
            {
                Date  = model.Date,
                Price = model.Price
            });

            if (element != null && element.Id != model.Id)
            {
                throw new Exception("Телефон или Email уже был зарегестрирован!");
            }
            if (model.Id.HasValue)
            {
                _deliveryStorage.Update(model);
            }
            else
            {
                _deliveryStorage.Insert(model);
            }
        }
 public async Task <IActionResult> MapDeliveryProduct([FromBody] DeliveryBindingModel model)
 {
     return(Ok(_mapper.Map <DeliveryBindingModel>(await _deliveryService.MapDeliveryProduct(_mapper.Map <Delivery>(model)))));
 }
 public async Task <IActionResult> Post([FromBody] DeliveryBindingModel model)
 {
     return(Ok(_mapper.Map <DeliveryBindingModel>(await _deliveryService.CreateDelivery(_mapper.Map <Delivery>(model)))));
 }