示例#1
0
        public IActionResult Put([FromBody] MessageViewModel model)
        {
            //return a generic HTTP status 500 (server error )
            //if the pay load is invalid
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }

            //mapping the message to the model
            var message = model.Adapt <Message>();

            //set data received from model into the mapped variable
            model.Content     = message.Content;
            model.DateCreated = DateTime.Now;
            model.Status      = message.Status;

            //add the mapped variable into the dbContext.
            DbContext.Messages.Add(message);

            //persist the data into the database
            DbContext.SaveChanges();

            //return the newly add message to the client side
            return(new JsonResult(message.Adapt <MessageViewModel>(), new JsonSerializerSettings()
            {
                Formatting = Formatting.Indented
            }));
        }