Example #1
0
        public async Task <IActionResult> Edit(string id, CustomerEditInputModel customerEdit)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(this.View(AutoMapper.Mapper.Map <CustomerEditViewModel>(customerEdit))));
            }

            if (string.IsNullOrEmpty(customerEdit.DeliveryAddress))
            {
                customerEdit.DeliveryAddress = customerEdit.PickUpAddress;
            }

            var hasAnyCustomerWithSameData = await this.customersService
                                             .GetAllCustomersAsync <CustomerEditViewModel>()
                                             .AnyAsync(x => x != null && x.FirstName == customerEdit.FirstName && x.LastName == customerEdit.LastName && x.PhoneNumber == customerEdit.PhoneNumber && x.PickUpAddress == customerEdit.PickUpAddress && ((x.DeliveryAddress == null && customerEdit.DeliveryAddress == null) || x.DeliveryAddress == customerEdit.DeliveryAddress));

            if (hasAnyCustomerWithSameData)
            {
                this.ModelState.AddModelError(string.Empty, CustomerConstants.ArgumentExceptionCustomerExist);

                return(this.View(AutoMapper.Mapper.Map <CustomerEditViewModel>(customerEdit)));
            }

            var result = await this.customersService.EditByIdAsync(id, customerEdit);

            return(this.RedirectToAction(nameof(this.Index)));
        }
Example #2
0
 public async Task <IActionResult> Edit(int id, CustomerEditInputModel customerEdit)
 {
     try
     {
         // TODO: Add update logic here
         return(this.RedirectToAction(nameof(this.Index)));
     }
     catch
     {
         return(this.View());
     }
 }
Example #3
0
        public async Task <CustomerEditViewModel> EditByIdAsync(string id, CustomerEditInputModel customerFromView)
        {
            var customerToUpdate = await this.customerRepository
                                   .All()
                                   .FirstOrDefaultAsync(x => x.Id == customerFromView.Id);

            if (customerToUpdate == null)
            {
                throw new NullReferenceException(string.Format(CustomerConstants.NullReferenceCustomerId, id));
            }

            customerToUpdate.FirstName       = customerFromView.FirstName;
            customerToUpdate.LastName        = customerFromView.LastName;
            customerToUpdate.PhoneNumber     = customerFromView.PhoneNumber;
            customerToUpdate.PickUpAddress   = customerFromView.PickUpAddress;
            customerToUpdate.DeliveryAddress = customerFromView.DeliveryAddress;

            this.customerRepository.Update(customerToUpdate);
            await this.customerRepository.SaveChangesAsync();

            return(customerToUpdate.To <CustomerEditViewModel>());
        }
Example #4
0
 public async Task <CustomerEditViewModel> EditByIdAsync(int id, CustomerEditInputModel customerFromView)
 {
     throw new NotImplementedException();
 }