예제 #1
0
        public async Task <IActionResult> Post([FromBody] ContactModel model) // FromBody allows us to map request bodies to .NET Objects
        {
            try
            {
                _logger.LogInformation("Creating a contact");

                var contact = _mapper.Map <Contact>(model);// map back to contact enitity for DB; have to do ReverseMap in mappings profile

                var result = await _contactStore.InsertAsync(contact);

                // don't assume the insert worked...
                if (result != null &&
                    result.New != null &&
                    result.New.Name == model.Name &&
                    !string.IsNullOrWhiteSpace(result.Key))
                {
                    var contactModel = _mapper.Map <ContactModel>(result.New); // back to ContactModel for the end user
                    var newUri       = Url.Link("ContactGet", new { key = result.New.Key });

                    return(Created(newUri, contactModel));
                }

                _logger.LogWarning("Could not save contact to the database");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Threw exception while saving contact: {ex}");
            }

            return(BadRequest());
        }