diff --git a/OurUmbraco.Site/Views/DisplayTopic.cshtml b/OurUmbraco.Site/Views/DisplayTopic.cshtml index bbc3369b..6d67897c 100644 --- a/OurUmbraco.Site/Views/DisplayTopic.cshtml +++ b/OurUmbraco.Site/Views/DisplayTopic.cshtml @@ -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 { topicAuthor }; var memberRoles = new Dictionary>(); - 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); } } } diff --git a/OurUmbraco.Site/Views/Partials/Forum/Thread.cshtml b/OurUmbraco.Site/Views/Partials/Forum/Thread.cshtml index 0ae998d4..6bf43bb0 100644 --- a/OurUmbraco.Site/Views/Partials/Forum/Thread.cshtml +++ b/OurUmbraco.Site/Views/Partials/Forum/Thread.cshtml @@ -33,7 +33,7 @@ -@if (Model.MemberData != null) +@if (Model != null && Model.MemberData != null) {

Are you sure?

diff --git a/OurUmbraco/Forum/Services/TopicService.cs b/OurUmbraco/Forum/Services/TopicService.cs index c4f96148..56896e17 100644 --- a/OurUmbraco/Forum/Services/TopicService.cs +++ b/OurUmbraco/Forum/Services/TopicService.cs @@ -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(topicVoteQuery, new { id = id }); - results.Votes = new List(); - 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(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(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(); - 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(commentsVotesQuery, new { id = id }); + + foreach (var readOnlyComment in results.Comments) + { + var votes = commentsVotes.Where(x => x.CommentId == readOnlyComment.Id); + readOnlyComment.Votes = new List(); + 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; }