コード例 #1
0
        public async Task <ActionResult <Person> > CreateRecord([FromBody] string personString)
        {
            //filter the raw text from the input and make a person object
            Person pers = Filtering.CreatePersonFromString(personString);

            //check if the person we got from the body is properly formatted
            if (pers.FavoriteColor == null)
            {
                _logger.LogWarning("CreateRecord() call failed to create a new person record due to improperly formatted person");
                return(BadRequest("Attempt to create new record failed"));
            }
            else
            {
                if (!personExists(pers))
                {
                    _context.Add(pers);
                }
                else
                {
                    _logger.LogWarning("CreateRecord() tried to add duplicate person");
                    return(BadRequest("Attempt to create new record failed"));
                }

                //sanity check that the db updated
                if (await _context.SaveChangesAsync() > 0)
                {
                    return(pers);
                }
                else
                {
                    _logger.LogWarning("CreateRecord() call failed");
                    return(BadRequest("Attempt to create new record failed"));
                }
            }
        }