public async Task <IActionResult> Post([FromBody] Dictionary <string, string> course)
        {
            if (Validation.IsAnyInputObjectDataNotSpecified(new List <object> {
                course.ContainsKey("Name") ? course["Name"] : null,
                course.ContainsKey("StartDate") ? course["StartDate"] : null, course.ContainsKey("EndDate") ? course["EndDate"] : null,
                course.ContainsKey("PassingScore") ? course["PassingScore"] : null
            }))
            {
                return(BadRequest("Error: all course input data must be specified"));
            }

            course["Name"]         = course["Name"].Trim();
            course["StartDate"]    = course["StartDate"].Trim();
            course["EndDate"]      = course["EndDate"].Trim();
            course["PassingScore"] = course["PassingScore"].Trim();

            if (!Validation.ValidateDateTimeAndGetParsed(course["StartDate"], out DateTime resultStartDate))
            {
                return(BadRequest("Error: start date is incorrect"));
            }

            if (!Validation.ValidateDateTimeAndGetParsed(course["EndDate"], out DateTime resultEndDate))
            {
                return(BadRequest("Error: end date is incorrect"));
            }

            if (!Validation.ValidateIntAndGetParsed(course["PassingScore"], out int resultPassingScore) || resultPassingScore <= 0)
            {
                return(BadRequest("Error: passing score is incorrect"));
            }

            var resultCourse = new Course
            {
                Name         = course["Name"],
                StartDate    = resultStartDate,
                EndDate      = resultEndDate,
                PassingScore = resultPassingScore
            };

            using (var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
            {
                try
                {
                    connection.Open();

                    await CoursesRepository.CreateCourseAsync(connection, resultCourse);
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }

            return(Ok(resultCourse));
        }
示例#2
0
        public async Task <ActionResult> CreateAsync(Course course)
        {
            if (ModelState.IsValid)
            {
                if (course.Id == default(Guid))
                {
                    await _coursesRepository.CreateCourseAsync(course);
                }
                else
                {
                    await _coursesRepository.UpdateCourseAsync(course.Id, course);
                }

                return(RedirectToAction("Index"));
            }

            return(View(course));
        }