Ejemplo n.º 1
0
        public async Task <ApplicationResponse <bool> > SignUpStudentToCourseAsync(Guid courseId, StudentDto student)
        {
            var appResp = new ApplicationResponse <bool>();
            var command = _mapper.Map <SignUpToCourseCommand>(student);

            command.CourseId = courseId;
            var signUpToCourseCommandResponse = await _signUpToCourseHandler.Handle(command);

            if (signUpToCourseCommandResponse.Succeeded)
            {
                var updCourseStatisticsCommand = new UpdateCourseStatisticsCommand
                {
                    Course  = signUpToCourseCommandResponse.Course,
                    Student = signUpToCourseCommandResponse.Student
                };

                var updateCourseStatisticsCommandResponse = await _updateCourseStatisticsHandler.Handle(updCourseStatisticsCommand);

                if (!updateCourseStatisticsCommandResponse.Succeeded)
                {
                    appResp.Message = "Oops, Something went wrong updating statistics.";
                    appResp.AddErrorsRange(updateCourseStatisticsCommandResponse.Errors);
                }
                else
                {
                    var email = new Email();
                    email.To   = student.Email;
                    email.Body = "Contratulation, You have been enrolled to a new Course";
                    await _emailSender.SendEmailAsync(email);

                    appResp.Message  = "OK - Success";
                    appResp.Response = true;
                }

                return(appResp);
            }
            else
            {
                appResp.Message = "Sorry, Something went wrong";
                appResp.AddErrorsRange(signUpToCourseCommandResponse.Errors);
            }

            return(appResp);
        }
Ejemplo n.º 2
0
        public async Task Update_Course_Statistics_Average_Age_Test(string dateOfBirth, int sumAges, int numberOfStudents, string courseName)
        {
            var courseSt = new CourseStatistics
            {
                CourseId = Guid.NewGuid(),
                Name     = courseName,
                SumAges  = sumAges
            };

            var mock = new Mock <ICourseStatisticsRepository>();

            mock.Setup(r => r.GetByCourseIdAsync(It.IsAny <Guid>()))
            .Returns(Task.FromResult(courseSt));

            mock.Setup(r => r.UpdateAsync(It.IsAny <CourseStatistics>()))
            .Returns(Task.FromResult(true));

            var signUpCommandHandler = new UpdateCourseStatisticsCommandHandler(mock.Object, logger);

            var updCourseStatisticsCommander = new UpdateCourseStatisticsCommand
            {
                Course = new Course {
                    Id = courseSt.CourseId, Name = courseSt.Name, NumberOfStudents = numberOfStudents
                },
                Student = new Student {
                    DateOfBirth = DateTime.Parse(dateOfBirth)
                }
            };

            var commandResponse = await signUpCommandHandler.Handle(updCourseStatisticsCommander);

            var age        = new DateTime(DateTime.Now.Subtract(DateTime.Parse(dateOfBirth)).Ticks).Year - 1;
            var averageAge = (decimal)(sumAges + age) / numberOfStudents;

            output.WriteLine("Course statistics: {0}, Average Age: {1}", commandResponse.CourseStatistics.Name, commandResponse.CourseStatistics.AverageAge);
            Assert.Equal(age, updCourseStatisticsCommander.Student.Age);
            Assert.Equal(averageAge, commandResponse.CourseStatistics.AverageAge);
        }