Example #1
0
        public IActionResult UpdateContact(Guid contactId, ContactForManipulationModel contact)
        {
            var contactFromRepo = _contactsRepository.GetContact(contactId);

            if (contactFromRepo == null)
            {
                var contactToAdd = _mapper.Map <Entities.Contact>(contact);
                contactToAdd.Id = contactId;

                _contactsRepository.AddContact(contactToAdd);

                _contactsRepository.Save();

                var contactToReturn = _mapper.Map <ContactModel>(contactToAdd);

                return(CreatedAtRoute("GetContact",
                                      new { contactId = contactToReturn.Id },
                                      contactToReturn));
            }

            // map the entity to a ContactForUpdateModel
            // apply the updated field values to that dto
            // map the ContactForUpdateModel back to an entity
            _mapper.Map(contact, contactFromRepo);

            _contactsRepository.UpdateContact(contactFromRepo);

            _contactsRepository.Save();
            return(NoContent());
        }
Example #2
0
        public ActionResult <ContactModel> CreateContact(ContactForManipulationModel contact)
        {
            var contactEntity = _mapper.Map <Entities.Contact>(contact);

            _contactsRepository.AddContact(contactEntity);
            _contactsRepository.Save();

            var contactToReturn = _mapper.Map <ContactModel>(contactEntity);

            return(CreatedAtRoute("GetContact",
                                  new { contactId = contactToReturn.Id },
                                  contactToReturn));
        }