public async Task <IActionResult> AddAddress(CustomerAddAddressInputModel customerAddAddress) { if (!this.ModelState.IsValid) { return(this.View(AutoMapper.Mapper.Map <CustomerAddAddressViewModel>(customerAddAddress))); } if (string.IsNullOrEmpty(customerAddAddress.DeliveryAddress)) { customerAddAddress.DeliveryAddress = customerAddAddress.PickUpAddress; } var hasAnyCustomerWithMaxSameData = await this.customersService.GetAllCustomersAsync <CustomerAddAddressViewModel>().Where(x => x.PhoneNumber == customerAddAddress.PhoneNumber).AnyAsync(x => HasMaxSameCustomerData(x, customerAddAddress.FirstName, customerAddAddress.LastName, customerAddAddress.PhoneNumber, customerAddAddress.PickUpAddress, customerAddAddress.DeliveryAddress)); if (hasAnyCustomerWithMaxSameData) { this.ModelState.AddModelError(string.Empty, CustomerConstants.ArgumentExceptionCustomerExist); return(this.View(AutoMapper.Mapper.Map <CustomerAddAddressViewModel>(customerAddAddress))); } var hasAnyCustomerWithMinSameData = await this.customersService.GetAllCustomersAsync <CustomerAddAddressViewModel>().Where(x => x.PhoneNumber == customerAddAddress.PhoneNumber).AnyAsync(x => !HasMinSameCustomerData(x, customerAddAddress.FirstName, customerAddAddress.LastName, customerAddAddress.PhoneNumber)); if (hasAnyCustomerWithMinSameData) { this.ModelState.AddModelError(string.Empty, string.Format(CustomerConstants.ArgumentExceptionCustomerPhone, customerAddAddress.PhoneNumber)); return(this.View(AutoMapper.Mapper.Map <CustomerAddAddressViewModel>(customerAddAddress))); } var result = await this.customersService.AddAddressToCustomerAsync(customerAddAddress); return(this.RedirectToAction(nameof(this.Index))); }
public async Task <CustomerAddAddressViewModel> AddAddressToCustomerAsync(CustomerAddAddressInputModel customerFromView) { var customerToDb = customerFromView.To <Customer>(); // Remove mapped Key to create new Customer customerToDb.Id = null; // if User with Id NOT EXIST set Null customerToDb.UserId = this.customerRepository.AllAsNoTracking().FirstOrDefault(x => x.Id == customerFromView.Id)?.UserId; await this.customerRepository.AddAsync(customerToDb); await this.customerRepository.SaveChangesAsync(); return(customerToDb.To <CustomerAddAddressViewModel>()); }