public async Task <IActionResult> UpdateContact(int id, [FromBody] UpdateContactDto updateContact) { try { if (updateContact == null) { return(BadRequest("Contact object is null")); } if (!ModelState.IsValid) { return(BadRequest("Invalid model object")); } var contactEntity = await _unitofwork.ContactRepository.GetContactByIdAsync(id); if (contactEntity == null) { return(NotFound()); } _mapper.Map(updateContact, contactEntity); _unitofwork.ContactRepository.UpdateContact(contactEntity); await _unitofwork.SaveAsync(); return(NoContent()); } catch (Exception ex) { return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> PutAsync(Int64 id, UpdateContactDto updateContactDto) { var existingContact = await contactsRepository.Get <Contact>(x => x.Id == id); if (existingContact == null) { return(NotFound()); } //existingContact.First = updateContactDto.first; //existingContact.Middle = updateContactDto.middle; //existingContact.Last = updateContactDto.last; //existingContact.Addresses = updateContactDto.address; //if (!existingContact.Phones.Any(x => x.Number == updateContactDto.phone.Number)) //{ // existingContact.Phones.Add(updateContactDto.phone); //} //else //{ // var updateNumber = existingContact.Phones.FirstOrDefault(x => x.Number == updateContactDto.phone.Number); // if (updateNumber != null) updateNumber.Type = updateContactDto.phone.Type; //} //await contactsRepository.UpdateAsync(existingContact); return(NoContent()); }
public async Task UpdateAsync(int contactId, UpdateContactDto dto) { var contact = await _contactRepository.FindByIdAsync(contactId); if (contact == null) { throw new ResourceNotFoundException(); } var isExistByNameOrAddress = await _contactRepository.IsExistByNameOrAddressAndContactIdAsync(dto.Name, dto.Address, contactId); if (isExistByNameOrAddress) { throw new BusinessException("Contact already exist with name or address"); } MapToInstance(dto, contact); foreach (var number in dto.TelephoneNumbers) { var exsisintNumber = contact.TelephoneNumbers.FirstOrDefault(tn => tn.Id == number.Id); exsisintNumber.Number = number.Number; } await _contactRepository.UpdateAsync(contact); }
public async Task <IActionResult> PatchContact(long id, [FromBody] UpdateContactDto updatedContact) { if (ModelState.IsValid) { return(buildResponse(await _service.UpdateContact(id, updatedContact))); } return(null); }
public async Task <IActionResult> UpdateCity(int contactId, [FromBody] UpdateContactDto request) { if (!ModelState.IsValid) { return(BadRequest(ModelState.GetErrorMessages())); } await _contactService.UpdateAsync(contactId, request); return(ApiResponseOk(null)); }
public async Task <IActionResult> UpdateContact([FromRoute] long id, [FromBody] UpdateContactDto updateContactDto, CancellationToken ct) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != updateContactDto.Id) { return(BadRequest()); } await _contactsService.UpdateContact(updateContactDto, ct); return(Ok()); }
public async Task UpdateContact(UpdateContactDto updateContactDto, CancellationToken ct) { var contact = await GetContact(updateContactDto.Id, ct); _mapper.Map(updateContactDto, contact); try { _dbContext.Update(contact); await _dbContext.SaveChangesAsync(ct); } catch (Exception ex) { _logger.LogError($"{DateTime.Now:g}: {ex.Message}"); throw; } }
private void updateContactMap(ref Mo.Contact contact, UpdateContactDto updatedContact) { contact.CityID = updatedContact.CityID; contact.CompanyID = updatedContact.CompanyID; contact.ProfileImage = updatedContact.ProfileImage; contact.Name = H.UppercaseFirst(updatedContact.Name); contact.Email = updatedContact.Email.ToLower(); contact.StreetName = H.UppercaseFirst(updatedContact.StreetName); contact.StreetNumber = updatedContact.StreetNumber; contact.BirthDate = updatedContact.BirthDate; var phoneIds = new HashSet <long>(); foreach (PhoneDto p in updatedContact.Phones) { // Add only new phones if (p.ID == 0) { contact.Phones.Add(_mapper.Map <Mo.Phone>(p)); } // keep old valid phones if (p.ID > 0) { phoneIds.Add(p.ID); } } // Soft Delete older phones foreach (Mo.Phone p in contact.Phones) { if (p.ID > 0 && !phoneIds.Contains(p.ID)) { p.Active = false; p.DeletedAt = DateTime.Now; } else { p.Active = true; } } contact.UpdatedAt = DateTime.Now; }
public async Task <ClientResponse <ContactDto> > UpdateContact(long ID, UpdateContactDto updateDto) { var response = new ClientResponse <ContactDto>(); try { if (updateDto.BirthDate > DateTime.Now) { response.Message = E.ErrHigherBirthDate; response.Status = System.Net.HttpStatusCode.BadRequest; return(response); } var contact = await _repository.Contacts.FirstOrDefaultAsync(x => x.ID == ID && x.Active); if (contact == null) { response.Message = E.ErrContactNotFound; response.Status = System.Net.HttpStatusCode.NoContent; return(response); } if (await validateCityAndCompany(contact.CityID, contact.CompanyID)) { var phones = await _repository.Phones.Where(x => x.ContactID == contact.ID).ToListAsync(); contact.Phones = phones; updateContactMap(ref contact, updateDto); await _repository.SaveChangesAsync(); return(await GetContactById(contact.ID)); } response.Message = E.ErrGeneric; response.Status = System.Net.HttpStatusCode.InternalServerError; return(response); } catch (Exception ex) { response.Message = ex.Message; response.Status = System.Net.HttpStatusCode.InternalServerError; return(response); } }