public async Task <IActionResult> CreateMatchSecretary([FromRoute] Guid golfClubId,
                                                               [FromBody] CreateMatchSecretaryRequest request,
                                                               CancellationToken cancellationToken)
        {
            // Get the Golf Club Id claim from the user
            Claim golfClubIdClaim = ClaimsHelper.GetUserClaim(this.User, CustomClaims.GolfClubId, golfClubId.ToString());

            Boolean validationResult = ClaimsHelper.ValidateRouteParameter(golfClubId, golfClubIdClaim);

            if (validationResult == false)
            {
                return(this.Forbid());
            }

            CreateMatchSecretaryCommand command = CreateMatchSecretaryCommand.Create(Guid.Parse(golfClubIdClaim.Value), request);

            await this.CommandRouter.Route(command, cancellationToken);

            // return the result
            return(this.Created($"{GolfClubController.ControllerRoute}/{golfClubId}/users/{request.EmailAddress}",
                                new CreateMatchSecretaryResponse
            {
                GolfClubId = golfClubId,
                UserName = request.EmailAddress
            }));
        }
예제 #2
0
        public void CreateMatchSecretaryCommand_CanBeCreated_IsCreated()
        {
            CreateMatchSecretaryCommand command = CreateMatchSecretaryCommand.Create(GolfClubTestData.AggregateId, GolfClubTestData.CreateMatchSecretaryRequest);

            command.ShouldNotBeNull();
            command.CommandId.ShouldNotBe(Guid.Empty);
            command.GolfClubId.ShouldBe(GolfClubTestData.AggregateId);
            command.CreateMatchSecretaryRequest.ShouldNotBeNull();
            command.CreateMatchSecretaryRequest.ShouldBe(GolfClubTestData.CreateMatchSecretaryRequest);
        }
        public void GolfClubCommandHandler_HandleCommand_CreateMatchSecretaryCommand_CommandHandled()
        {
            Mock <IAggregateRepository <GolfClubAggregate> > repository = new Mock <IAggregateRepository <GolfClubAggregate> >();

            repository.Setup(r => r.GetLatestVersion(It.IsAny <Guid>(), CancellationToken.None)).ReturnsAsync(GolfClubTestData.GetCreatedGolfClubAggregate);
            Mock <ISecurityService> oAuth2SecurityService = new Mock <ISecurityService>();
            Mock <IGolfClubMembershipApplicationService> golfClubMembershipApplicationService = new Mock <IGolfClubMembershipApplicationService>();
            GolfClubCommandHandler handler = new GolfClubCommandHandler(repository.Object, oAuth2SecurityService.Object,
                                                                        golfClubMembershipApplicationService.Object);

            oAuth2SecurityService
            .Setup(o => o.RegisterUser(It.IsAny <RegisterUserRequest>(), CancellationToken.None))
            .ReturnsAsync(GolfClubTestData.GetRegisterUserResponse());

            CreateMatchSecretaryCommand command = GolfClubTestData.GetCreateMatchSecretaryCommand();

            Should.NotThrow(async() => { await handler.Handle(command, CancellationToken.None); });
        }
        public async Task <IActionResult> CreateMatchSecretary([FromRoute] Guid golfClubId,
                                                               [FromBody] CreateMatchSecretaryRequest request,
                                                               CancellationToken cancellationToken)
        {
            // Get the Golf Club Id claim from the user
            Claim golfClubIdClaim = ClaimsHelper.GetUserClaim(this.User, CustomClaims.GolfClubId);

            Boolean validationResult = ClaimsHelper.ValidateRouteParameter(golfClubId, golfClubIdClaim);

            if (validationResult == false)
            {
                return(this.Forbid());
            }

            CreateMatchSecretaryCommand command = CreateMatchSecretaryCommand.Create(Guid.Parse(golfClubIdClaim.Value), request);

            await this.CommandRouter.Route(command, cancellationToken);

            return(this.NoContent());
        }
        /// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        private async Task HandleCommand(CreateMatchSecretaryCommand command,
                                         CancellationToken cancellationToken)
        {
            Guid golfClubAggregateId = command.GolfClubId;

            // Rehydrate the aggregate
            GolfClubAggregate golfClubAggregate = await this.GolfClubRepository.GetLatestVersion(golfClubAggregateId, cancellationToken);

            // Create the user
            RegisterUserRequest registerUserRequest = new RegisterUserRequest
            {
                EmailAddress = command.CreateMatchSecretaryRequest.EmailAddress,
                Claims       = new Dictionary <String, String>
                {
                    { "GolfClubId", golfClubAggregateId.ToString() }
                },
                Password    = "******",
                PhoneNumber = command.CreateMatchSecretaryRequest.TelephoneNumber,
                MiddleName  = command.CreateMatchSecretaryRequest.MiddleName,
                FamilyName  = command.CreateMatchSecretaryRequest.FamilyName,
                GivenName   = command.CreateMatchSecretaryRequest.GivenName,
                Roles       = new List <String>
                {
                    RoleNames.MatchSecretary
                }
            };

            // Create the user
            RegisterUserResponse registerUserResponse = await this.OAuth2SecurityService.RegisterUser(registerUserRequest, cancellationToken);

            // Record against the aggregate
            golfClubAggregate.CreateMatchSecretarySecurityUser(registerUserResponse.UserId);

            // Save the changes
            await this.GolfClubRepository.SaveChanges(golfClubAggregate, cancellationToken);
        }
예제 #6
0
 /// <summary>
 /// Creates the handler.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <returns></returns>
 private ICommandHandler CreateHandler(CreateMatchSecretaryCommand command)
 {
     return(new GolfClubCommandHandler(this.ClubRepository, this.OAuth2SecurityService, this.GolfClubMembershipApplicationService));
 }