Exemple #1
0
        public PaginationList <Word> GetWords(WordUrlQuery query)
        {
            var words = _context.Words
                        .AsQueryable();
            var list = new PaginationList <Word>();

            if (query.Date.HasValue)
            {
                words = words
                        .Where(w => w.CreatedAt > query.Date.Value || w.ModifiedAt > query.Date.Value);
            }

            if (query.Page.HasValue && query.Quantity.HasValue)
            {
                var totalQuantity = words.Count();
                words = words
                        .Skip((query.Page.Value - 1) * query.Quantity.Value)
                        .Take(query.Quantity.Value);

                var pagination = new Pagination()
                {
                    Page       = query.Page.Value,
                    Quantity   = query.Quantity.Value,
                    TotalItems = totalQuantity,
                    TotalPages = (int)Math.Ceiling((double)totalQuantity / query.Quantity.Value)
                };

                list.Pagination = pagination;
            }

            list.Results.AddRange(words.ToList());

            return(list);
        }
Exemple #2
0
        //MÉTODOS PRIVADOS
        private PaginationList <DTOWord> CreateLinkListDTOWord(WordUrlQuery query, PaginationList <Word> item)
        {
            var list = _mapper.Map <PaginationList <Word>, PaginationList <DTOWord> >(item);

            foreach (var word in list.Results)
            {
                word.Links = new List <DTOLink>();
                word.Links.Add(new DTOLink("self", Url.Link("FindWord", new { id = word.Id }), "GET"));
            }

            list.Links.Add(new DTOLink("self", Url.Link("FindAllWords", query), "GET"));
            if (item.Pagination != null)
            {
                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(item.Pagination));

                if (query.numPage.Value + 1 <= item.Pagination.TotalPages)
                {
                    var queryString = new WordUrlQuery()
                    {
                        numPage = query.numPage + 1, regPerPage = query.regPerPage, date = query.date
                    };
                    list.Links.Add(new DTOLink("next", Url.Link("FindAllWords", queryString), "GET"));
                }
                if (query.numPage.Value - 1 > 0)
                {
                    var queryString = new WordUrlQuery()
                    {
                        numPage = query.numPage - 1, regPerPage = query.regPerPage, date = query.date
                    };
                    list.Links.Add(new DTOLink("prev", Url.Link("FindAllWords", queryString), "GET"));
                }
            }

            return(list);
        }
        public PaginationList <Word> GetWords(WordUrlQuery query)
        {
            var list = new PaginationList <Word>();

            var words = _db.Words.AsQueryable();

            if (query.Date.HasValue)
            {
                words = words.Where(p => p.CreatedAt > query.Date);
            }

            if (query.Page.HasValue && query.PageSize.HasValue)
            {
                var total = words.Count();
                words = words.Skip((int)((query.Page - 1) * query.PageSize)).Take((int)query.PageSize);

                var pagination = new Pagination();
                pagination.PageNumber = query.Page.Value;
                pagination.PageSize   = query.PageSize.Value;
                pagination.Total      = total;
                pagination.TotalPage  = (int)Math.Ceiling((decimal)(total / query.PageSize.Value));

                list.Pagination = pagination;
            }

            list.Results.AddRange(words.ToList());

            return(list);
        }
        public PaginationList <Word> FindAllWordsAsync(WordUrlQuery query)
        {
            var list = new PaginationList <Word>();
            var item = _context.Words.AsNoTracking().AsQueryable();

            if (query.date.HasValue)
            {
                item = item.Where(i => i.CreationDate >= query.date.Value && i.UpdateDate >= query.date.Value);
            }

            if (query.numPage.HasValue)
            {
                var qtdRegisters = _context.Words.Count();
                item = item.Skip((query.numPage.Value - 1) * query.regPerPage.Value).Take(query.regPerPage.Value);

                var pagination = new Pagination();
                pagination.PageNumber      = query.numPage.Value;
                pagination.RegisterPerPage = query.regPerPage.Value;
                pagination.TotalRegisters  = qtdRegisters;
                pagination.TotalPages      = (int)Math.Ceiling((double)pagination.TotalRegisters / pagination.RegisterPerPage);

                list.Pagination = pagination;
            }

            list.Results.AddRange(item.ToList());
            return(list);
        }
Exemple #5
0
        public IActionResult FindAllWords([FromQuery] WordUrlQuery query)
        {
            var item = _repository.FindAllWordsAsync(query);

            if (item.Results.Count == 0)
            {
                return(NotFound());
            }

            PaginationList <DTOWord> list = CreateLinkListDTOWord(query, item);

            return(Ok(list));
        }
Exemple #6
0
        public ActionResult GetWords([FromQuery] WordUrlQuery query)
        {
            var words = _wordRepository.GetWords(query);

            if (words.Results.Count == 0)
            {
                return(NotFound());
            }

            PaginationList <WordDTO> list = CreateLinks(query, words);

            return(Ok(list));
        }
Exemple #7
0
        public ActionResult GetWords(WordUrlQuery query)
        {
            var wordsList = _wordReposirory.GetWords(query);

            if (wordsList.Results.Count == 0)
            {
                return(NotFound());
            }

            var wordDTOList = _mapper.Map <PaginationList <Word>, PaginationList <WordDTO> >(wordsList);

            foreach (var word in wordDTOList.Results)
            {
                word.Links.Add(new LinkDTO("self", Url.Link("Get", new { id = word.Id }), "GET"));
            }

            wordDTOList.Links.Add(new LinkDTO("self", Url.Link("GetWords", query), "GET"));

            if (wordsList.Pagination != null)
            {
                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(wordsList.Pagination));

                if (query.Page + 1 <= wordsList.Pagination.TotalPage)
                {
                    var queryNext = new WordUrlQuery()
                    {
                        PageSize = query.PageSize, Page = query.Page + 1, Date = query.Date
                    };
                    wordDTOList.Links.Add(new LinkDTO("next", Url.Link("GetWords", queryNext), "GET"));
                }

                if (query.Page - 1 > 0)
                {
                    var queryPrev = new WordUrlQuery()
                    {
                        PageSize = query.PageSize, Page = query.Page - 1, Date = query.Date
                    };
                    wordDTOList.Links.Add(new LinkDTO("prev", Url.Link("GetWords", queryPrev), "GET"));
                }
            }

            return(Ok(wordDTOList));
        }
Exemple #8
0
        private PaginationList <WordDTO> CreateLinks(WordUrlQuery query, PaginationList <Word> words)
        {
            var list = _mapper.Map <PaginationList <Word>, PaginationList <WordDTO> >(words);

            foreach (var word in list.Results)
            {
                word.Links = new List <LinkDTO>();
                word.Links.Add(new LinkDTO("self", Url.Link("GetWord", new { id = word.Id }), "GET"));
            }

            list.Links.Add(new LinkDTO("self", Url.Link("GetWords", query), "GET"));

            if (words.Pagination != null)
            {
                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(words.Pagination));

                if (query.Page + 1 <= words.Pagination.TotalPages)
                {
                    var queryString = new WordUrlQuery()
                    {
                        Page = query.Page + 1, Quantity = query.Quantity, Date = query.Date
                    };
                    list.Links.Add(new LinkDTO("next", Url.Link("GetWords", queryString), "GET"));
                }
                if (query.Page - 1 > 0)
                {
                    var queryString = new WordUrlQuery()
                    {
                        Page = query.Page - 1, Quantity = query.Quantity, Date = query.Date
                    };
                    list.Links.Add(new LinkDTO("prev", Url.Link("GetWords", queryString), "GET"));
                }
            }

            return(list);
        }