Пример #1
0
        public IActionResult RegisterStudent([FromBody] RegisterStudentDto dto)
        {
            var    command = new RegisterStudentCommand(dto.Name, dto.Age, dto.Address);
            Result result  = _messages.Dispatch(command);

            return(FromResult(result));
        }
Пример #2
0
        public IActionResult RegisterStudent(RegisterStudentDto dto)
        {
            var command = new RegisterStudentCommand(dto.Name, dto.Surname);
            var result  = _messages.Dispatch(command);

            return(HandleCommandResult(result));
        }
Пример #3
0
        public void It_Validates_The_Name()
        {
            var command = new RegisterStudentCommand("", "*****@*****.**");
            var result  = validator.Validate(command);

            AssertHasValidationError(result, "StudentName");
        }
        public async Task Handle_CourseExists_StudentIsAdded()
        {
            //Arrange
            var courseId   = Guid.NewGuid();
            var studentDto = new StudentDto();
            var command    = new RegisterStudentCommand {
                CourseId = courseId, Student = studentDto
            };
            var course = new Course
            {
                Id       = courseId,
                Capacity = 10,
            };
            var student = new Student {
                FirstName = "Jonathan"
            };

            _repository.Setup(x => x.FindById(It.IsAny <Guid>())).ReturnsAsync(course);
            _mapper.Setup(x => x.Map <Student>(studentDto)).Returns(student);
            _mapper.Setup(x => x.Map <CourseDto>(course)).Returns(new CourseDto());

            //Act
            await _handler.Handle(command, CancellationToken.None);

            //Assert
            Assert.AreEqual(1, course.Students.Count);
            Assert.AreEqual("Jonathan", course.Students.FirstOrDefault()?.FirstName);
        }
        public async Task Handle_CourseExists_MapperIsCalled()
        {
            //Arrange
            var courseId   = Guid.NewGuid();
            var studentDto = new StudentDto();
            var command    = new RegisterStudentCommand {
                CourseId = courseId, Student = studentDto
            };
            var course = new Course
            {
                Id       = courseId,
                Capacity = 10,
            };

            _repository.Setup(x => x.FindById(It.IsAny <Guid>())).ReturnsAsync(course);
            _mapper.Setup(x => x.Map <Student>(studentDto)).Returns(new Student());
            _mapper.Setup(x => x.Map <CourseDto>(course)).Returns(new CourseDto());

            //Act
            await _handler.Handle(command, CancellationToken.None);

            //Assert
            _mapper.Verify(x => x.Map <Student>(studentDto), Times.Once);
            _mapper.Verify(x => x.Map <CourseDto>(course), Times.Once);
            _mapper.VerifyNoOtherCalls();
        }
        public async Task <IActionResult> Register([FromBody] RegisterStudentRequest request)
        {
            var command = new RegisterStudentCommand(request.Name, request.Email);
            var result  = await mediator.Send(command);

            return(FromPresenter(new RegisterStudentPresenter(result)));
        }
Пример #7
0
        public void Register(StudentViewModel sutdengtViewModel)
        {
            //_studentRepository.Add(_mapper.Map<Student>(sutdengtViewModel));
            //_studentRepository.SaveChanges();
            RegisterStudentCommand registerStudentCommand = _mapper.Map <RegisterStudentCommand>(sutdengtViewModel);

            Bus.SendCommand(registerStudentCommand);
        }
        public async Task Handle_CourseDoesNotExists_CourseNotFoundExceptionIsThrown()
        {
            //Arrange
            var command = new RegisterStudentCommand();

            //Act & Assert
            Assert.ThrowsAsync <CourseNotFoundException>(() => _handler.Handle(command, CancellationToken.None));
        }
Пример #9
0
        public async Task It_Returns_A_Student_Id()
        {
            var command = new RegisterStudentCommand("Jordan Walker", "*****@*****.**");
            var result  = await handler.Handle(command, new CancellationToken());

            Assert.True(result.IsSuccess);
            Assert.Equal(studentRepositorySpy.Students[0].Id, result.Value);
        }
 public RegisterStudentService(
     RegisterStudentCommand studentCommand,
     RegisterUserCommand userCommand,
     IStudentRepository repository) : base(studentCommand)
 {
     _studentCommand = studentCommand;
     _userCommand    = userCommand;
     _repository     = repository;
 }
Пример #11
0
        public async Task <IActionResult> RegisterStudent(RegisterStudentDto dto)
        {
            var command = new RegisterStudentCommand(dto.FirstName, dto.LastName, dto.NameSuffixId, dto.Email,
                                                     dto.FavoriteCourseId, dto.FavoriteCourseGrade);

            Result result = await _messages.Dispatch(command);

            return(FromResult(result));
        }
Пример #12
0
        public async Task It_Adds_A_Student_To_The_Repository()
        {
            var command = new RegisterStudentCommand("Jordan Walker", "*****@*****.**");
            var result  = await handler.Handle(command, new CancellationToken());

            Assert.Single(studentRepositorySpy.Students);
            Assert.Equal("Jordan Walker", studentRepositorySpy.Students[0].Name.Name);
            Assert.Equal("*****@*****.**", studentRepositorySpy.Students[0].Email.Address);
            Assert.True(unitOfWork.WasCommited);
        }
Пример #13
0
        public Task <IActionResult> RegisterUser([FromBody] RegisterStudentCommand student, [FromBody] RegisterUserCommand user)
        {
            RegisterStudentService service = new RegisterStudentService(student, user, null);

            service.Run();

            return(ReturnResponse(
                       service,
                       new { message = "Bem vindo ao balta.io" },
                       new { message = "Seu cadastro não pode ser efetuado" }
                       ));
        }
        public async Task RegisterStudent_CommandIsValid_MediatorIsCalled()
        {
            //Arrange
            var command = new RegisterStudentCommand();

            //Act
            var result = await _controller.RegisterStudent(command);

            //Assert
            _mediator.Verify(x => x.Send(command, It.IsAny <CancellationToken>()), Times.Once);
            _mediator.VerifyNoOtherCalls();
        }
Пример #15
0
        public async Task It_Notifies_The_Student_Of_The_Registration()
        {
            var command = new RegisterStudentCommand("Jordan Walker", "*****@*****.**");
            var result  = await handler.Handle(command, new CancellationToken());

            var notification = notificationService.Registrations.FirstOrDefault();

            Assert.NotNull(notification);
            Assert.Single(notificationService.Registrations);
            Assert.Equal(result.Value, notification.Id);
            Assert.Equal("Jordan Walker", notification.Name);
            Assert.Equal("*****@*****.**", notification.Email);
        }
Пример #16
0
        public void It_Validates_The_Email()
        {
            RegisterStudentCommand command;
            Result <Guid>          result;

            command = new RegisterStudentCommand("Jordan Walker", "");
            result  = validator.Validate(command);
            AssertHasValidationError(result, "StudentEmailAddress");

            command = new RegisterStudentCommand("Jordan Walker", "not_a_real_email");
            result  = validator.Validate(command);
            AssertHasValidationError(result, "StudentEmailAddress");
        }
Пример #17
0
        public Student(RegisterStudentCommand command)
        {
            if (command.HasNotifications())
            {
                return;
            }

            Id        = Guid.NewGuid();
            FirstName = command.FirstName;
            LastName  = command.LastName;
            Email     = command.Email;
            Document  = command.Document;
        }
        public async Task RegisterStudent_CommandIsValid_CreatedIsReturned()
        {
            //Arrange
            var command = new RegisterStudentCommand();

            _mediator.Setup(x => x.Send(command, It.IsAny <CancellationToken>()))
            .ReturnsAsync(new CourseDto());

            //Act
            var result = await _controller.RegisterStudent(command);

            //Assert
            Assert.IsInstanceOf <CreatedResult>(result);
        }
Пример #19
0
        public async Task <IActionResult> RegisterStudent(RegisterStudentCommand command)
        {
            Request.Headers.TryGetValue("Authorization", out var token);
            string role = await AuthHelper.GetRoleFromTokenAsync(token);

            if (role != "admin")
            {
                return(StatusCode(401, new { Error = "Unauthorized" }));
            }

            var result = await Mediator.Send(command);

            return(!result.Success ? StatusCode(result.Error.StatusCode, result.Error) : Ok(result));
        }
Пример #20
0
        public async Task It_Returns_A_Failed_Result_If_Student_Already_Exists()
        {
            var student = new Student(
                new StudentName("Jordan Walker"),
                new EmailAddress("*****@*****.**")
                );
            await studentRepositorySpy.Add(student);

            var command = new RegisterStudentCommand("Jordan Walker", "*****@*****.**");
            var result  = await handler.Handle(command, new CancellationToken());

            Assert.True(result.IsFailure);
            Assert.IsType <StudentAlreadyRegisteredError>(result.Error);
        }
        public ActionResult Create(StudentViewModel studentViewModel)
        {
            try
            {
                _cache.Remove("ErrorDate");
                ViewBag.ErrorData = null;
                // 视图模型验证
                if (!ModelState.IsValid)
                {
                    return(View(studentViewModel));
                }

                //添加命令验证,采用构造函数方法实例
                RegisterStudentCommand registerStudentCommand = new RegisterStudentCommand(studentViewModel.Name, studentViewModel.Email, studentViewModel.BirthDate, studentViewModel.Phone);

                //如果命令无效,证明有错误
                if (!registerStudentCommand.IsValid())
                {
                    List <string> errorInfo = new List <string>();
                    //获取到错误,请思考这个Result从哪里来的
                    foreach (var error in registerStudentCommand.ValidationResult.Errors)
                    {
                        errorInfo.Add(error.ErrorMessage);
                    }
                    //对错误进行记录,还需要抛给前台
                    ViewBag.ErrorData = errorInfo;
                    return(View(studentViewModel));
                }
                // 执行添加方法
                _studentAppService.Register(studentViewModel);
                ViewBag.Sucesso = "Student Registered!";
                return(View(studentViewModel));
            }
            catch (Exception e)
            {
                return(View(e.Message));
            }
        }
        public async Task Handle_CourseExists_RepositoryIsCalled()
        {
            //Arrange
            var courseId = Guid.NewGuid();
            var command  = new RegisterStudentCommand {
                CourseId = courseId
            };
            var course = new Course
            {
                Id       = courseId,
                Capacity = 10,
            };

            _repository.Setup(x => x.FindById(It.IsAny <Guid>())).ReturnsAsync(course);

            //Act
            await _handler.Handle(command, CancellationToken.None);

            //Assert
            _repository.Verify(x => x.FindById(command.CourseId), Times.Once);
            _repository.Verify(x => x.Upsert(course), Times.Once);
            _repository.VerifyNoOtherCalls();
        }
Пример #23
0
 public async Task <IActionResult> Register([FromBody] RegisterStudentCommand command)
 {
     return(Ok(await Mediator.Send(command)));
 }
Пример #24
0
        public async Task <IActionResult> RegisterStudent([FromBody] RegisterStudentCommand command)
        {
            var result = await _mediator.Send(command);

            return(Created(string.Empty, result));
        }
Пример #25
0
        public async Task <IActionResult> RegisterTwo([FromBody] RegisterStudentCommand student)
        {
            await Mediator.Send(student);

            return(NoContent());
        }