public PollView GetLatestPoll() { ISession session = NHibernateHelper.GetCurrentSession(); string hql = "FROM Poll p " + "ORDER BY p.PostedDate DESC"; IQuery query = session.CreateQuery(hql); query.SetMaxResults(1); Poll poll = query.UniqueResult<Poll>(); PollView latestPoll = new PollView(); latestPoll.Id = poll.Id; latestPoll.Question = poll.Question; latestPoll.Options = new List<PollOptionView>(); latestPoll.PostedDate = poll.PostedDate; latestPoll.NumComments = session.CreateCriteria(typeof(PollComment)) .Add(Restrictions.Eq("Poll.Id", poll.Id)) .SetProjection(Projections.Count("Id")) .UniqueResult<int>(); foreach (PollOption option in poll.PollOptions.OrderBy(po => po.OrderIndex)) { PollOptionView optionView = new PollOptionView(); optionView.Answer = option.Answer; optionView.Id = option.Id; optionView.Votes = option.Votes; optionView.SuggestComment = option.SuggestComment; optionView.Poll = latestPoll; latestPoll.Options.Add(optionView); } return latestPoll; }
public IList<PollView> GetPolls(int pageNumber, int recordsPerPage, out int totalRecords) { ISession session = NHibernateHelper.GetCurrentSession(); int firstRecord = ((pageNumber - 1) * recordsPerPage); IList<Poll> polls = session.CreateCriteria(typeof(Poll)) .AddOrder(Order.Desc("PostedDate")) .SetFirstResult(firstRecord) .SetMaxResults(recordsPerPage) .List<Poll>(); totalRecords = session.CreateCriteria(typeof(Poll)) .SetProjection(Projections.Count("Id")) .UniqueResult<int>(); List<PollView> pollViews = new List<PollView>(recordsPerPage); foreach (Poll poll in polls) { PollView pollView = new PollView(); pollView.Id = poll.Id; pollView.Question = poll.Question; pollView.PostedDate = poll.PostedDate; pollView.NumComments = session.CreateCriteria(typeof(PollComment)) .Add(Restrictions.Eq("Poll.Id", poll.Id)) .SetProjection(Projections.Count("Id")) .UniqueResult<int>(); pollView.ResultsMode = true; pollView.Options = (from o in poll.PollOptions orderby o.Votes descending select new PollOptionView() { Id = o.Id, Answer = o.Answer, Poll = pollView, Votes = o.Votes, SuggestComment = o.SuggestComment }).ToList(); pollViews.Add(pollView); } return pollViews; }