/// <summary> /// Handles the command. /// </summary> /// <param name="command">The command.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> private async Task HandleCommand(CreateGolfClubCommand command, CancellationToken cancellationToken) { Guid golfClubAggregateId = command.GolfClubId; // Rehydrate the aggregate GolfClubAggregate golfClubAggregate = await this.GolfClubRepository.GetLatestVersion(golfClubAggregateId, cancellationToken); // Call the aggregate method golfClubAggregate.CreateGolfClub(command.CreateGolfClubRequest.Name, command.CreateGolfClubRequest.AddressLine1, command.CreateGolfClubRequest.AddressLine2, command.CreateGolfClubRequest.Town, command.CreateGolfClubRequest.Region, command.CreateGolfClubRequest.PostalCode, command.CreateGolfClubRequest.TelephoneNumber, command.CreateGolfClubRequest.Website, command.CreateGolfClubRequest.EmailAddress); // Record club admin user against aggregate golfClubAggregate.CreateGolfClubAdministratorSecurityUser(command.SecurityUserId); // Save the changes await this.GolfClubRepository.SaveChanges(golfClubAggregate, cancellationToken); // Setup the response command.Response = new CreateGolfClubResponse { GolfClubId = golfClubAggregateId }; }
public async Task <IActionResult> CreateGolfClub([FromBody] CreateGolfClubRequest request, CancellationToken cancellationToken) { if (ClaimsHelper.IsPasswordToken(this.User) == false) { return(this.Forbid()); } // Get the user id (subject) for the user Claim subjectIdClaim = ClaimsHelper.GetUserClaim(this.User, JwtClaimTypes.Subject); // Get the Golf Club Id claim from the user Claim golfClubIdClaim = ClaimsHelper.GetUserClaim(this.User, CustomClaims.GolfClubId); Guid golfClubId = Guid.Parse(golfClubIdClaim.Value); Guid securityUserId = Guid.Parse(subjectIdClaim.Value); // Create the command CreateGolfClubCommand command = CreateGolfClubCommand.Create(golfClubId, securityUserId, request); // Route the command await this.CommandRouter.Route(command, cancellationToken); // return the result return(this.Created($"{GolfClubController.ControllerRoute}/{golfClubId}", new CreateGolfClubResponsev2 { GolfClubId = golfClubId })); }
public void CreateGolfClubCommand_CanBeCreated_IsCreated() { CreateGolfClubCommand command = CreateGolfClubCommand.Create(GolfClubTestData.AggregateId, GolfClubTestData.GolfClubAdministratorSecurityUserId, GolfClubTestData.CreateGolfClubRequest); command.ShouldNotBeNull(); command.CommandId.ShouldNotBe(Guid.Empty); command.GolfClubId.ShouldBe(GolfClubTestData.AggregateId); command.SecurityUserId.ShouldBe(GolfClubTestData.GolfClubAdministratorSecurityUserId); command.CreateGolfClubRequest.ShouldNotBeNull(); command.CreateGolfClubRequest.ShouldBe(GolfClubTestData.CreateGolfClubRequest); }
public void GolfClubCommandHandler_HandleCommand_CreateGolfClubCommand_CommandHandled() { Mock <IAggregateRepository <GolfClubAggregate> > repository = new Mock <IAggregateRepository <GolfClubAggregate> >(); repository.Setup(r => r.GetLatestVersion(It.IsAny <Guid>(), CancellationToken.None)).ReturnsAsync(GolfClubTestData.GetEmptyGolfClubAggregate()); Mock <ISecurityService> oAuth2SecurityService = new Mock <ISecurityService>(); oAuth2SecurityService .Setup(o => o.RegisterUser(It.IsAny <RegisterUserRequest>(), CancellationToken.None)) .ReturnsAsync(GolfClubTestData.GetRegisterUserResponse()); Mock <IGolfClubMembershipApplicationService> golfClubMembershipApplicationService = new Mock <IGolfClubMembershipApplicationService>(); GolfClubCommandHandler handler = new GolfClubCommandHandler(repository.Object, oAuth2SecurityService.Object, golfClubMembershipApplicationService.Object); CreateGolfClubCommand command = GolfClubTestData.GetCreateGolfClubCommand(); Should.NotThrow(async() => { await handler.Handle(command, CancellationToken.None); }); }
public async Task <IActionResult> CreateGolfClub([FromBody] CreateGolfClubRequest request, CancellationToken cancellationToken) { if (ClaimsHelper.IsPasswordToken(this.User) == false) { return(this.Forbid()); } // Get the user id (subject) for the user Claim subjectIdClaim = ClaimsHelper.GetUserClaim(this.User, JwtClaimTypes.Subject, null); // Get the Golf Club Id claim from the user Claim golfClubIdClaim = ClaimsHelper.GetUserClaim(this.User, CustomClaims.GolfClubId); // Create the command CreateGolfClubCommand command = CreateGolfClubCommand.Create(Guid.Parse(golfClubIdClaim.Value), Guid.Parse(subjectIdClaim.Value), request); // Route the command await this.CommandRouter.Route(command, cancellationToken); // return the result return(this.Created($"api/golfclub/{command.Response}", command.Response)); }
/// <summary> /// Creates the handler. /// </summary> /// <param name="command">The command.</param> /// <returns></returns> private ICommandHandler CreateHandler(CreateGolfClubCommand command) { return(new GolfClubCommandHandler(this.ClubRepository, this.OAuth2SecurityService, this.GolfClubMembershipApplicationService)); }