public List <MessageViewDTO> GetHashtagFeed(string hashtag, DiscussionSortFields field, CommonEnums.SortDirections dir, out long?hashtagId, out string error) { error = string.Empty; hashtagId = null; try { hashtagId = FindHashtagByName(hashtag); if (hashtagId == null) { error = "Hashtag " + hashtag + " not found"; return(new List <MessageViewDTO>()); } var messages = DiscussionClassRoomRepository.GetHashtagMessages((long)hashtagId).Select(s => s.Entity2MessageViewDto()).ToList(); return(BuildMessageFeedTree(messages.ToArray(), field, dir)); } catch (Exception ex) { error = Utils.FormatError(ex); Logger.Error("get hashtag " + hashtag + " feed", null, ex, CommonEnums.LoggerObjectTypes.Discussion); return(new List <MessageViewDTO>()); } }
public List <MessageViewDTO> GetRoomMessages(int roomId, DiscussionSortFields field, CommonEnums.SortDirections dir) { try { var messages = DiscussionClassRoomRepository.GetRoomMessages(roomId).Select(s => s.Entity2MessageViewDto()).ToList(); return(BuildMessageFeedTree(messages.ToArray(), field, dir)); } catch (Exception ex) { Logger.Error("get room feed", roomId, ex, CommonEnums.LoggerObjectTypes.Discussion); return(new List <MessageViewDTO>()); } }
private static List <MessageViewDTO> BuildMessageFeedTree(MessageViewDTO[] unsorted, DiscussionSortFields field, CommonEnums.SortDirections dir) { var parents = unsorted.Where(x => x.ParentMessageId == null).ToList(); var sortedParents = new List <MessageViewDTO>(); switch (field) { case DiscussionSortFields.AddOn: switch (dir) { case CommonEnums.SortDirections.desc: sortedParents = parents.OrderByDescending(x => x.AddOn).ToList(); break; case CommonEnums.SortDirections.asc: sortedParents = parents.OrderBy(x => x.AddOn).ToList(); break; } break; case DiscussionSortFields.CreatorName: switch (dir) { case CommonEnums.SortDirections.desc: sortedParents = parents.OrderByDescending(x => x.CreatorName).ToList(); break; case CommonEnums.SortDirections.asc: sortedParents = parents.OrderBy(x => x.CreatorName).ToList(); break; } break; case DiscussionSortFields.CourseName: switch (dir) { case CommonEnums.SortDirections.desc: sortedParents = parents.OrderByDescending(x => x.CourseName).ToList(); break; case CommonEnums.SortDirections.asc: sortedParents = parents.OrderBy(x => x.CourseName).ToList(); break; } break; } foreach (var parent in sortedParents) { var p = parent; foreach (var child in unsorted.Where(x => x.ParentMessageId != null && x.ParentMessageId == p.MessageId).OrderByDescending(x => x.PostedOn)) { p.Replies.Add(child); } } return(sortedParents); }