コード例 #1
0
        public async Task <IActionResult> GetAllAsync(double?rating, int?votes)
        {
            try
            {
                ImdbFilterRequest filterRequest = new ImdbFilterRequest()
                {
                    Rating = rating, Votes = votes
                };
                var imdbs = await FilterImdbsRequest(filterRequest)
                            .Select(m => new Imdb()
                {
                    Id      = m.Id,
                    Votes   = m.Votes,
                    Rating  = m.Rating,
                    MovieId = m.MovieId
                })
                            .OrderBy(m => m.Votes)
                            .ThenBy(c => c.Rating)
                            .ToListAsync();

                return(Ok(imdbs));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
コード例 #2
0
        private IMongoQueryable <Imdb> FilterImdbsRequest(ImdbFilterRequest filterRequest)
        {
            var filter = _dbContext.Imdbs.AsQueryable();

            if (filterRequest.Rating.HasValue)
            {
                filter.Where(i => i.Rating >= filterRequest.Rating.Value);
            }

            if (filterRequest.Votes.HasValue)
            {
                filter.Where(i => i.Votes <= filterRequest.Votes.Value);
            }

            return(filter);
        }