public static async Task <LocalCommunity.BBThread> GetBBThreadWithComments(int threadId, LocalAccount.PegaUser currentUser) { try { using (PegasunDBContext db = new PegasunDBContext()) { var getThreadTask = GetBBThreadsFromDB(db, currentUser, threadId: threadId); var getCommentsTask = GetBBThreadCommentsFromDB(db, threadId, currentUser); LocalCommunity.BBThread thread = (await getThreadTask).FirstOrDefault(); List <LocalCommunity.BBComment> comments = await getCommentsTask; if (thread == null) { return(null); } thread.ThreadComments = comments; thread.ThreadName = "thread_" + thread.ThreadId; return(thread); } } catch (Exception ex) { Utilities.LogException(new[] { $"ThreadId:{threadId}" }, ex); return(new LocalCommunity.BBThread()); } }
public static async Task <ResultsItem> CreateBBThread(LocalCommunity.BBThread thread, LocalAccount.PegaUser user) { try { using (PegasunDBContext db = new PegasunDBContext()) { thread.Message = thread.Message.Clean(new[] { Types.CleanInputType.AZ09CommonCharsSM }); thread.UserId = user.UserId; BBThreads serviceMessageThread = thread.Adapt <BBThreads>(); serviceMessageThread.CreateDate = DateTime.Now; serviceMessageThread.UserId = user.UserId; serviceMessageThread.Message = serviceMessageThread.Message.Clean(new[] { Types.CleanInputType.AZ09CommonCharsSM }); serviceMessageThread.Title = serviceMessageThread.Title.Clean(new[] { Types.CleanInputType.AZ09CommonCharsSM }); db.BBThreads.Add(serviceMessageThread); await db.SaveChangesAsync(); return(ResultsItem.Success("Successfully created a new thread")); } } catch (Exception ex) { Utilities.LogException(new[] { $"User:{user.Username}" }, ex); return(ResultsItem.Error($"Unable to create a new thread. {ex.Message}")); } }
private static async Task <List <LocalCommunity.BBThread> > GetBBThreadsFromDB(PegasunDBContext db, LocalAccount.PegaUser currentUser, int?take = null, int?threadId = null, List <Types.ConvThreadCategory> categories = null, int officialCoindId = 0) { if ((take == null || categories.IsNullOrEmpty()) && threadId == null) { throw new Exception("GetMessageThread: Not all parameters were passed in correctly."); } var query = db.BBThreads.OrderByDescending(x => x.CreateDate).Select(x => new { BBThread = x, User = new { x.User.UserId, x.User.Username, x.User.Email }, VoteResult = new { TotalVotes = x.BBThreadVotes.Count, TotalUpvotes = x.BBThreadVotes.Count(v => v.IsUpvote) }, TotalComments = x.BBComments.Count }); var result = threadId.GetValueOrDefault() > 0 ? await query.Where(x => x.BBThread.ThreadId == threadId).ToListAsync() : officialCoindId > 0 ? await query.Where(x => x.BBThread.OfficialCoinId == officialCoindId).Take(take.GetValueOrDefault()).ToListAsync() : await query.Where(x => categories.Any(c => (short)c == x.BBThread.CategoryCode)).Take(take.GetValueOrDefault()).ToListAsync(); List <LocalCommunity.BBThread> threads = new List <LocalCommunity.BBThread>(); result.ForEach(r => { LocalCommunity.BBThread thread = r.BBThread.Adapt <LocalCommunity.BBThread>(); thread.User = r.User.Adapt <LocalAccount.PegaUser>(); thread.VoteResult = r.VoteResult.Adapt <LocalCommunity.VoteResult>(); thread.TotalComments = r.TotalComments; thread.CurrentLoggedInUserID = (currentUser?.UserId).GetValueOrDefault(); threads.Add(thread); }); return(threads); }