Exemplo n.º 1
0
        public IActionResult Create(CreateViewModel course)
        {
            string callerId = _userManager.GetUserId(HttpContext.User);

            if (ModelState.IsValid)
            {
                CourseViewModel result;

                Course editingCourse;

                try
                {
                    if (course.Id.HasValue && (editingCourse =
                                                   _context.Courses.Include(c => c.Teachers)
                                                   .FirstOrDefault(c => c.Id == course.Id.Value)) != null)
                    {
                        result = _courseLogic.Edit(callerId, editingCourse.Id, course);
                    }
                    else
                    {
                        result = _courseLogic.Create(callerId, course);
                    }
                    return(RedirectToAction("Index", "Course"));
                }
                catch (NotFoundException)
                {
                    return(NotFound());
                }
                catch (IllegalAccessException)
                {
                    return(Challenge());
                }
            }
            return(View(course));
        }
Exemplo n.º 2
0
        public JsonResult CreateCourse(string code, string name, int progId, int deptId, int levelId)
        {
            JsonResponseModel result = new JsonResponseModel();

            try
            {
                if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(name) && progId > 0 && deptId > 0 && levelId > 0)
                {
                    CourseLogic courseLogic    = new CourseLogic();
                    COURSE      existingCourse = courseLogic.GetEntitiesBy(c => c.Code.ToLower().Trim() == code.ToLower().Trim()).LastOrDefault();
                    if (existingCourse == null)
                    {
                        existingCourse = new COURSE();

                        existingCourse.Department_Id = deptId;
                        existingCourse.Programme_Id  = progId;
                        existingCourse.Level_Id      = levelId;
                        existingCourse.Code          = code;
                        existingCourse.Name          = name;
                        existingCourse.Active        = true;

                        courseLogic.Create(existingCourse);

                        result.IsError = false;
                        result.Message = "Operation Successful!";
                    }
                    else
                    {
                        result.IsError = true;
                        result.Message = "Course with the same code exists.";
                    }
                }
                else
                {
                    result.IsError = true;
                    result.Message = "Invalid parameter";
                }
            }
            catch (Exception ex)
            {
                result.IsError = true;
                result.Message = ex.Message;
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }