public async Task <IActionResult> GetSleepListPage([FromQuery] int pageSize = 8, [FromQuery] int pageIndex = 1, [FromQuery] int progenyId = Constants.DefaultChildId, [FromQuery] int accessLevel = 5, [FromQuery] int sortBy = 1) { // Check if user should be allowed access. string userEmail = User.GetEmail() ?? Constants.DefaultUserEmail; UserAccess userAccess = await _dataService.GetProgenyUserAccessForUser(progenyId, userEmail); if (userAccess == null && progenyId != Constants.DefaultChildId) { return(Unauthorized()); } if (pageIndex < 1) { pageIndex = 1; } List <Sleep> allItems = await _dataService.GetSleepList(progenyId); allItems = allItems.OrderBy(s => s.SleepStart).ToList(); if (sortBy == 1) { allItems.Reverse(); } int sleepCounter = 1; int slpCount = allItems.Count; foreach (Sleep slp in allItems) { if (sortBy == 1) { slp.SleepNumber = slpCount - sleepCounter + 1; } else { slp.SleepNumber = sleepCounter; } sleepCounter++; } var itemsOnPage = allItems .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .ToList(); SleepListPage model = new SleepListPage(); model.SleepList = itemsOnPage; model.TotalPages = (int)Math.Ceiling(allItems.Count / (double)pageSize); model.PageNumber = pageIndex; model.SortBy = sortBy; return(Ok(model)); }
public async Task <IActionResult> GetSleepListPage([FromQuery] int pageSize = 8, [FromQuery] int pageIndex = 1, [FromQuery] int progenyId = Constants.DefaultChildId, [FromQuery] int accessLevel = 5, [FromQuery] int sortBy = 1) { if (pageIndex < 1) { pageIndex = 1; } List <Sleep> allItems = await _dataService.GetSleepList(Constants.DefaultChildId); allItems = allItems.Where(s => s.AccessLevel == 5).OrderBy(s => s.SleepStart).ToList(); if (sortBy == 1) { allItems.Reverse(); } int sleepCounter = 1; int slpCount = allItems.Count; foreach (Sleep slp in allItems) { if (sortBy == 1) { slp.SleepNumber = slpCount - sleepCounter + 1; } else { slp.SleepNumber = sleepCounter; } sleepCounter++; } var itemsOnPage = allItems .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .ToList(); SleepListPage model = new SleepListPage(); model.SleepList = itemsOnPage; model.TotalPages = (int)Math.Ceiling(allItems.Count / (double)pageSize); model.PageNumber = pageIndex; model.SortBy = sortBy; return(Ok(model)); }
private async Task UpdateSleep() { _viewModel.IsBusy = true; if (_viewModel.PageNumber < 1) { _viewModel.PageNumber = 1; } _viewModel.ItemsPerPage = Preferences.Get(Constants.SleepPerPage, 20); SleepListPage sleepListPage = await ProgenyService.GetSleepListPage(_viewModel.PageNumber, _viewModel.ItemsPerPage, _viewModel.ViewChild, _viewModel.UserAccessLevel, _viewModel.UserInfo.Timezone, 1); if (sleepListPage.SleepList != null) { _viewModel.SleepItems.ReplaceRange(sleepListPage.SleepList); _viewModel.PageNumber = sleepListPage.PageNumber; _viewModel.PageCount = sleepListPage.TotalPages; SleepListView.ScrollTo(0); } _viewModel.IsBusy = false; }