Skip to content

Instantly share code, notes, and snippets.

@cprieto
Created July 16, 2010 18:52
Show Gist options
  • Save cprieto/478753 to your computer and use it in GitHub Desktop.
Save cprieto/478753 to your computer and use it in GitHub Desktop.
using System;
using BrokersWeb.Search.Domain;
using NHibernate;
using NHibernate.Criterion;
using System.Linq;
namespace BrokersWeb.Search.Data{
public class NhSearchListingQuery : ISearchListingQuery{
public NhSearchListingQuery(ISessionFactory factory){
ISession session = factory.GetCurrentSession();
Criteria = session.QueryOver<Listing>();
}
public IQueryOver<Listing, Listing> Criteria { get; set; }
#region ISearchListingQuery Members
public string StateCode{
set{
if (string.IsNullOrEmpty(value)) return;
Criteria.Where(
Restrictions.On<Listing>(l => l.StateCode).IsLike(value, MatchMode.Exact));
}
}
public int? ProductId{
set{
if (!value.HasValue) return;
Criteria.Where(l => l.ProductId == value.Value);
}
}
public int? AffiliateId{
set{
if (!value.HasValue) return;
throw new NotImplementedException();
}
}
public int? ExcludedDomain{
set{
if (!value.HasValue) return;
Criteria.Where(l => l.Domain.Id != value.Value);
}
}
public int[] ClickedItems {
set {
if (value == null || value.Length == 0)
return;
Criteria.Where(!Restrictions.On<Listing>(l => l.Id).IsIn(value));
}
}
public int[] ExcludeCategories {
set {
if (value == null || value.Length == 0)
return;
throw new NotImplementedException();
}
}
// TODO: Implement not in categories
public IPagedList<Listing> Query(int limit, int page){
var countQuery = Criteria.ToRowCountQuery();
int offset = (page - 1) * limit;
var listings = Criteria.OrderBy(l => l.Bid).Desc.Take(limit).Skip(offset).Future<Listing>();
var count = countQuery.FutureValue<int>().Value;
return new PagedList<Listing>(listings, count);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment