public async Task <IActionResult> AddMeasuredCourseToGolfClub([FromRoute] Guid golfClubId,
                                                                      [FromBody] AddMeasuredCourseToClubRequest 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());
            }

            Guid golfClubIdValue  = Guid.Parse(golfClubIdClaim.Value);
            Guid measuredCourseId = Guid.NewGuid();

            // Create the command
            AddMeasuredCourseToClubCommand command = AddMeasuredCourseToClubCommand.Create(golfClubIdValue, measuredCourseId, request);

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

            // return the result
            return(this.Created($"{GolfClubController.ControllerRoute}/{golfClubId}/measuredcourses/{measuredCourseId}",
                                new AddMeasuredCourseToClubResponse
            {
                GolfClubId = golfClubIdValue,
                MeasuredCourseId = measuredCourseId
            }));
        }
Ejemplo n.º 2
0
        public void AddMeasuredCourseToClubCommand_CanBeCreated_IsCreated()
        {
            AddMeasuredCourseToClubCommand command = AddMeasuredCourseToClubCommand.Create(GolfClubTestData.AggregateId, GolfClubTestData.MeasuredCourseId, GolfClubTestData.AddMeasuredCourseToClubRequest);

            command.ShouldNotBeNull();
            command.CommandId.ShouldNotBe(Guid.Empty);
            command.AddMeasuredCourseToClubRequest.ShouldNotBeNull();
            command.GolfClubId.ShouldBe(GolfClubTestData.AggregateId);
            command.MeasuredCourseId.ShouldBe(GolfClubTestData.MeasuredCourseId);
            command.AddMeasuredCourseToClubRequest.ShouldBe(GolfClubTestData.AddMeasuredCourseToClubRequest);
        }
        public void GolfClubCommandHandler_HandleCommand_AddMeasuredCourseToClubCommand_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>();

            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);

            AddMeasuredCourseToClubCommand command = GolfClubTestData.GetAddMeasuredCourseToClubCommand();

            Should.NotThrow(async() => { await handler.Handle(command, CancellationToken.None); });
        }
        public async Task <IActionResult> AddMeasuredCourseToGolfClub([FromRoute] Guid golfClubId,
                                                                      [FromBody] AddMeasuredCourseToClubRequest 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());
            }

            // Create the command
            AddMeasuredCourseToClubCommand command = AddMeasuredCourseToClubCommand.Create(Guid.Parse(golfClubIdClaim.Value), request.MeasuredCourseId, request);

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

            // return the result
            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(AddMeasuredCourseToClubCommand command,
                                         CancellationToken cancellationToken)
        {
            // Rehydrate the aggregate
            GolfClubAggregate golfClubAggregate = await this.GolfClubRepository.GetLatestVersion(command.GolfClubId, cancellationToken);

            // Translate the request to the input for AddMeasuredCourse
            MeasuredCourseDataTransferObject measuredCourse = new MeasuredCourseDataTransferObject
            {
                MeasuredCourseId     = command.MeasuredCourseId,
                Name                 = command.AddMeasuredCourseToClubRequest.Name,
                StandardScratchScore = command.AddMeasuredCourseToClubRequest.StandardScratchScore,
                TeeColour            = command.AddMeasuredCourseToClubRequest.TeeColour,
                Holes                = new List <HoleDataTransferObject>()
            };

            foreach (HoleDataTransferObjectRequest holeDataTransferObject in command.AddMeasuredCourseToClubRequest.Holes)
            {
                measuredCourse.Holes.Add(new HoleDataTransferObject
                {
                    HoleNumber     = holeDataTransferObject.HoleNumber,
                    Par            = holeDataTransferObject.Par,
                    StrokeIndex    = holeDataTransferObject.StrokeIndex,
                    LengthInYards  = holeDataTransferObject.LengthInYards,
                    LengthInMeters = holeDataTransferObject.LengthInMeters
                });
            }

            // Add the measured course
            golfClubAggregate.AddMeasuredCourse(measuredCourse);

            // Save the changes
            await this.GolfClubRepository.SaveChanges(golfClubAggregate, cancellationToken);

            // No Response to set
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates the handler.
 /// </summary>
 /// <param name="command">The command.</param>
 /// <returns></returns>
 private ICommandHandler CreateHandler(AddMeasuredCourseToClubCommand command)
 {
     return(new GolfClubCommandHandler(this.ClubRepository, this.OAuth2SecurityService, this.GolfClubMembershipApplicationService));
 }