public async Task <IActionResult> GetNumberOfMembersByAgeCategoryReport(Guid golfClubId,
                                                                                CancellationToken cancellationToken)
        {
            GetNumberOfMembersByAgeCategoryReportResponse response = await this.ReportingManager.GetNumberOfMembersByAgeCategoryReport(golfClubId, cancellationToken);

            return(this.Ok(response));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets the number of members by age category report.
        /// </summary>
        /// <param name="accessToken">The access token.</param>
        /// <param name="golfClubId">The golf club identifier.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <GetNumberOfMembersByAgeCategoryReportResponse> GetNumberOfMembersByAgeCategoryReport(String accessToken,
                                                                                                                Guid golfClubId,
                                                                                                                CancellationToken cancellationToken)
        {
            GetNumberOfMembersByAgeCategoryReportResponse response = null;
            String requestUri = $"{this.BaseAddress}/api/Reporting/GolfClub/{golfClubId}/numberofmembersbyagecategory";

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

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

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

                // call was successful so now deserialise the body to the response object
                response = JsonConvert.DeserializeObject <GetNumberOfMembersByAgeCategoryReportResponse>(content);
            }
            catch (Exception ex)
            {
                // An exception has occurred, add some additional information to the message
                Exception exception = new Exception($"Error getting number of members report by age category for Golf Club {golfClubId}.", ex);

                throw exception;
            }

            return(response);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts from.
        /// </summary>
        /// <param name="apiResponse">The API response.</param>
        /// <returns></returns>
        public ChartJsPieChartDataViewModel ConvertFrom(GetNumberOfMembersByAgeCategoryReportResponse apiResponse)
        {
            List <Color> chartColors = new List <Color>();

            chartColors = new List <Color>();
            chartColors.Add(Color.Aqua);
            chartColors.Add(Color.YellowGreen);
            chartColors.Add(Color.BlueViolet);
            chartColors.Add(Color.Coral);
            chartColors.Add(Color.CadetBlue);
            chartColors.Add(Color.Crimson);
            chartColors.Add(Color.DeepSkyBlue);

            ChartJsPieChartDataViewModel viewModel = new ChartJsPieChartDataViewModel();

            ChartJsPieChartDataDataSet dataSet = new ChartJsPieChartDataDataSet();

            Int32 counter = 0;

            foreach (MembersByAgeCategoryResponse membersByAgeCategoryResponse in apiResponse.MembersByAgeCategoryResponse)
            {
                Color backgroundColour = chartColors[counter];
                dataSet.BackgroundColor.Add(backgroundColour.ToHex());
                dataSet.Data.Add(membersByAgeCategoryResponse.NumberOfMembers);
                viewModel.Labels.Add(membersByAgeCategoryResponse.AgeCategory);
                counter++;
            }

            viewModel.Datasets.Add(dataSet);

            return(viewModel);
        }
        public async Task ReportingController_GET_GetNumberOfMembersByAgeCategoryReport_NumberOfMembersByAgeCategoryReportReturned()
        {
            // 1. Arrange
            HttpClient client = this.WebApplicationFactory.CreateClient();

            String uri = $"api/reports/golfclubs/{TestData.GolfClubId}/numberofmembersbyagecategory";

            client.DefaultRequestHeaders.Add("api-version", "2.0");
            // 2. Act
            HttpResponseMessage response = await client.GetAsync(uri, CancellationToken.None);

            // 3. Assert
            response.StatusCode.ShouldBe(HttpStatusCode.OK);

            String responseAsJson = await response.Content.ReadAsStringAsync();

            responseAsJson.ShouldNotBeNullOrEmpty();

            GetNumberOfMembersByAgeCategoryReportResponse responseObject = JsonConvert.DeserializeObject <GetNumberOfMembersByAgeCategoryReportResponse>(responseAsJson);

            responseObject.ShouldNotBeNull();
            responseObject.GolfClubId.ShouldBe(TestData.GolfClubId);
            responseObject.MembersByAgeCategoryResponse.ShouldNotBeNull();
            responseObject.MembersByAgeCategoryResponse.ShouldNotBeEmpty();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Gets the members by age category report.
        /// </summary>
        /// <param name="accessToken">The access token.</param>
        /// <param name="claimsIdentity">The claims identity.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <ChartJsPieChartDataViewModel> GetMembersByAgeCategoryReport(String accessToken,
                                                                                       ClaimsIdentity claimsIdentity,
                                                                                       CancellationToken cancellationToken)
        {
            Guid golfClubId = ApiClient.GetClaimValue <Guid>(claimsIdentity, "GolfClubId");

            GetNumberOfMembersByAgeCategoryReportResponse getNumberOfMembersByAgeCategoryReportResponse = await this.ReportingClient.GetNumberOfMembersByAgeCategoryReport(accessToken, golfClubId, cancellationToken);

            return(this.ModelFactory.ConvertFrom(getNumberOfMembersByAgeCategoryReportResponse));
        }
Exemplo n.º 6
0
        public async Task ReportingManager_GetNumberOfMembersByAgeCategoryReport_NoMembers_ReportDataReturned()
        {
            String databaseName = Guid.NewGuid().ToString("N");
            ManagementAPIReadModel        context         = this.GetContext(databaseName);
            Func <ManagementAPIReadModel> contextResolver = () => { return(context); };

            ReportingManager reportingManager = new ReportingManager(contextResolver);

            GetNumberOfMembersByAgeCategoryReportResponse reportData = await reportingManager.GetNumberOfMembersByAgeCategoryReport(GolfClubTestData.AggregateId, CancellationToken.None);

            reportData.GolfClubId.ShouldBe(GolfClubTestData.AggregateId);
            reportData.MembersByAgeCategoryResponse.ShouldBeEmpty();
        }
        public void ModelFactory_ConvertFrom_GetNumberOfMembersByAgeCategoryReportResponse_ConvertedSuccessfully()
        {
            ModelFactory factory = new ModelFactory();

            GetNumberOfMembersByAgeCategoryReportResponse apiResponse = ModelFactoryTestData.GetNumberOfMembersByAgeCategoryReportResponse();

            ChartJsPieChartDataViewModel viewModel = factory.ConvertFrom(apiResponse);

            viewModel.Labels.ShouldNotBeEmpty();
            viewModel.Datasets.ShouldNotBeEmpty();
            viewModel.Datasets.Count.ShouldBe(1);
            foreach (MembersByAgeCategoryResponse membersByAgeCategoryResponse in apiResponse.MembersByAgeCategoryResponse)
            {
                viewModel.Labels.ShouldContain(membersByAgeCategoryResponse.AgeCategory);
                viewModel.Datasets.First().Data.ShouldContain(membersByAgeCategoryResponse.NumberOfMembers);
            }
        }
        public async Task ReportingClient_GetNumberOfMembersByAgeCategoryReport_ReportDataReturned()
        {
            // 1. Arrange
            HttpClient            client          = this.WebApplicationFactory.AddGolfClubAdministrator().CreateClient();
            Func <String, String> resolver        = api => "http://localhost";
            IReportingClient      reportingClient = new ReportingClient(resolver, client);

            String token =
                "eyJhbGciOiJSUzI1NiIsImtpZCI6ImVhZDQyNGJjNjI5MzU0NGM4MGFmZThhMDk2MzEyNjU2IiwidHlwIjoiSldUIn0.eyJuYmYiOjE1NzAyODk3MDksImV4cCI6MTU3MDI5MzMwOSwiaXNzIjoiaHR0cDovLzE5Mi4xNjguMS4xMzI6NTAwMSIsImF1ZCI6WyJodHRwOi8vMTkyLjE2OC4xLjEzMjo1MDAxL3Jlc291cmNlcyIsIm1hbmFnZW1lbnRhcGkiLCJzZWN1cmlydHlzZXJ2aWNlYXBpIl0sImNsaWVudF9pZCI6ImdvbGZoYW5kaWNhcC50ZXN0ZGF0YWdlbmVyYXRvciIsInNjb3BlIjpbIm1hbmFnZW1lbnRhcGkiLCJzZWN1cmlydHlzZXJ2aWNlYXBpIl19.vLfs2bOMXshW93nw5TTOqd6NPGNYpcrhcom8yZoYc9WGSuYH48VqM5BdbodEukNNJmgbV9wUVgoj1uGztlFfHGFA_q6IQfd3xZln_LIxju6ZNZs8qUyRXDTGxu0dlfF8STLfBUq469SsY9eNi1hBYFyNxl963OfKqDSHAdeBg9yNlwnbky1Tnsxobu9W33fLcjH0KoutlwTFV51UFUEKCBk0w1zsjaDVZacETn74t56y0CvMS7ZSN2_yyunq4JvoUsh3xM5lQ-gl23eQyo6l4QE4wukCS7U_Zr2dg8-EF63VKiCH-ZD49M76TD9kIIge-XIgHqa2Xf3S-FpLxXfEqw";

            // 2. Act
            GetNumberOfMembersByAgeCategoryReportResponse getNumberOfMembersByAgeCategoryReport =
                await reportingClient.GetNumberOfMembersByAgeCategoryReport(token, TestData.PlayerId, CancellationToken.None);

            // 3. Assert
            getNumberOfMembersByAgeCategoryReport.GolfClubId.ShouldBe(TestData.GolfClubId);
            getNumberOfMembersByAgeCategoryReport.MembersByAgeCategoryResponse.ShouldNotBeEmpty();
        }
        /// <summary>
        /// Gets the number of members by age category report.
        /// </summary>
        /// <param name="golfClubId">The golf club identifier.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public async Task <GetNumberOfMembersByAgeCategoryReportResponse> GetNumberOfMembersByAgeCategoryReport(Guid golfClubId,
                                                                                                                CancellationToken cancellationToken)
        {
            // TODO: get the configured age categories from the golf club record
            Guard.ThrowIfInvalidGuid(golfClubId, nameof(golfClubId));

            GetNumberOfMembersByAgeCategoryReportResponse response = new GetNumberOfMembersByAgeCategoryReportResponse();

            using (ManagementAPIReadModel context = this.ReadModelResolver())
            {
                // Get the members grouped by age
                IQueryable <GolfClubMembershipReporting> members = context.GolfClubMembershipReporting.Where(g => g.GolfClubId == golfClubId).AsQueryable();
                DateTime zeroTime = new DateTime(1, 1, 1);

                // Group the members records by age
                var membersByAge = members.GroupBy(g => (zeroTime + DateTime.Now.Date.Subtract(g.DateOfBirth)).Year).Select(x => new
                {
                    Age   = x.Key,
                    Count = x.Count()
                });
                // Now build the members by age category to go into the response
                var membersByAgeCategory = membersByAge.Select(x => new
                {
                    AgeCategory = (x.Age < 17 ? "Junior" :
                                   x.Age >= 17 && x.Age < 19 ? "Juvenile" :
                                   x.Age >= 19 && x.Age < 21 ? "Youth" :
                                   x.Age >= 21 && x.Age < 25 ? "Young Adult" :
                                   x.Age >= 25 && x.Age < 65 ? "Adult" :
                                   x.Age >= 65 ? "Senior" : "Unknown"),
                    Count = x.Count
                });

                response.GolfClubId = golfClubId;
                response.MembersByAgeCategoryResponse = membersByAgeCategory.GroupBy(g => g.AgeCategory).Select(x => new MembersByAgeCategoryResponse
                {
                    AgeCategory     = x.Key,
                    NumberOfMembers = x.Sum(s => s.Count)
                }).ToList();
            }

            return(response);
        }
Exemplo n.º 10
0
        public async Task ReportingManager_GetNumberOfMembersByAgeCategoryReport_ReportDataReturned()
        {
            String databaseName            = Guid.NewGuid().ToString("N");
            ManagementAPIReadModel context = this.GetContext(databaseName);

            List <GolfClubMembershipReporting> reportingData = new List <GolfClubMembershipReporting>();

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-16),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 1,
                PlayerGender     = "M",
                PlayerName       = "Test Player 1"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-18),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 2,
                PlayerGender     = "M",
                PlayerName       = "Test Player 2"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-18),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 2,
                PlayerGender     = "M",
                PlayerName       = "Test Player 3"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-19),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 2,
                PlayerGender     = "M",
                PlayerName       = "Test Player 4"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-20),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 5"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-22),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 6"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-24),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 7"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-26),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 8"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-35),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 9"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-64),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 10"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-65),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 11"
            });

            reportingData.Add(new GolfClubMembershipReporting
            {
                DateJoined       = DateTime.Now,
                DateOfBirth      = DateTime.Now.AddYears(-70),
                PlayerId         = Guid.NewGuid(),
                GolfClubId       = GolfClubTestData.AggregateId,
                GolfClubName     = GolfClubTestData.Name,
                HandicapCategory = 3,
                PlayerGender     = "M",
                PlayerName       = "Test Player 12"
            });

            await context.GolfClubMembershipReporting.AddRangeAsync(reportingData, CancellationToken.None);

            await context.SaveChangesAsync(CancellationToken.None);

            Func <ManagementAPIReadModel> contextResolver = () => { return(context); };

            ReportingManager reportingManager = new ReportingManager(contextResolver);

            GetNumberOfMembersByAgeCategoryReportResponse reportData = await reportingManager.GetNumberOfMembersByAgeCategoryReport(GolfClubTestData.AggregateId, CancellationToken.None);

            reportData.GolfClubId.ShouldBe(GolfClubTestData.AggregateId);
            reportData.MembersByAgeCategoryResponse.ShouldNotBeEmpty();
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Junior").NumberOfMembers.ShouldBe(1);
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Juvenile").NumberOfMembers.ShouldBe(2);
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Youth").NumberOfMembers.ShouldBe(2);
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Young Adult").NumberOfMembers.ShouldBe(2);
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Adult").NumberOfMembers.ShouldBe(3);
            reportData.MembersByAgeCategoryResponse.Single(q => q.AgeCategory == "Senior").NumberOfMembers.ShouldBe(2);
        }