public async Task <int> UpdateAsync(EditCustomerInputModel input) { var customer = await customersRepository.GetByIdWithDeletedAsync(input.Id); var customerEmails = this.emailsService.GetAll <EmailCreateInputModel>(input.Id); var customerPhones = this.phonesServices.GetAll <PhoneCreateInputModel>(input.Id); foreach (var email in input.Emails) { if (email.Id != null && (email.Email != customerEmails.FirstOrDefault(x => x.Id == email.Id)?.Email || email.EmailType != customerEmails.FirstOrDefault(x => x.Id == email.Id)?.EmailType)) { await this.emailsService.UpdateAsync(email); } else if (email.Email != null && email.Id == null) { await this.emailsService.CreateAsync(email.Email, email.EmailType, customer.Id); } } foreach (var phone in input.Phones) { if (phone.Id != null && (phone.Phone != customerPhones.FirstOrDefault(x => x.Id == phone.Id)?.Phone || phone.PhoneType != customerPhones.FirstOrDefault(x => x.Id == phone.Id)?.PhoneType)) { await this.phonesServices.UpdateAsync(phone); } else if (phone.Phone != null && phone.Id == null) { await this.phonesServices.CreateAsync(phone.Phone, phone.PhoneType, customer.Id); } } await this.addressesService.UpdateAsync(input.Address); customer.Title = input.Title; customer.FirstName = input.FirstName; customer.MiddleName = input.MiddleName; customer.LastName = input.LastName; customer.AdditionalInfo = input.AdditionalInfo; customer.JobTitle = input.JobTitle; this.customersRepository.Update(customer); return(await this.customersRepository.SaveChangesAsync()); }
public async Task <IActionResult> Edit(EditCustomerInputModel input) { var user = await this.userManager.GetUserAsync(this.User); if (user.OrganizationId == null) { return(this.RedirectToAction("Create", "Organizations")); } foreach (var email in input.Emails) { if (!this.validationService.IsAvailableEmail(user.Id, email.Email, email.Id, email.CustomerId)) { ModelState.AddModelError("", $"This {email.Email} is already taken by other customer in your list"); } } foreach (var phone in input.Phones) { if (!this.validationService.IsAvailablePhone(user.Id, phone.Phone, phone.Id, phone.CustomerId)) { ModelState.AddModelError("", $"This {phone.Phone} is already taken by other customer in your list"); } } if (!this.ModelState.IsValid) { return(this.View(input)); } await this.customersService.UpdateAsync(input); return(this.RedirectToAction("Details", new { id = input.Id })); }