Пример #1
0
        public IEnumerable <PostInfoData> Search(PostCriteria criteria)
        {
            IEnumerable <PostInfoData> result = null;

            RepositoryExceptionWrapper.Wrap(GetType(), () =>
            {
                IQuery query = CurrentSession.GetNamedQuery("GetPostInfoList");
                if (criteria.TopicId != null)
                {
                    query.SetParameter("TopicId", criteria.TopicId);
                }
                else
                {
                    query.SetString("TopicId", null);
                }
                if (criteria.UserId != null)
                {
                    query.SetParameter("UserId", criteria.UserId);
                }
                else
                {
                    query.SetString("UserId", null);
                }

                result = query.List <PostInfoData>();
            });

            return(result);
        }
Пример #2
0
        private static Expression<Func<Post, bool>> ExpressionBuilder(PostCriteria criteria)
        {
            // Start with our base expression - we want all posts
            //Expression<Func<Post, bool>> criteriaExpression = PredicateBuilder.True<Post>();
            var criteriaExpression = PredicateBuilder.True<Post>();

            if(criteria == null)
            {
                return criteriaExpression;
            }

            // If a title has been specified in the filter, add an expression to include
            // any Post where the Title contains the value specified in the filter
            if(!String.IsNullOrEmpty(criteria.Title))
            {
                criteriaExpression = criteriaExpression.And(post => post.Title.Contains(criteria.Title));
            }

            // If a 'Before' date is specified, add an expression to include posts which have
            // a PublishedDate less than the date specified
            if(criteria.BeforeDate.HasValue)
            {
                criteriaExpression = criteriaExpression.And(post => post.PublishDate.CompareTo(criteria.BeforeDate.Value) < 0);
            }

            // If an 'After' date is specified, add an expression to include posts which have
            // a PublishedDate greater than the date specified
            if(criteria.AfterDate.HasValue)
            {
                criteriaExpression = criteriaExpression.And(post => post.PublishDate.CompareTo(criteria.AfterDate.Value) >= 0);
            }

            return criteriaExpression;
        }
Пример #3
0
        public void BetweenDatesUsingSQL()
        {
            IPostRepository repo = new AlternatePostRepository(new BlogContext());

            var criteria = new PostCriteria(new DateTime(2012, 1, 3), new DateTime(2012, 1, 7), String.Empty);

            var posts = repo.Retrieve(criteria, Order<Post>.ByDescending(post => post.PublishDate));

            posts.Count().Should().Be(4);
        }
Пример #4
0
        public void BetweenDatesUsingSQL()
        {
            IPostRepository repo = new AlternatePostRepository(new BlogContext());

            var criteria = new PostCriteria(new DateTime(2012, 1, 3), new DateTime(2012, 1, 7), String.Empty);

            var posts = repo.Retrieve(criteria, Order <Post> .ByDescending(post => post.PublishDate));

            posts.Count().Should().Be(4);
        }
Пример #5
0
        public IQueryable <Posts> SearchPost(PostCriteria criteria, ref int totalRecords)
        {
            var query = postRepository.Get
                        .Include(t => t.CategoryDetail)
                        .Include(t => t.Region).AsQueryable();

            // .Where(t=>(string.IsNullOrEmpty(criteria.SearchText))

            totalRecords = query.Count();
            return(query);
        }
Пример #6
0
        public void ByTitle()
        {
            IPostRepository repo = new PostRepository(new BlogContext());

            var criteria = new PostCriteria(null, null, "9");

            var posts = repo.Retrieve(criteria, Order<Post>.ByDescending(post => post.PublishDate));

            posts.Count().Should().Be(1);
            posts.First().Title.Should().Be("Post Title 9");
        }
Пример #7
0
        public void ByTitle()
        {
            IPostRepository repo = new PostRepository(new BlogContext());

            var criteria = new PostCriteria(null, null, "9");

            var posts = repo.Retrieve(criteria, Order <Post> .ByDescending(post => post.PublishDate));

            posts.Count().Should().Be(1);
            posts.First().Title.Should().Be("Post Title 9");
        }
Пример #8
0
        public HttpResponseMessage SearchPosts()
        {
            IQueryable <Posts> result = null;
            int totalRecords          = 0;

            try
            {
                PostCriteria criteria = null;
                result = postService.SearchPost(criteria, ref totalRecords);
            }
            catch (Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, result));
        }
Пример #9
0
        internal List <TDto> RetrievePostsBySearch <TDto>(PostCriteria criteria, IDataConverter <PostInfoData, TDto> converter)
            where TDto : class
        {
            ArgumentValidator.IsNotNull("criteria", criteria);
            ArgumentValidator.IsNotNull("converter", converter);

            IPostService service = UnitOfWork.GetService <IPostService>();

            var query = service.Search(criteria);

            if (query.HasResult)
            {
                return(query.DataToDtoList(converter).ToList());
            }

            return(null);
        }
        /// <summary>
        /// Override of Retrieve that builds an explicit SQL query to do the work instead of have EF do it
        /// </summary>
        /// <param name="criteria">The post criteria</param>
        /// <param name="orderBy">The criteria for ordering the results</param>
        /// <returns>All of the Posts in the database which satisfy the criteria</returns>
        /// <remarks>
        /// This is an example to demonstrate replacing an EF-based method with a custom method. 
        /// Because the Order class and the custom criteria class provide enough information, 
        /// EF's generated query can be completely replaced with something else. Here it's a SQL 
        /// statement that we cobble together; it could just as easily be a stored procedure or a query
        /// against a NoSQL store of some kind. You're not locked into EF or SQL Server in any way.
        /// </remarks>
        public override IEnumerable<Post> Retrieve(PostCriteria criteria = null, params Order<Post>[] orderBy)
        {
            string whereClause = string.Empty;
            var parameters = new List<DbParameter>();

            string orderByClause = string.Empty;

            if(criteria != null)
            {
                if(!string.IsNullOrEmpty(criteria.Title))
                {
                    if(whereClause.Length > 0)
                    {
                        whereClause += " and ";
                    }

                    whereClause += " title = @title";
                    var param = new SqlParameter("title", criteria.Title);
                    parameters.Add(param);
                }

                if(criteria.AfterDate.HasValue)
                {
                    if(whereClause.Length > 0)
                    {
                        whereClause += " and ";
                    }

                    whereClause += " publishdate >= @afterDate";
                    var param = new SqlParameter("afterDate", criteria.AfterDate.Value);
                    parameters.Add(param);
                }

                if(criteria.BeforeDate.HasValue)
                {
                    if(whereClause.Length > 0)
                    {
                        whereClause += " and ";
                    }

                    whereClause += " publishdate < @beforeDate";
                    var param = new SqlParameter("beforeDate", criteria.BeforeDate.Value);
                    parameters.Add(param);
                }
            }

            foreach(var o in orderBy)
            {
                if(orderByClause.Length > 0)
                {
                    orderByClause += ", ";
                }
                else
                {
                    orderByClause += "order by ";
                }

                // For the love of all that's holy, if you implement something like this
                // in the real world make sure you sanitize this 'order by' clause that you're
                // building to avoid SQL injection
                orderByClause += o.PropertyName + (o.Descending ? " desc" : " asc");
            }

            if(whereClause.Length > 0)
            {
                whereClause = "where " + whereClause;
            }

            string command = string.Format("select * from posts {0} {1}", whereClause, orderByClause);

            return _context.Database.SqlQuery<Post>(command, parameters.Cast<object>().ToArray());
        }
Пример #11
0
        public IServiceQueryResultList <PostInfoData> Search(PostCriteria criteria)
        {
            IEnumerable <PostInfoData> result = Repository.Search(criteria);

            return(ServiceResultFactory.BuildServiceQueryResult <PostInfoData>(result));
        }
Пример #12
0
 public IEnumerable<Post> Retrieve(int pageSize, int pageIndex, out int virtualCount, PostCriteria criteria = null,
     params Order<Post>[] orderBy)
 {
     return _pagedRepository.Retrieve(pageSize, pageIndex, out virtualCount, criteria, orderBy);
 }
Пример #13
0
 public virtual IEnumerable<Post> Retrieve(PostCriteria criteria = null, params Order<Post>[] orderBy)
 {
     return _matchingRepository.Retrieve(criteria, orderBy);
 }