Ejemplo n.º 1
0
        public async Task <ActionResult <List <AuthorModel> > > GetAuthorList([FromQuery] AuthorsListModel rules)
        {
            if (apiService.CheckPropertyExist(rules.OrderBy) || apiService.CheckPropertyExist(rules.FilterBy))
            {
                return(BadRequest());
            }
            var result = await apiService.GetAuthorListAsync(rules);

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public ActionResult Index(AuthorsListSuccessMessage message = AuthorsListSuccessMessage.None)
        {
            var authors = new AuthorRepository().GetAll();
            var model   = new AuthorsListModel
            {
                Authors = authors.Select(author => new AuthorListItemModel
                {
                    Id       = author.Id,
                    FullName = author.FirstName + " " + author.LastName
                }).ToList()
            };

            if (message != AuthorsListSuccessMessage.None)
            {
                model.SuccessMessage = message.GetDescriptionAttributeValue();
            }

            return(View(model));
        }
Ejemplo n.º 3
0
        public async Task <List <AuthorModel> > GetAuthorListAsync(AuthorsListModel rules)
        {
            var filterBy = rules.FilterBy;
            var filterComparisonOperator = rules.FilterComparisonOperator;
            var filterValue = rules.FilterValue;
            var paging      = rules.Paging == 0 ? 10 : rules.Paging;
            var orderBy     = rules.OrderBy;
            var desc        = rules.Desc;

            var uniqueCacheKey = $"{filterBy}{filterComparisonOperator}{filterValue}{paging}{orderBy}{desc}";

            var authors = new List <AuthorModel>();

            var isCached = this.memoryCache.TryGetValue(uniqueCacheKey, out authors);

            if (isCached)
            {
                authors = await context.Author
                          .AsNoTracking()
                          .Where(filterBy, filterComparisonOperator, filterValue)
                          .OrderBy(orderBy, desc)
                          .Take(paging).Select(a => new AuthorModel
                {
                    AuthorId  = a.AuthorId,
                    BirthYear = a.BirthYear,
                    Name      = a.Name,
                    Books     = a.Books.Select(b => new BookModel
                    {
                        BookId          = b.BookId,
                        Genre           = b.BookId,
                        PublicationYear = b.PublicationYear,
                        Title           = b.Title
                    }).ToList()
                }).ToListAsync();

                if (authors.Count > 0)
                {
                    this.memoryCache.Set(uniqueCacheKey, authors, TimeSpan.FromSeconds(600));
                }
            }
            return(authors);
        }