Ejemplo n.º 1
0
        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"));
            }
        }
Ejemplo n.º 2
0
        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());
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 public async Task <IActionResult> PatchContact(long id, [FromBody] UpdateContactDto updatedContact)
 {
     if (ModelState.IsValid)
     {
         return(buildResponse(await _service.UpdateContact(id, updatedContact)));
     }
     return(null);
 }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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());
        }
Ejemplo n.º 7
0
        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;
            }
        }
Ejemplo n.º 8
0
        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;
        }
Ejemplo n.º 9
0
        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);
            }
        }