public ActionResult <PaginatedResults <ExerciseDTO> > Get(int firstRecord, short pageSize, string nameContains = null, string hasTargetAreas = null) { try { var filter = BuildExerciseFilter(nameContains, hasTargetAreas); int totalCount = _exerciseService.GetTotalCount(filter); //TODO: Modify to get total count by filter //Blows up after upgrading to EF Core 3.1 from 2.2! //More info at https://stackoverflow.com/questions/59677609/problem-with-ef-core-after-migrating-from-2-2-to-3-1 //Had to add .ToList() to the call below. var exercises = _exerciseService .Get(firstRecord, pageSize, filter) .OrderBy(x => x.Name); var results = exercises.Select((exercise) => { return(new ExerciseDTO( exercise.Id, exercise.Name, string.Join(", ", exercise.ExerciseTargetAreaLinks.Select(x => x.TargetArea.Name)))); }); var result = new PaginatedResults <ExerciseDTO>(results, totalCount); return(Ok(result)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public ActionResult <PaginatedResults <WorkoutDTO> > Get(int firstRecord, short pageSize, bool activeOnly, string nameContains = null) { try { //TODO: Consolidate this code somewhere! var userId = GetUserID(); var filter = BuildWorkoutFilter(userId, activeOnly, nameContains); int totalCount = _workoutService.GetTotalCount(filter); //Blows up after upgrading to EF Core 3.1 from 2.2! //More info at https://stackoverflow.com/questions/59677609/problem-with-ef-core-after-migrating-from-2-2-to-3-1 //Had to add .ToList() to the call below. var workouts = _workoutService .Get(firstRecord, pageSize, filter) .OrderBy(x => x.Name); var results = workouts.Select((workout) => { return(_workoutDTOAdapter.AdaptFromWorkout(workout)); }); var result = new PaginatedResults <WorkoutDTO>(results, totalCount); return(Ok(result)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public void Records_FirstPage_OneToPageSize() { var pagination = new Pagination(3, 1); var subject = new PaginatedResults <int>(new[] { 10, 20, 30 }, 7, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(1)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(3)); }
public void Records_CompletePage_FirstRecordPlusPageSize() { var pagination = new Pagination(3, 2); var subject = new PaginatedResults <int>(new[] { 40, 50, 60 }, 7, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(4)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(6)); }
public void ctor_EmptyCollection_NoException() { var pagination = new Pagination(40, 1); var subject = new PaginatedResults <int>(new int[0], 0, pagination); Assert.That(subject.PageOfResults, Is.Empty); Assert.That(subject.CurrentPage, Is.EqualTo(pagination.PageNumber)); Assert.That(subject.TotalResults, Is.EqualTo(0)); Assert.That(subject.TotalResults, Is.EqualTo(0)); }
public void Records_LessThanOnePage_OneToLength() { var pagination = new Pagination(3, 1); var subject = new PaginatedResults <int>(new[] { 10 }, 1, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(1)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(1)); subject = new PaginatedResults <int>(new[] { 10, 20 }, 2, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(1)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(2)); }
public void Records_IncompletePage_FirstRecordPlusResultsLength() { var pagination = new Pagination(3, 3); var subject = new PaginatedResults <int>(new[] { 70 }, 7, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(7)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(7)); subject = new PaginatedResults <int>(new[] { 70, 80 }, 8, pagination); Assert.That(subject.RecordNumbers.LowerBound, Is.EqualTo(7)); Assert.That(subject.RecordNumbers.UpperBound, Is.EqualTo(8)); }
public virtual ActionResult <IList <TDatabaseType> > Get(int?page = 0, int?pageSize = 10) { var data = _service.Get(page: page.Value, pageSize: pageSize.Value); var dtos = ConvertToDtoType(data.Results); var retval = new PaginatedResults <TDtoType>(); retval.Results = dtos; retval.Returned.Count = data.Returned.Count; retval.Returned.FirstRecord = data.Returned.FirstRecord; retval.Returned.LastRecord = data.Returned.LastRecord; retval.Returned.Page = data.Returned.Page; retval.Totals.Records = data.Totals.Records; retval.Totals.Pages = data.Totals.Pages; return(Ok(retval)); }
public void Project_MapsToNewForm_KeepsSameMeta() { var pagination = new Pagination(3, 1); int totalResults = 5; var subject = new PaginatedResults <int>(new[] { 10, 20 }, totalResults, pagination); PaginatedResults <string> projection = subject.Project(x => x.ToString()); Assert.That(projection.Pagination, Is.EqualTo(subject.Pagination)); Assert.That(projection.Pagination.PageNumber, Is.EqualTo(subject.Pagination.PageNumber)); Assert.That(projection.Pagination.PageSize, Is.EqualTo(subject.Pagination.PageSize)); Assert.That(projection.TotalResults, Is.EqualTo(subject.TotalResults).And.EqualTo(5)); Assert.That(projection.PageOfResults, Is.EqualTo(new[] { "10", "20" })); }
public PaginatedResults <D> GetPaginatedResults <E, D>(IEnumerable <E> results, PaginationOptions options) { var count = results.Count(); var pageSize = options?.Count ?? count; if (pageSize == 0) { pageSize = count; } var highestPage = (int)Math.Ceiling((decimal)count / (pageSize != 0 ? Math.Abs(pageSize) : 1)); var pageNum = options?.PageNumber ?? 1; if (pageNum > highestPage) { pageNum = highestPage; } else if (pageNum < 1) { pageNum = 1; } var paginatedResults = new PaginatedResults <D> { Total = count, CurrentPage = pageNum, LastPage = highestPage, itemsPerPage = pageSize, Results = _mapper.Map <List <D> >(GetResults(results, pageSize, pageNum, highestPage).ToList()), }; if (pageNum + 1 <= highestPage) { paginatedResults.NextPage = pageNum + 1; } if (pageNum - 1 >= 1) { paginatedResults.PreviousPage = pageNum - 1; } return(paginatedResults); }
public ActionResult <PaginatedResults <ExecutedWorkoutDTO> > GetPlanned(int firstRecord, short pageSize, bool newestFirst = true) { try { var userId = GetUserID(); var filter = BuildExecutedWorkoutFilter( userId, null, null, true); //last param of true is "planned only" int totalCount = _executedWorkoutService.GetTotalCount(filter); var executedWorkouts = _executedWorkoutService .GetFilteredSubset(firstRecord, pageSize, filter, newestFirst) .ToList(); var results = executedWorkouts.Select((executedWorkout) => { return(new ExecutedWorkoutDTO( executedWorkout.Id, executedWorkout.Workout.Name, executedWorkout.WorkoutId, executedWorkout.StartDateTime, executedWorkout.EndDateTime, executedWorkout.CreatedDateTime, executedWorkout.Journal)); }); var result = new PaginatedResults <ExecutedWorkoutDTO>(results, totalCount); return(Ok(result)); } catch (BadHttpRequestException ex) { return(BadRequest(ex)); } catch (Exception ex) { return(StatusCode(500, ex.Message)); } }
public async Task <IActionResult> ItemsAsync([FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0, string ids = null) { if (!string.IsNullOrEmpty(ids)) { var arrayItems = await GetItemsByIdsAsync(ids); if (!arrayItems.Any()) { return(BadRequest("ids value invalid. Must be comma-separated list of numbers")); } return(Ok(arrayItems)); } var totalItems = await _tripQuery.CountAsync(); var items = await _tripQuery.GetPagedAsync(pageSize, pageIndex); var results = new PaginatedResults <TripViewModel>(pageIndex, pageSize, totalItems, items); return(Ok(results)); }