Esempio n. 1
0
        public async Task <ICollection <PostDTO> > GetPostsByCriteria(PostsCriteriaInputModel model)
        {
            if (!model.Ids.Any() &&
                string.IsNullOrWhiteSpace(model.Title) &&
                string.IsNullOrWhiteSpace(model.Text) &&
                model.RateEquals == null &&
                model.RateGreater == null &&
                model.RateLower == null &&
                !model.UserIds.Any() &&
                model.DateCreated == null &&
                model.BlogId == null
                )
            {
                throw new ArgumentNullException("No criteria typed");
            }
            var posts = this.repository.All();

            if (model.Ids.Any())
            {
                posts = posts
                        .Where(p => model.Ids.Contains(p.Id));
            }

            if (!string.IsNullOrWhiteSpace(model.Title))
            {
                posts = posts
                        .Where(p => p.Title.Contains(model.Title));
            }

            if (!string.IsNullOrWhiteSpace(model.Text))
            {
                posts = posts
                        .Where(p => p.Text.Contains(model.Text));
            }

            if (model.RateEquals != null)
            {
                posts = posts
                        .Where(p => p.Rate == model.RateEquals);
            }

            if (model.RateLower != null)
            {
                posts = posts
                        .Where(p => p.Rate < model.RateLower);
            }

            if (model.RateGreater != null)
            {
                posts = posts
                        .Where(p => p.Rate > model.RateGreater);
            }

            if (model.UserIds.Any())
            {
                posts = posts
                        .Where(p => model.UserIds.Contains((long)p.UserCreatorId));
            }

            if (model.DateCreated != null)
            {
                posts = posts
                        .Where(p => p.DataCreated.Date == model.DateCreated.Value.Date);
            }

            if (model.BlogId != null)
            {
                posts = posts
                        .Where(p => p.BlogId == model.BlogId);
            }
            return(await posts.Select(PostMapper.SelectPostDtoFromPost).ToListAsync());
        }
 public async Task <ICollection <PostDTO> > GetBlogsByCriteria([FromBody] PostsCriteriaInputModel post)
 {
     return(await service.GetPostsByCriteria(post));
 }