public async Task ToggleBoard(InputModels.ToggleBoardInput input) { var topic = await DbContext.Topics.FindAsync(input.TopicId); var boards = await BoardRepository.Records(); if (topic is null || topic.Deleted || !boards.Any(r => r.Id == input.BoardId)) { throw new HttpNotFoundError(); } var existingRecords = await DbContext.TopicBoards.Where(p => p.TopicId == input.TopicId && p.BoardId == input.BoardId).ToListAsync(); if (existingRecords.Count() == 0) { var topicBoardRecord = new DataModels.TopicBoard { TopicId = input.TopicId, BoardId = input.BoardId, UserId = UserContext.ApplicationUser.Id }; DbContext.TopicBoards.Add(topicBoardRecord); } else { DbContext.TopicBoards.RemoveRange(existingRecords); } await DbContext.SaveChangesAsync(); }
public async Task<ServiceModels.ServiceResponse> CreateTopic(InputModels.MessageInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); var processedMessage = await ProcessMessageInput(serviceResponse, input.Body); if (!serviceResponse.Success) { return serviceResponse; } var record = CreateMessageRecord(processedMessage, null); var existingMessageBoards = DbContext.MessageBoards.Where(item => item.MessageId == record.Id).ToList(); foreach (var item in existingMessageBoards) { DbContext.Remove(item); } foreach (var selectedBoard in input.SelectedBoards) { var board = (await BoardRepository.Records()).FirstOrDefault(item => item.Id == selectedBoard); if (board != null) { DbContext.MessageBoards.Add(new DataModels.MessageBoard { MessageId = record.Id, BoardId = board.Id, TimeAdded = DateTime.Now, UserId = UserContext.ApplicationUser.Id }); } } DbContext.SaveChanges(); serviceResponse.RedirectPath = UrlHelper.DisplayMessage(record.Id); return serviceResponse; }
public async Task Toggle(InputModels.ToggleBoardInput input) { var messageRecord = await DbContext.Messages.FindAsync(input.MessageId); if (messageRecord is null) { throw new HttpNotFoundError(); } if (!(await BoardRepository.Records()).Any(r => r.Id == input.BoardId)) { throw new HttpNotFoundError(); } var messageId = input.MessageId; if (messageRecord.ParentId > 0) { messageId = messageRecord.ParentId; } var boardId = input.BoardId; var existingRecord = await DbContext.MessageBoards.FirstOrDefaultAsync(p => p.MessageId == messageId && p.BoardId == boardId); if (existingRecord is null) { var messageBoardRecord = new DataModels.MessageBoard { MessageId = messageId, BoardId = boardId, UserId = UserContext.ApplicationUser.Id }; DbContext.MessageBoards.Add(messageBoardRecord); } else { DbContext.MessageBoards.Remove(existingRecord); } await DbContext.SaveChangesAsync(); }
public async Task <ControllerModels.Topics.CreateTopicResult> CreateTopic(ControllerModels.Topics.CreateTopicInput input) { var result = new ControllerModels.Topics.CreateTopicResult(); var processedMessage = await MessageRepository.ProcessMessageInput(input.Body); result.Errors = processedMessage.Errors; if (!result.Errors.Any()) { var message = await MessageRepository.CreateMessageRecord(processedMessage); var topic = new DataModels.Topic { FirstMessageId = message.Id, FirstMessagePostedById = message.PostedById, FirstMessageTimePosted = message.TimePosted, FirstMessageShortPreview = message.ShortPreview, LastMessageId = message.Id, LastMessagePostedById = message.PostedById, LastMessageTimePosted = message.TimePosted, LastMessageShortPreview = message.ShortPreview, }; DbContext.Topics.Add(topic); await DbContext.SaveChangesAsync(); message.TopicId = topic.Id; DbContext.Update(message); MessageRepository.UpdateTopicParticipation(topic.Id, UserContext.ApplicationUser.Id, message.TimePosted); var boards = await BoardRepository.Records(); foreach (var selectedBoard in input.SelectedBoards) { var board = boards.FirstOrDefault(item => item.Id == selectedBoard); if (board != null) { DbContext.TopicBoards.Add(new DataModels.TopicBoard { TopicId = topic.Id, BoardId = board.Id, TimeAdded = DateTime.Now, UserId = UserContext.ApplicationUser.Id }); } } await DbContext.SaveChangesAsync(); await ForumHub.Clients.All.SendAsync("new-topic", new HubModels.Message { TopicId = topic.Id, MessageId = message.Id }); result.TopicId = topic.Id; result.MessageId = message.Id; } return(result); }
public async Task <List <ViewModels.TopicPreview> > GetPreviews(List <int> topicIds) { var topicsQuery = from topic in DbContext.Topics where topicIds.Contains(topic.Id) where !topic.Deleted select topic; var topics = await topicsQuery.ToListAsync(); var topicBoardsQuery = from topicBoard in DbContext.TopicBoards where topicIds.Contains(topicBoard.TopicId) select new { topicBoard.BoardId, topicBoard.TopicId }; var topicBoards = await topicBoardsQuery.ToListAsync(); var users = await AccountRepository.Records(); var topicPreviews = new List <ViewModels.TopicPreview>(); var today = DateTime.Now.Date; var boards = await BoardRepository.Records(); foreach (var topicId in topicIds) { var topic = topics.First(item => item.Id == topicId); var firstMessagePostedBy = users.First(r => r.Id == topic.FirstMessagePostedById); var lastMessagePostedBy = users.FirstOrDefault(r => r.Id == topic.LastMessagePostedById); var topicPreview = new ViewModels.TopicPreview { Id = topic.Id, Pinned = topic.Pinned, ViewCount = topic.ViewCount, ReplyCount = topic.ReplyCount, Popular = topic.ReplyCount > UserContext.ApplicationUser.PopularityLimit, Pages = Convert.ToInt32(Math.Ceiling(1.0 * topic.ReplyCount / UserContext.ApplicationUser.MessagesPerPage)), FirstMessageId = topic.FirstMessageId, FirstMessageTimePosted = topic.FirstMessageTimePosted, FirstMessagePostedById = topic.FirstMessagePostedById, FirstMessagePostedByName = firstMessagePostedBy?.DecoratedName ?? "User", FirstMessagePostedByBirthday = today == new DateTime(today.Year, firstMessagePostedBy.Birthday.Month, firstMessagePostedBy.Birthday.Day).Date, FirstMessageShortPreview = string.IsNullOrEmpty(topic.FirstMessageShortPreview.Trim()) ? "No subject" : topic.FirstMessageShortPreview, LastMessageId = topic.Id, LastMessageTimePosted = topic.LastMessageTimePosted, LastMessagePostedById = topic.LastMessagePostedById, LastMessagePostedByName = lastMessagePostedBy?.DecoratedName ?? "User", LastMessagePostedByBirthday = today == new DateTime(today.Year, firstMessagePostedBy.Birthday.Month, firstMessagePostedBy.Birthday.Day).Date, }; topicPreviews.Add(topicPreview); var historyTimeLimit = DateTime.Now.AddDays(-14); if (topic.LastMessageTimePosted > historyTimeLimit) { topicPreview.Unread = GetUnreadLevel(topic.Id, topic.LastMessageTimePosted); } var topicPreviewBoardsQuery = from topicBoard in topicBoards where topicBoard.TopicId == topic.Id join board in boards on topicBoard.BoardId equals board.Id orderby board.DisplayOrder select new Models.ViewModels.Boards.IndexBoard { Id = board.Id.ToString(), Name = board.Name, Description = board.Description, DisplayOrder = board.DisplayOrder, }; topicPreview.Boards = topicPreviewBoardsQuery.ToList(); } return(topicPreviews); }
public async Task <List <ItemModels.MessagePreview> > GetPreviews(List <int> messageIds) { var messageQuery = from message in DbContext.Messages where messageIds.Contains(message.Id) select new { message.Id, message.ShortPreview, message.ViewCount, message.ReplyCount, message.TimePosted, message.PostedById, message.LastReplyId, message.LastReplyById, message.LastReplyPosted, message.Pinned }; var messages = await messageQuery.ToListAsync(); var users = await AccountRepository.Records(); var messagePreviews = new List <ItemModels.MessagePreview>(); var today = DateTime.Now.Date; var boards = await BoardRepository.Records(); foreach (var messageId in messageIds) { var message = messages.First(item => item.Id == messageId); var postedBy = users.First(r => r.Id == message.PostedById); var messagePreview = new ItemModels.MessagePreview { Id = message.Id, ShortPreview = string.IsNullOrEmpty(message.ShortPreview.Trim()) ? "No subject" : message.ShortPreview, Views = message.ViewCount, Replies = message.ReplyCount, Pages = Convert.ToInt32(Math.Ceiling(1.0 * message.ReplyCount / UserContext.ApplicationUser.MessagesPerPage)), LastReplyId = message.Id, Popular = message.ReplyCount > UserContext.ApplicationUser.PopularityLimit, Pinned = message.Pinned, TimePosted = message.TimePosted, PostedById = message.PostedById, PostedByName = postedBy.DecoratedName, PostedByBirthday = today == new DateTime(today.Year, postedBy.Birthday.Month, postedBy.Birthday.Day).Date }; messagePreviews.Add(messagePreview); var lastMessageTime = message.TimePosted; if (message.LastReplyId != 0) { var lastReply = (from item in DbContext.Messages where item.Id == message.LastReplyId select new { item.ShortPreview }).FirstOrDefault(); if (lastReply != null) { var lastReplyBy = users.FirstOrDefault(r => r.Id == message.LastReplyById); messagePreview.LastReplyId = message.LastReplyId; messagePreview.LastReplyPreview = lastReply.ShortPreview; messagePreview.LastReplyById = message.LastReplyById; messagePreview.LastReplyPosted = message.LastReplyPosted; lastMessageTime = message.LastReplyPosted; messagePreview.LastReplyByName = lastReplyBy?.DecoratedName ?? "User"; if (!(lastReplyBy is null)) { messagePreview.LastReplyByBirthday = today.Date == new DateTime(today.Year, lastReplyBy.Birthday.Month, lastReplyBy.Birthday.Day).Date; } } } var historyTimeLimit = DateTime.Now.AddDays(-14); if (lastMessageTime > historyTimeLimit) { messagePreview.Unread = GetUnreadLevel(message.Id, lastMessageTime); } var boardIdQuery = from messageBoard in DbContext.MessageBoards where messageBoard.MessageId == message.Id select messageBoard.BoardId; var messageBoards = new List <Models.ViewModels.Boards.Items.IndexBoard>(); foreach (var boardId in boardIdQuery) { var boardRecord = boards.Single(r => r.Id == boardId); messageBoards.Add(new Models.ViewModels.Boards.Items.IndexBoard { Id = boardRecord.Id.ToString(), Name = boardRecord.Name, Description = boardRecord.Description, DisplayOrder = boardRecord.DisplayOrder, }); } messagePreview.Boards = messageBoards.OrderBy(r => r.DisplayOrder).ToList(); } return(messagePreviews); }