コード例 #1
0
        public async Task <IActionResult> Edit(int id, CourseFormModel course)
        {
            if (!ModelState.IsValid)
            {
                course.Trainers = await this.GenerateTrainersSelectListItems();

                return(View(course));
            }

            if (!this.courses.CourseExist(id))
            {
                this.GenerateMessage(string.Format(NonExistingCourse, id), Alert.Warning);
                return(RedirectToAction(nameof(CoursesController.All), WebConstants.Controller.Courses));
            }

            if (course.StartDate <= DateTime.Now)
            {
                return(await InvalidCourseDates(course, CourseStartDateInFuture));
            }

            if (course.EndDate <= course.StartDate)
            {
                return(await InvalidCourseDates(course, CourseEndDateAfterStartDate));
            }

            this.courses.Edit(id, course.Name, course.Description, course.TrainerId, course.StartDate, course.EndDate);

            this.GenerateMessage(string.Format(SuccessCourseEdit, id), Alert.Success);

            return(RedirectToAction(nameof(CoursesController.All), WebConstants.Controller.Courses));
        }
コード例 #2
0
        public async Task <IActionResult> Add(CourseFormModel course)
        {
            if (!ModelState.IsValid)
            {
                course.Trainers = await GenerateTrainersSelectListItems();

                return(View(course));
            }

            if (course.StartDate <= DateTime.Now)
            {
                return(await InvalidCourseDates(course, CourseStartDateInFuture));
            }

            if (course.EndDate <= course.StartDate)
            {
                return(await InvalidCourseDates(course, CourseEndDateAfterStartDate));
            }

            var trainerName = this.users.GetNameById(course.TrainerId);

            this.courses.Add(course.Name, course.Description, course.TrainerId, course.StartDate, course.EndDate);

            this.GenerateMessage(string.Format(SuccessCourseCreation, course.Name, trainerName), Alert.Success);

            return(RedirectToAction(nameof(CoursesController.All), WebConstants.Controller.Courses));
        }
コード例 #3
0
        private async Task <IActionResult> InvalidCourseDates(CourseFormModel course, string message)
        {
            course.Trainers = await GenerateTrainersSelectListItems();

            ModelState.AddModelError(string.Empty, message);
            return(View(course));
        }
コード例 #4
0
        public async Task <IActionResult> Create()
        {
            var model = new CourseFormModel
            {
                Trainers  = await this.GetTrainersAsync(),
                StartDate = DateTime.Now, // local time
                EndDate   = DateTime.Now  // local time
            };

            return(this.View(CourseFormView, model));
        }
コード例 #5
0
        private void AssertEqualViewResultWithModel(CourseFormModel form, IActionResult result)
        {
            var viewResult = Assert.IsType <ViewResult>(result);

            Assert.Equal(CoursesController.CourseFormView, viewResult.ViewName);

            var model = Assert.IsType <CourseFormModel>(viewResult.Model);

            Assert.NotNull(model);

            this.AssertTrainersSelectList(model.Trainers);
            Assert.Equal(form, model);
        }
コード例 #6
0
        private void AssertAdminCourseForm(CourseFormModel model, FormActionEnum expectedAction)
        {
            var expectedCourse = this.GetAdminCourse();

            Assert.NotNull(model);
            Assert.Equal(expectedAction, model.Action);
            Assert.Equal(expectedCourse.Name, model.Name);
            Assert.Equal(expectedCourse.Description, model.Description);
            Assert.Equal(expectedCourse.StartDate, model.StartDate);
            Assert.Equal(expectedCourse.EndDate, model.EndDate);
            Assert.Equal(expectedCourse.TrainerId, model.TrainerId);

            this.AssertTrainersSelectList(model.Trainers);
        }
コード例 #7
0
        public async Task <IActionResult> Edit(int id, CourseFormModel model)
        {
            var courseExists = this.courseService.Exists(id);

            if (!courseExists)
            {
                this.TempData.AddErrorMessage(WebConstants.CourseNotFoundMsg);
                return(this.RedirectToAction(nameof(Web.Controllers.CoursesController.Index), WebConstants.CoursesController));
            }

            var user = await this.userManager.FindByIdAsync(model.TrainerId);

            if (user == null)
            {
                this.ModelState.AddModelError(string.Empty, WebConstants.InvalidUserMsg);
            }

            if (!this.ModelState.IsValid)
            {
                model.Trainers = await this.GetTrainersAsync();

                return(this.View(CourseFormView, model));
            }

            var success = await this.adminCourseService.UpdateAsync(
                id,
                model.Name,
                model.Description,
                model.StartDate,
                model.EndDate,
                model.Price,
                model.TrainerId);

            if (!success)
            {
                this.TempData.AddErrorMessage(WebConstants.CourseUpdateErrorMsg);

                model.Trainers = await this.GetTrainersAsync();

                return(this.View(CourseFormView, model));
            }

            this.TempData.AddSuccessMessage(WebConstants.CourseUpdateSuccessMsg);

            return(this.RedirectToAction(
                       nameof(Web.Controllers.CoursesController.Details),
                       WebConstants.CoursesController,
                       routeValues: new { id }));
        }
コード例 #8
0
        public async Task <IActionResult> Delete(int id, CourseFormModel model)
        {
            var courseExists = this.courseService.Exists(id);

            if (!courseExists)
            {
                this.TempData.AddErrorMessage(WebConstants.CourseNotFoundMsg);
                return(this.RedirectToAction(nameof(Web.Controllers.CoursesController.Index), WebConstants.CoursesController));
            }
            var success = await this.adminCourseService.RemoveAsync(id);

            if (!success)
            {
                this.TempData.AddErrorMessage(WebConstants.CourseDeleteErrorMsg);
                return(this.RedirectToAction(nameof(Delete), new { id }));
            }

            this.TempData.AddSuccessMessage(WebConstants.CourseDeleteSuccessMsg);

            return(this.RedirectToAction(nameof(Web.Controllers.CoursesController.Index), WebConstants.CoursesController));
        }
コード例 #9
0
        public async Task <IActionResult> Create(CourseFormModel model)
        {
            if (!ModelState.IsValid)
            {
                var trainers = await this.userService.Trainers();


                model.Trainers = trainers
                                 .Select(t => new SelectListItem
                {
                    Value = t.TrainerId,
                    Text  = t.Name
                });

                return(View(model));
            }

            await this.courseService.CreateAsync(model.Tilte, model.Description,
                                                 model.StartDate, model.EndDate, model.Credits, model.TrainerId);

            return(RedirectToAction("Index", "Home"));
        }
コード例 #10
0
        public async Task <IActionResult> Create(CourseFormModel model)
        {
            var user = await this.userManager.FindByIdAsync(model.TrainerId);

            if (user == null)
            {
                this.ModelState.AddModelError(string.Empty, WebConstants.InvalidUserMsg);
            }

            if (!this.ModelState.IsValid)
            {
                model.Trainers = await this.GetTrainersAsync();

                return(this.View(CourseFormView, model));
            }

            var id = await this.adminCourseService.CreateAsync(
                model.Name,
                model.Description,
                model.StartDate,
                model.EndDate,
                model.Price,
                model.TrainerId);

            if (id < 0)
            {
                this.TempData.AddErrorMessage(WebConstants.CourseCreateErrorMsg);

                model.Trainers = await this.GetTrainersAsync();

                return(this.View(CourseFormView, model));
            }

            this.TempData.AddSuccessMessage(WebConstants.CourseCreateSuccessMsg);

            return(this.RedirectToAction(nameof(Web.Controllers.CoursesController.Index), WebConstants.CoursesController));
        }