예제 #1
0
        /// <summary>
        /// returns a list of all Passage Objets
        /// </summary>
        public async Task <SystemResponse <AllSermonsSummaryResponse> > GetAllSermons()
        {
            var getAllSermonsResponse = await _sermonsRepository.GetAllSermons();

            // we need to convert everything to the right response pattern
            var sortedSeries = getAllSermonsResponse.Sermons.OrderByDescending(i => i.StartDate);

            // for each one add only the properties we want to the list
            var responseList = new List <SermonSeriesSummary>();

            foreach (var series in sortedSeries)
            {
                var elemToAdd = new SermonSeriesSummary
                {
                    ArtUrl    = series.ArtUrl,
                    Id        = series.Id,
                    StartDate = series.StartDate.Value,
                    Title     = series.Name
                };

                responseList.Add(elemToAdd);
            }

            var response = new AllSermonsSummaryResponse
            {
                Summaries = responseList
            };

            return(new SystemResponse <AllSermonsSummaryResponse>(response, "Success!"));
        }
        /// <summary>
        /// Recieve Sermon Series in a paged format
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public async Task <SystemResponse <SermonsSummaryPagedResponse> > GetPagedSermons(int pageNumber)
        {
            // determine which number of sermon series to request & which ones to return
            var responseCount = 10;

            if (pageNumber < 3)
            {
                responseCount = 5;
            }

            // what indexes should we use as the beginning? However, if larger than this we'll need to do more cheeckyness
            var beginningCount = pageNumber == 2 ? 5 : 0;

            if (pageNumber >= 3)
            {
                beginningCount = (pageNumber * 10) - 20; // we subtract 20 because the first 10 pages we only return 5
            }

            var totalPageNumber = 1;

            var documents = await _sermonsCollection.Find(_ => true)
                            .SortByDescending(i => i.StartDate)
                            .Skip(beginningCount)
                            .Limit(responseCount)
                            .ToListAsync();

            // use this response in the total Record count
            var totalRecordDelegate = GetAllSermons();

            // get the count from the async task above
            var totalRecord = await totalRecordDelegate;

            // converting this to an array first will allow us to not have to enumerate the collection multiple times
            // ToArray() just converts the DataType via reflection through a Buffer -> O(n) BUT length reads are O(1) after this
            var totalRecordCount = totalRecord.Sermons.ToArray().Length;

            // since we know how many there are, in this method we can use that to indicate the total Pages in this method
            // Remember that pages 1 & 2 return a response of only 5
            // take out the first 2 sets of 5, and as long as the number isn't neg before we get there then we have 2 pages
            var remainingRecords = totalRecordCount - 10;

            if (remainingRecords <= 0)
            {
                totalPageNumber = 2;
            }

            // we already know that there are only 2 pages, so if there's only those 2 then continue,
            // otherwise calculate the leftovers
            if (totalRecordCount > 10)
            {
                // we know that there are at least 2 pages if we are here
                totalPageNumber = 2;

                // Now we divide the total count by the calc and if we have a remainder then we round up to the next highest whole number
                double remainingPages = remainingRecords / 10.0;

                // this technichally is similiar to modulo, except we want the remainder and the integer,
                // so that we can add whole pages that are included as the int and any leftovers that are not quite a full page yet in the remainder
                long   intPart        = (long)remainingPages;
                double fractionalPart = remainingPages - intPart;

                var value = (int)intPart;
                if (value > 0)
                {
                    // Append whatever number of records are left based on our paging scheme
                    totalPageNumber += value;
                }

                // We don't have another page
                if (fractionalPart <= 1 && fractionalPart > 0)
                {
                    totalPageNumber++;
                }
            }

            if (pageNumber > totalPageNumber)
            {
                return(new SystemResponse <SermonsSummaryPagedResponse>(true, string.Format(SystemMessages.InvalidPagingNumber, pageNumber, totalPageNumber)));
            }

            // construct the list of summaries
            var summariesList = new List <SermonSeriesSummary>();

            foreach (var series in documents)
            {
                var summary = new SermonSeriesSummary
                {
                    // Use the thumbnail URL for these summaries,
                    // because we will be loading many of them at once
                    ArtUrl    = series.Thumbnail,
                    Id        = series.Id,
                    StartDate = series.StartDate.Value,
                    Title     = series.Name
                };
                summariesList.Add(summary);
            }

            // construct the final response
            var response = new SermonsSummaryPagedResponse
            {
                Summaries  = summariesList,
                PagingInfo = new PageInfo
                {
                    PageNumber       = pageNumber,
                    TotalPageCount   = totalPageNumber,
                    TotalRecordCount = totalRecordCount
                }
            };

            return(new SystemResponse <SermonsSummaryPagedResponse>(response, "Success!"));
        }