Check if topic still exist before trying to render it

This commit is contained in:
Sebastiaan Janssen
2017-01-28 21:47:35 +01:00
parent 9845fe0f76
commit 94b5533322
3 changed files with 64 additions and 55 deletions
+43 -28
View File
@@ -8,51 +8,66 @@
var topicService = new TopicService(ApplicationContext.DatabaseContext);
var topic = topicService.CurrentTopic(Context, ApplicationContext.ApplicationCache.RequestCache, MemberData);
var subscribed = false;
if (MemberData != null && MemberData.Member != null)
if (MemberData != null && MemberData.Member != null && topic != null)
{
subscribed = Utils.IsSubscribedToForumTopic(topic.Id, MemberData.Member.Id);
}
var currentMember = MemberData != null && MemberData.Member != null ? MemberData.Member : null;
var topicAuthor = currentMember != null && topic.MemberId == currentMember.Id ? currentMember : Members.GetById(topic.MemberId);
IPublishedContent topicAuthor = null;
if (topic != null)
{
if (currentMember != null && topic.MemberId == currentMember.Id)
{
topicAuthor = currentMember;
}
else
{
topicAuthor = Members.GetById(topic.MemberId);
}
}
var topicMembers = new List<IPublishedContent> { topicAuthor };
var memberRoles = new Dictionary<int, List<string>>();
if (currentMember != null && currentMember.Id == topic.MemberId)
if (topic != null)
{
memberRoles.Add(topic.MemberId, MemberData.Roles.ToList());
}
else
{
memberRoles.Add(topic.MemberId, topicAuthor.GetRoles());
}
foreach (var comment in topic.Comments)
{
if (topicMembers.Any(x => x.Id == comment.MemberId) == false)
if (currentMember != null && currentMember.Id == topic.MemberId)
{
IPublishedContent commentAuthor;
if (MemberData != null && MemberData.Member != null && MemberData.Member.Id == comment.MemberId)
memberRoles.Add(topic.MemberId, MemberData.Roles.ToList());
}
else
{
memberRoles.Add(topic.MemberId, topicAuthor.GetRoles());
}
foreach (var comment in topic.Comments)
{
if (topicMembers.Any(x => x.Id == comment.MemberId) == false)
{
commentAuthor = currentMember;
if (memberRoles.ContainsKey(comment.MemberId) == false)
IPublishedContent commentAuthor;
if (MemberData != null && MemberData.Member != null && MemberData.Member.Id == comment.MemberId)
{
var roles = MemberData.Roles.ToList();
memberRoles.Add(comment.MemberId, roles);
commentAuthor = currentMember;
if (memberRoles.ContainsKey(comment.MemberId) == false)
{
var roles = MemberData.Roles.ToList();
memberRoles.Add(comment.MemberId, roles);
}
}
}
else
{
commentAuthor = Members.GetById(comment.MemberId);
if (memberRoles.ContainsKey(comment.MemberId) == false)
else
{
var roles = commentAuthor.GetRoles();
memberRoles.Add(comment.MemberId, roles);
}
commentAuthor = Members.GetById(comment.MemberId);
if (memberRoles.ContainsKey(comment.MemberId) == false)
{
var roles = commentAuthor.GetRoles();
memberRoles.Add(comment.MemberId, roles);
}
}
topicMembers.Add(commentAuthor);
}
topicMembers.Add(commentAuthor);
}
}
}
@@ -33,7 +33,7 @@
<!-- COPY LINK END -->
<!-- DELETE THREAD/COMMENT START -->
@if (Model.MemberData != null)
@if (Model != null && Model.MemberData != null)
{
<div id="confirm-wrapper" class="confirm-wrapper">
<h4>Are you sure?</h4>
+20 -26
View File
@@ -231,34 +231,26 @@ WHERE forumTopics.id=@id
new TopicCommentRelator().Map,
sql, new { id = id }).FirstOrDefault();
if (results == null)
return null;
const string topicVoteQuery = @"SELECT memberId, umbracoNode.text AS memberName FROM powersTopic LEFT JOIN umbracoNode ON (powersTopic.memberId = umbracoNode.Id) WHERE powersTopic.id = @id AND receiverId != 0";
var topicVotes = _databaseContext.Database.Fetch<SimpleMember>(topicVoteQuery, new { id = id });
results.Votes = new List<SimpleMember>();
if(topicVotes != null)
if (results != null)
{
const string topicVoteQuery = @"SELECT memberId, umbracoNode.text AS memberName FROM powersTopic LEFT JOIN umbracoNode ON (powersTopic.memberId = umbracoNode.Id) WHERE powersTopic.id = @id AND receiverId != 0";
var topicVotes = _databaseContext.Database.Fetch<SimpleMember>(topicVoteQuery, new { id = id });
results.Votes = topicVotes;
if (results.Comments.Any() == false)
return results;
const string commentsVotesQuery = @"SELECT powersComment.id, powersComment.memberId, umbracoNode.text AS memberName FROM powersComment LEFT JOIN umbracoNode ON (powersComment.memberId = umbracoNode.id) WHERE powersComment.id in (SELECT id FROM forumComments WHERE topicId = @id) AND receiverId != 0";
var commentsVotes = _databaseContext.Database.Fetch<SimpleMember>(commentsVotesQuery, new { id = id });
foreach (var readOnlyComment in results.Comments)
{
if (commentsVotes == null)
continue;
var votes = commentsVotes.Where(x => x.CommentId == readOnlyComment.Id).ToList();
if (votes.Any() == false)
continue;
readOnlyComment.Votes = new List<SimpleMember>();
foreach (var simpleMember in votes)
if (results.Comments.Any())
{
readOnlyComment.Votes.Add(simpleMember);
const string commentsVotesQuery = @"SELECT powersComment.id, powersComment.memberId, umbracoNode.text AS memberName FROM powersComment LEFT JOIN umbracoNode ON (powersComment.memberId = umbracoNode.id) WHERE powersComment.id in (SELECT id FROM forumComments WHERE topicId = @id) AND receiverId != 0";
var commentsVotes = _databaseContext.Database.Fetch<SimpleMember>(commentsVotesQuery, new { id = id });
foreach (var readOnlyComment in results.Comments)
{
var votes = commentsVotes.Where(x => x.CommentId == readOnlyComment.Id);
readOnlyComment.Votes = new List<SimpleMember>();
foreach (var simpleMember in votes)
{
readOnlyComment.Votes.Add(simpleMember);
}
}
}
}
@@ -365,7 +357,9 @@ WHERE forumTopics.id=@id
return null;
});
topic.MemberData = memberData;
if(topic != null)
topic.MemberData = memberData;
return topic;
}