Exemple #1
0
        public async Task GolfClubController_PATCH_ProduceTournamentResult_Successful()
        {
            // 1. Arrange
            HttpClient client = this.WebApplicationFactory.AddPlayer().CreateClient();

            TournamentPatchRequest tournamentPatchRequest = TestData.TournamentPatchRequestProduceTournamentResult;
            String uri = $"api/golfclubs/{TestData.GolfClubId}/tournaments/{TestData.TournamentId}";

            client.DefaultRequestHeaders.Add("api-version", "2.0");
            StringContent content = Helpers.CreateStringContent(tournamentPatchRequest);

            // 2. Act
            HttpResponseMessage response = await client.PatchAsync(uri, content, CancellationToken.None);

            // 3. Assert
            response.StatusCode.ShouldBe(HttpStatusCode.OK);
        }
        public async Task <IActionResult> PatchTournament([FromRoute] Guid golfClubId,
                                                          [FromRoute] Guid tournamentId,
                                                          TournamentPatchRequest tournamentPatchRequest,
                                                          CancellationToken cancellationToken)
        {
            // Get the Golf Club Id claim from the user
            Claim golfClubIdClaim = ClaimsHelper.GetUserClaim(this.User, CustomClaims.GolfClubId, golfClubId.ToString());

            if (tournamentPatchRequest.Status == TournamentStatusUpdate.Complete)
            {
                // Create the command
                CompleteTournamentCommand command = CompleteTournamentCommand.Create(Guid.Parse(golfClubIdClaim.Value), tournamentId);

                // Route the command
                await this.CommandRouter.Route(command, cancellationToken);
            }
            else if (tournamentPatchRequest.Status == TournamentStatusUpdate.Cancel)
            {
                CancelTournamentRequest cancelTournamentRequest = new CancelTournamentRequest
                {
                    CancellationReason = tournamentPatchRequest.CancellationReason
                };

                // Create the command
                CancelTournamentCommand command = CancelTournamentCommand.Create(Guid.Parse(golfClubIdClaim.Value), tournamentId, cancelTournamentRequest);

                // Route the command
                await this.CommandRouter.Route(command, cancellationToken);
            }
            else if (tournamentPatchRequest.Status == TournamentStatusUpdate.ProduceResult)
            {
                // Create the command
                ProduceTournamentResultCommand command = ProduceTournamentResultCommand.Create(Guid.Parse(golfClubIdClaim.Value), tournamentId);

                // Route the command
                await this.CommandRouter.Route(command, cancellationToken);
            }
            else
            {
                return(this.BadRequest());
            }

            // return the result
            return(this.Ok());
        }
        /// <summary>
        /// Cancels the tournament.
        /// </summary>
        /// <param name="accessToken">The access token.</param>
        /// <param name="golfClubId">The golf club identifier.</param>
        /// <param name="tournamentId">The tournament identifier.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        public async Task CancelTournament(String accessToken,
                                           Guid golfClubId,
                                           Guid tournamentId,
                                           CancelTournamentRequest request,
                                           CancellationToken cancellationToken)
        {
            String requestUri = $"{this.BaseAddress}/api/golfclubs/{golfClubId}/tournaments/{tournamentId}";

            try
            {
                // Add the access token to the client headers
                this.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

                // Create the patch request
                TournamentPatchRequest tournamentPatchRequest = new TournamentPatchRequest
                {
                    CancellationReason = request.CancellationReason,
                    Status             = TournamentStatusUpdate.Cancel
                };

                String requestSerialised = JsonConvert.SerializeObject(tournamentPatchRequest);

                StringContent httpContent = new StringContent(requestSerialised, Encoding.UTF8, "application/json");

                // Make the Http Call here
                HttpResponseMessage httpResponse = await this.Patch(requestUri, httpContent, cancellationToken);

                // Process the response
                await this.HandleResponse(httpResponse, cancellationToken);

                // call was successful there is no response object
            }
            catch (Exception ex)
            {
                // An exception has occurred, add some additional information to the message
                Exception exception = new Exception($"Error cancelling tournament {tournamentId}.", ex);

                throw exception;
            }
        }