コード例 #1
0
        private static async Task <List <LocalCommunity.BBComment> > GetBBThreadCommentsFromDB(PegasunDBContext db, int threadId, LocalAccount.PegaUser currentUser)
        {
            var query = db.BBComments.Select(x => new
            {
                BBComment  = x,
                User       = new { x.User.UserId, x.User.Username, x.User.Email },
                VoteResult = new { TotalVotes = x.BBCommentVotes.Count, TotalUpvotes = x.BBCommentVotes.Count(v => v.IsUpvote) }
            });

            var result = await query.Where(x => x.BBComment.ThreadId == threadId).ToListAsync();

            List <LocalCommunity.BBComment> comments = new List <LocalCommunity.BBComment>();

            result.ForEach(r =>
            {
                LocalCommunity.BBComment comment = r.BBComment.Adapt <LocalCommunity.BBComment>();
                comment.User                  = r.User.Adapt <LocalAccount.PegaUser>();
                comment.VoteResult            = r.VoteResult.Adapt <LocalCommunity.VoteResult>();
                comment.CurrentLoggedInUserID = (currentUser?.UserId).GetValueOrDefault();

                comments.Add(comment);
            });

            return(comments);
        }
コード例 #2
0
        public static async Task <ResultsItem> CreateBBComment(LocalCommunity.BBComment comment, LocalAccount.PegaUser user)
        {
            try
            {
                using (PegasunDBContext db = new PegasunDBContext())
                {
                    comment.Message.Clean(new[] { Types.CleanInputType.AZ09CommonCharsSM });

                    BBComments serviceComment = comment.Adapt <BBComments>();
                    serviceComment.CreateDate = DateTime.Now;
                    serviceComment.UserId     = user.UserId;
                    serviceComment.Message    = serviceComment.Message.Clean(new[] { Types.CleanInputType.AZ09CommonCharsSM });

                    db.BBComments.Add(serviceComment);
                    await db.SaveChangesAsync();

                    return(ResultsItem.Success("Successfully posted a new comment."));
                }
            }
            catch (Exception ex)
            {
                Utilities.LogException(new[] { $"User:{user.Username}" }, ex);
                return(ResultsItem.Error($"Unable to create a comment. {ex.Message}"));
            }
        }