diff --git a/OurUmbraco.Site/Views/MacroPartials/Forum/Latest.cshtml b/OurUmbraco.Site/Views/MacroPartials/Forum/Latest.cshtml
index 4d1f6c9c..06402aae 100644
--- a/OurUmbraco.Site/Views/MacroPartials/Forum/Latest.cshtml
+++ b/OurUmbraco.Site/Views/MacroPartials/Forum/Latest.cshtml
@@ -9,7 +9,7 @@
}
- @foreach (var topic in topics.Items)
+ @foreach (var topic in topics)
{
-
@topic.Title
diff --git a/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml b/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml
index 60357378..385a6872 100644
--- a/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml
+++ b/OurUmbraco.Site/Views/MacroPartials/Forum/LatestActivity.cshtml
@@ -6,16 +6,16 @@
}
- @Column(ts.GetLatestTopics(3, 1).Items)
+ @Column(ts.GetLatestTopics(3, 1))
- @Column(ts.GetLatestTopics(3, 2).Items)
+ @Column(ts.GetLatestTopics(3, 2))
-@helper Column(List topics)
+@helper Column(IEnumerable topics)
{
- @foreach (var topic in topics)
+ @foreach (var topic in topics.ToArray())
{
var cat = Umbraco.TypedContent(topic.ParentId);
var mem = Members.GetById(topic.LatestReplyAuthor);
diff --git a/uForum/Api/PublicForumController.cs b/uForum/Api/PublicForumController.cs
index 33715e24..2ac699c3 100644
--- a/uForum/Api/PublicForumController.cs
+++ b/uForum/Api/PublicForumController.cs
@@ -19,7 +19,7 @@ namespace uForum.Api
public IEnumerable LatestPaged(int page, int cat)
{
var l = new List();
- foreach (var topic in TopicService.GetLatestTopics(50, page, true, cat).Items)
+ foreach (var topic in TopicService.GetLatestTopics(50, page, true, cat))
{
dynamic o = new ExpandoObject();
diff --git a/uForum/Services/TopicService.cs b/uForum/Services/TopicService.cs
index c04400a2..6255a1f3 100644
--- a/uForum/Services/TopicService.cs
+++ b/uForum/Services/TopicService.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -28,24 +29,34 @@ namespace uForum.Services
///
///
///
- public Page GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1)
+ public IEnumerable GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1)
{
- var sql = new Sql().Select(@"forumTopics.*, u1.[text] as LastReplyAuthorName, u2.[text] as AuthorName")
- .From("forumTopics")
- .LeftOuterJoin("umbracoNode u1").On("(forumTopics.latestReplyAuthor = u1.id AND u1.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')")
- .LeftOuterJoin("umbracoNode u2").On("(forumTopics.memberId = u2.id AND u2.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')");
+ const string sql1 = @"SELECT forumTopics.*, u1.[text] as LastReplyAuthorName, u2.[text] as AuthorName
+FROM forumTopics
+LEFT OUTER JOIN umbracoNode u1 ON (forumTopics.latestReplyAuthor = u1.id AND u1.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')
+LEFT OUTER JOIN umbracoNode u2 ON (forumTopics.memberId = u2.id AND u2.nodeObjectType = '39EB0F98-B348-42A1-8662-E7EB18487560')
+";
+ const string sql2 = @"
+ORDER BY updated
+OFFSET @offset ROWS
+FETCH NEXT @count ROWS ONLY";
- if (ignoreSpam)
+ const string sqlxx = sql1 + sql2;
+ const string sqlix = sql1 + "WHERE isSpam=0" + sql2;
+ const string sqlxc = sql1 + "WHERE forumTopics.parentId=@category" + sql2;
+ const string sqlic = sql1 + "WHERE isSpam=0 AND forumTopics.parentId=@category" + sql2;
+
+ var sql = ignoreSpam
+ ? (category > 0 ? sqlic : sqlix)
+ : (category > 0 ? sqlxc : sqlxx);
+
+ // probably as fast as PetaPoco can be...
+ return _databaseContext.Database.Fetch(sql, new
{
- sql.Where(x => x.IsSpam != true);
- }
-
- if (category > 0)
- sql.Where(x => x.ParentId == category);
-
-
- sql.OrderByDescending("updated");
- return _databaseContext.Database.Page(page, take, sql);
+ offset = (page - 1) * take,
+ count = take,
+ category = category
+ }).OrderByDescending(x => x.Updated);
}
///