Revert to optimized TopicService and fixed related code properly

This commit is contained in:
Sebastiaan Janssen
2015-06-02 23:19:02 +02:00
parent e30e362d94
commit ab4cc4c098
4 changed files with 32 additions and 21 deletions
@@ -9,7 +9,7 @@
}
<ul>
@foreach (var topic in topics.Items)
@foreach (var topic in topics)
{
<li>
@topic.Title
@@ -6,16 +6,16 @@
}
<div class="col-md-6">
@Column(ts.GetLatestTopics(3, 1).Items)
@Column(ts.GetLatestTopics(3, 1))
</div>
<div class="col-md-6">
@Column(ts.GetLatestTopics(3, 2).Items)
@Column(ts.GetLatestTopics(3, 2))
</div>
@helper Column(List<uForum.Models.ReadOnlyTopic> topics)
@helper Column(IEnumerable<uForum.Models.ReadOnlyTopic> topics)
{
<ul>
@foreach (var topic in topics)
@foreach (var topic in topics.ToArray())
{
var cat = Umbraco.TypedContent(topic.ParentId);
var mem = Members.GetById(topic.LatestReplyAuthor);
+1 -1
View File
@@ -19,7 +19,7 @@ namespace uForum.Api
public IEnumerable<ExpandoObject> LatestPaged(int page, int cat)
{
var l = new List<ExpandoObject>();
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();
+26 -15
View File
@@ -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
/// <param name="ignoreSpam"></param>
/// <param name="category"></param>
/// <returns></returns>
public Page<ReadOnlyTopic> GetLatestTopics(long take = 50, long page = 1, bool ignoreSpam = true, int category = -1)
public IEnumerable<ReadOnlyTopic> 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<ReadOnlyTopic>(sql, new
{
sql.Where<Topic>(x => x.IsSpam != true);
}
if (category > 0)
sql.Where<Topic>(x => x.ParentId == category);
sql.OrderByDescending("updated");
return _databaseContext.Database.Page<ReadOnlyTopic>(page, take, sql);
offset = (page - 1) * take,
count = take,
category = category
}).OrderByDescending(x => x.Updated);
}
/// <summary>