Exemple #1
0
        public void AddCourseToGroup_with_invalid_model_does_not_call_add_course_service_and_returns_view()
        {
            // Given
            const int groupId         = 1;
            const int customisationId = 25;
            var       formData        = new AddCourseFormData
            {
                MonthsToComplete = "invalidString",
            };

            groupCoursesController.ModelState.AddModelError(nameof(AddCourseFormData.MonthsToComplete), "Is Invalid.");
            A.CallTo(() => courseService.GetCourseCategoryId(customisationId, ControllerContextHelper.CentreId))
            .Returns(1);

            // When
            var result = groupCoursesController.AddCourseToGroup(formData, groupId, customisationId);

            // Then
            using (new AssertionScope())
            {
                A.CallTo(
                    () => groupsService.AddCourseToGroup(
                        A <int> ._,
                        A <int> ._,
                        A <int> ._,
                        A <int> ._,
                        A <bool> ._,
                        A <int?> ._,
                        A <int> ._
                        )
                    )
                .MustNotHaveHappened();
                result.Should().BeViewResult().WithDefaultViewName();
            }
        }
        public IActionResult AddCourseToGroup(AddCourseFormData formData, int groupId, int customisationId)
        {
            var centreId = User.GetCentreId();

            if (!ModelState.IsValid)
            {
                var courseCategoryId = courseService.GetCourseCategoryId(customisationId, centreId) !.Value;
                var supervisors      = userService.GetSupervisorsAtCentreForCategory(centreId, courseCategoryId);
                var model            = new AddCourseViewModel(formData, groupId, customisationId, supervisors);
                return(View(model));
            }

            var completeWithinMonths = formData.MonthsToComplete == null ? 0 : int.Parse(formData.MonthsToComplete);

            groupsService.AddCourseToGroup(
                groupId,
                customisationId,
                completeWithinMonths,
                User.GetAdminId() !.Value,
                formData.CohortLearners,
                formData.SupervisorId,
                centreId
                );

            var confirmationViewModel = new AddCourseConfirmationViewModel(
                groupId,
                formData.GroupName,
                formData.CourseName,
                completeWithinMonths
                );

            return(View("AddCourseToGroupConfirmation", confirmationViewModel));
        }