コード例 #1
0
        public JsonResult Post(string planName, [FromBody] Course vm)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // Map to the Entity
                    var newCourse = Mapper.Map <Course>(vm);

                    // Save to the db
                    _repository.AddNewCourseToPlan(planName, User.Identity.Name, newCourse);

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

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Json("Validation failed on new course"));
        }