예제 #1
0
        public async Task <IActionResult> GetVocabularyListPage([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 <VocabularyItem> allItems = await _dataService.GetVocabularyList(progenyId);

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

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

            int vocabularyCounter = 1;
            int vocabularyCount   = allItems.Count;

            foreach (VocabularyItem word in allItems)
            {
                if (sortBy == 1)
                {
                    word.VocabularyItemNumber = vocabularyCount - vocabularyCounter + 1;
                }
                else
                {
                    word.VocabularyItemNumber = vocabularyCounter;
                }

                vocabularyCounter++;
            }

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

            VocabularyListPage model = new VocabularyListPage();

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

            return(Ok(model));
        }
예제 #2
0
        private async Task UpdateVocabulary()
        {
            _viewModel.IsBusy = true;
            if (_viewModel.PageNumber < 1)
            {
                _viewModel.PageNumber = 1;
            }

            _viewModel.ItemsPerPage = Preferences.Get(Constants.VocabularyPerPage, 20);
            VocabularyListPage vocabularyListPage = await ProgenyService.GetVocabularyListPage(_viewModel.PageNumber, _viewModel.ItemsPerPage, _viewModel.ViewChild, _viewModel.UserAccessLevel, 1);

            if (vocabularyListPage.VocabularyList != null)
            {
                vocabularyListPage.VocabularyList =
                    vocabularyListPage.VocabularyList.OrderByDescending(v => v.Date).ToList();
                _viewModel.VocabularyItems.ReplaceRange(vocabularyListPage.VocabularyList);
                _viewModel.PageNumber = vocabularyListPage.PageNumber;
                _viewModel.PageCount  = vocabularyListPage.TotalPages;
                VocabularyListView.ScrollTo(0);
            }
            _viewModel.IsBusy = false;
        }