コード例 #1
0
        public bool HasMore(int userId, BookQueryOptions options)
        {
            var query = _context.Books
                        .Where(b => b.UserId == userId);

            if (options.Search != null)
            {
                query = query
                        .Where(x => x.Title.Contains(options.Search) || x.Author.Contains(options.Search));
            }

            if (options.Category.HasValue)
            {
                query = query
                        .Where(x => x.CategoryId == options.Category.Value);
            }

            if (options.Rating.HasValue)
            {
                query = query
                        .Where(x => x.RatingId == options.Rating.Value);
            }

            var entriesPerPage = options.EntriesPerPage ?? 10;

            return(query
                   .Skip((options.Page + 1) * entriesPerPage)
                   .Any());
        }
コード例 #2
0
        public IEnumerable <BookDto> GetUserBooks(int userId, BookQueryOptions options)
        {
            var query = _context.Books
                        .Where(b => b.UserId == userId);

            if (options.Search != null)
            {
                query = query
                        .Where(x => x.Title.Contains(options.Search) || x.Author.Contains(options.Search));
            }

            if (options.Category.HasValue)
            {
                query = query
                        .Where(x => x.CategoryId == options.Category.Value);
            }

            if (options.Rating.HasValue)
            {
                query = query
                        .Where(x => x.RatingId == options.Rating.Value);
            }

            var entriesPerPage = options.EntriesPerPage ?? 10;

            return(query
                   .OrderByDescending(x => x.FinishedOn)
                   .Skip(options.Page * entriesPerPage)
                   .Take(entriesPerPage)
                   .Select(b => ToBookDto(b)));
        }
コード例 #3
0
        public ActionResult <BooksDto> GetUserBooks(int userId, [FromBody] BookQueryOptions queryOptions)
        {
            if (!_userRepository.UserExists(userId))
            {
                return(BadRequest($"User with Id {userId} does not exist."));
            }

            return(new BooksDto
            {
                Books = _bookRepository.GetUserBooks(userId, queryOptions),
                HasMore = _bookRepository.HasMore(userId, queryOptions)
            });
        }
コード例 #4
0
        public ActionResult <BooksDto> GetCurrentUserBooks([FromBody] BookQueryOptions queryOptions)
        {
            var userId = _userHelper.GetUserId(HttpContext);

            if (!_userRepository.UserExists(userId))
            {
                return(BadRequest($"User with Id {userId} does not exist."));
            }

            return(new BooksDto
            {
                Books = _bookRepository.GetUserBooks(userId, queryOptions),
                HasMore = _bookRepository.HasMore(userId, queryOptions)
            });
        }