Пример #1
0
        public async Task <ClientResponse <ContactDto> > CreateContact(CreateContactDto contactDto)
        {
            var response = new ClientResponse <ContactDto>();

            try
            {
                if (contactDto.BirthDate > DateTime.Now)
                {
                    response.Message = E.ErrHigherBirthDate;
                    response.Status  = System.Net.HttpStatusCode.BadRequest;
                    return(response);
                }
                if (await validateContactNotExists(contactDto.Email, contactDto.Name))
                {
                    response.Message = E.ErrContactAlreadyExists;
                    response.Status  = System.Net.HttpStatusCode.BadRequest;
                    return(response);
                }
                if (await validateCityAndCompany(contactDto.CityID, contactDto.CompanyID))
                {
                    var contact = new Mo.Contact
                    {
                        Name         = H.UppercaseFirst(contactDto.Name),
                        BirthDate    = contactDto.BirthDate,
                        StreetName   = H.UppercaseFirst(contactDto.StreetName),
                        StreetNumber = contactDto.StreetNumber,
                        CompanyID    = contactDto.CompanyID,
                        CityID       = contactDto.CityID,
                        ProfileImage = contactDto.ProfileImage,
                        Email        = contactDto.Email.ToLower(),
                        Phones       = new List <Mo.Phone>(),
                        Active       = true
                    };

                    contact.CreatedAt = DateTime.Now;
                    foreach (PhoneDto p in contactDto.Phones)
                    {
                        contact.Phones.Add(_mapper.Map <Mo.Phone>(p));
                    }
                    _repository.Contacts.Add(contact);

                    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);
            }
        }
Пример #2
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;
        }