コード例 #1
0
        public ActionResult AddCourse(string course_name, CourseStatus status)
        {
            var result = new ResultData <Course>()
            {
                Error = true, Status = HttpStatusCode.BadRequest
            };

            try
            {
                CourseServices.ValidateName(course_name);
                Course course = new Course(course_name, status);
                if (db.Curso.Where(q => q.Name.ToLower() == course_name.ToLower()).Any())
                {
                    throw new ArgumentException($"O nome {course.Name} já esta cadastrado");
                }

                db.Curso.Add(course);
                db.SaveChanges();
                result.Error  = false;
                result.Status = HttpStatusCode.OK;
                result.Data   = db.Curso.ToList();
                return(Ok(result));
            }
            catch (Exception e)
            {
                result.Message.Add(e.Message);
                return(BadRequest(result));
            }
            finally
            {
                db.Dispose();
            }
        }