private void SetTopicReadDate(int topicId) { TopicRead topicRead = this.Session.Query <TopicRead>().SingleOrDefault(r => r.ReadByUser.Id == this.MediaCommUser.Id && r.ReadTopic.Id == topicId) ?? new TopicRead { ReadByUser = this.MediaCommUser, ReadTopic = this.Session.Load <Topic>(topicId) }; // Add one second to prevent false unread results for own posts topicRead.LastVisit = DateTime.Now.AddSeconds(1); this.Session.SaveOrUpdate(topicRead); }
/// <summary>Gets the posts for the specified page of the topic.</summary> /// <param name="topicId">The topic ID.</param> /// <param name="pagingParameters">The paging parameters.</param> /// <param name="currentUser">The current user.</param> /// <returns>The posts.</returns> public IEnumerable <Post> GetPostsForTopic(int topicId, PagingParameters pagingParameters, MediaCommUser currentUser) { this.Logger.Debug("Getting posts for topic with id '{0}' and paging parameters: {1}", topicId, pagingParameters); int postsToSkip = (pagingParameters.CurrentPage - 1) * pagingParameters.PageSize; List <Post> posts = this.Session.Query <Post>().Where(p => p.Topic.Id == topicId) .OrderBy(p => p.Created) .Skip(postsToSkip) .Take(pagingParameters.PageSize).ToList(); int lastPage = ((pagingParameters.TotalCount - 1) / pagingParameters.PageSize) + 1; bool isLastPage = pagingParameters.CurrentPage == lastPage; if (isLastPage) { this.InvokeTransaction( s => { TopicRead topicRead = s.Query <TopicRead>().SingleOrDefault( r => r.ReadByUser.Id == currentUser.Id && r.ReadTopic.Id == topicId) ?? new TopicRead { ReadByUser = currentUser, ReadTopic = s.Get <Topic>(topicId) }; topicRead.LastVisit = DateTime.Now; s.SaveOrUpdate(topicRead); }); } this.Logger.Debug("Got {0} posts", posts.Count()); return(posts); }