Ejemplo n.º 1
0
        public JsonResult Post([FromBody] LearningPlanViewModel vm)
        {
            try {
                if (ModelState.IsValid)
                {
                    var newPlan = Mapper.Map <LearningPlan>(vm);
                    newPlan.UserName = User.Identity.Name;

                    // Save to the db
                    _logger.LogInformation("Attempting to save a new plan");
                    _repository.AddLearningPlan(newPlan);

                    if (_repository.SaveAll())
                    {
                        Response.StatusCode = (int)HttpStatusCode.Created;
                        return(Json(Mapper.Map <LearningPlanViewModel>(newPlan)));
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Failed to save new plan", ex);
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return(Json(new { Message = ex.Message }));
            }
            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json(new { Message = "Failed", ModelState = ModelState }));
        }