Exemplo n.º 1
0
        public async Task <IActionResult> GetMeasurementsListPage([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 <Measurement> allItems = await _dataService.GetMeasurementsList(progenyId);

            allItems = allItems.OrderBy(m => m.Date).ToList();

            if (sortBy == 1)
            {
                allItems.Reverse();
            }

            int measurementsCounter = 1;
            int measurementsCount   = allItems.Count;

            foreach (Measurement mes in allItems)
            {
                if (sortBy == 1)
                {
                    mes.MeasurementNumber = measurementsCount - measurementsCounter + 1;
                }
                else
                {
                    mes.MeasurementNumber = measurementsCounter;
                }

                measurementsCounter++;
            }

            var itemsOnPage = allItems
                              .Skip(pageSize * (pageIndex - 1))
                              .Take(pageSize)
                              .ToList();

            MeasurementsListPage model = new MeasurementsListPage();

            model.MeasurementsList = itemsOnPage;
            model.TotalPages       = (int)Math.Ceiling(allItems.Count / (double)pageSize);
            model.PageNumber       = pageIndex;
            model.SortBy           = sortBy;

            return(Ok(model));
        }
        private async Task UpdateMeasurements()
        {
            _viewModel.IsBusy = true;
            if (_viewModel.PageNumber < 1)
            {
                _viewModel.PageNumber = 1;
            }

            _viewModel.ItemsPerPage = Preferences.Get(Constants.MeasurementsPerPage, 20);
            MeasurementsListPage measurementsListPage = await ProgenyService.GetMeasurementsListPage(_viewModel.PageNumber, _viewModel.ItemsPerPage, _viewModel.ViewChild, _viewModel.UserAccessLevel, 1);

            if (measurementsListPage.MeasurementsList != null)
            {
                measurementsListPage.MeasurementsList =
                    measurementsListPage.MeasurementsList.OrderByDescending(m => m.Date).ToList();
                _viewModel.MeasurementItems.ReplaceRange(measurementsListPage.MeasurementsList);
                _viewModel.PageNumber = measurementsListPage.PageNumber;
                _viewModel.PageCount  = measurementsListPage.TotalPages;
                MeasurementsListView.ScrollTo(0);
            }
            _viewModel.IsBusy = false;
        }