Ejemplo n.º 1
0
        public async Task<ICollection<CommentDTO>> GetCommentsByCriteriaAsync(CommentCriteriaInputModel model)
        {
            if (!model.CommentIds.Any() &&
                !model.PostIds.Any() &&
                string.IsNullOrWhiteSpace(model.Text) &&
                model.RateEquals == null &&
                model.RateLower == null &&
                model.RateGreater == null &&
                !model.UserIds.Any() &&
                model.DateCreated == null &&
                model.BlogId == null
                )
            {
                throw new ArgumentNullException("Critera is empty");
            }

            var comments = this.repository.All();

            if (model.CommentIds.Any())
            {
                comments = comments
                    .Where(c => model.CommentIds.Contains(c.Id));
            }

            if (model.PostIds.Any())
            {
                comments = comments
                    .Where(c => model.PostIds.Contains(c.PostId));
            }

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

            if (model.RateEquals != null)
            {
                comments = comments
                    .Where(c => c.Rate == model.RateEquals);
            }

            if (model.RateGreater != null)
            {
                comments = comments
                    .Where(c => c.Rate > model.RateGreater);
            }

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

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

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

            if (model.BlogId != null)
            {
                comments = comments
                    .Where(c => c.Post.BlogId == model.BlogId);
            }
            return await comments.Select(CommentMapper.SelectCommentDtoFromComment).ToListAsync();
        }
 public async Task <ICollection <CommentDTO> > GetCommentsByCriteria([FromBody] CommentCriteriaInputModel comment)
 {
     return(await service.GetCommentsByCriteriaAsync(comment));
 }