예제 #1
0
        public async Task <IViewComponentResult> InvokeAsync(long entityId, string entityTypeId)
        {
            var model = new CommentVm
            {
                EntityId     = entityId,
                EntityTypeId = entityTypeId
            };

            model.Items.Data = await _commentRepository
                               .Query()
                               .Where(x => (x.EntityId == entityId) && (x.EntityTypeId == entityTypeId) && (x.Status == CommentStatus.Approved))
                               .Where(x => x.ParentId == null)
                               .OrderByDescending(x => x.CreatedOn)
                               .Select(x => new CommentItem
            {
                Id            = x.Id,
                CommentText   = x.CommentText,
                CommenterName = x.CommenterName,
                CreatedOn     = x.CreatedOn,
                Replies       = x.Replies
                                .Where(r => r.Status == CommentStatus.Approved)
                                .OrderByDescending(r => r.CreatedOn)
                                .Select(r => new CommentItem()
                {
                    CommentText   = r.CommentText,
                    CommenterName = r.CommenterName,
                    CreatedOn     = r.CreatedOn
                })
                                .ToList()
            }).ToListAsync();

            model.CommentsCount = model.Items.Data.Count;

            return(View(this.GetViewPath(), model));
        }
        public async Task <IActionResult> GetCommentDetail(int commentId)
        {
            //// GET COMMENT WITH ID (KEY)
            var comment = await _context.Comments.FindAsync(commentId);

            //// IF KEY NOT EXIST (COMMENT IS NULL), RETURN STATUS 404
            if (comment == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found comment with id: {commentId}")));
            }

            //// GET USER OWNER COMMENT
            var user = await _context.Users.FindAsync(comment.OwnerUserId);

            //// CREATE A CONSTANCE OF COMMENT JUST SHOW NEEDED FIELD
            var commentVm = new CommentVm()
            {
                Id               = comment.Id,
                Content          = comment.Content,
                CreateDate       = comment.CreateDate,
                KnowledgeBaseId  = comment.KnowledgeBaseId,
                LastModifiedDate = comment.LastModifiedDate,
                OwnerUserId      = comment.OwnerUserId,
                OwnerName        = user.FirstName + " " + user.LastName
            };

            return(Ok(commentVm));
        }
예제 #3
0
        public List <CommentVm> GetComment([FromRoute] int id)
        {
            //if (!ModelState.IsValid)
            //{
            //    return BadRequest(ModelState);
            //}

            var comments       = _context.Comment.Where(m => m.PostId == id).ToList();
            var ListOfcomments = new List <CommentVm>();

            foreach (var comment in comments)
            {
                var user = _context.ApplicationUser.Where(u => u.Id == comment.UserId).FirstOrDefault();

                var userCommentVm = new CommentVm
                {
                    Text         = comment.Text,
                    UserName     = user.UserName,
                    profileImage = user.ImageUrl
                };
                ListOfcomments.Add(userCommentVm);
            }



            return(ListOfcomments);
            //if (comment == null)
            //         {
            //             return NotFound();
            //         }

            //         return Ok(comment);
        }
예제 #4
0
        public async Task <IActionResult> UpdateComment(CommentVm commentVm)
        {
            try
            {
                var comment = await _dbContext.Set <Comment>()
                              .Where(x => x.Id == commentVm.Id)
                              .FirstOrDefaultAsync();

                if (!string.IsNullOrWhiteSpace(commentVm.Text))
                {
                    comment.Text = commentVm.Text;
                }
                // if (!string.IsNullOrWhiteSpace(commentVm.FirstName))
                // {
                //     comment.Title = commentVm.Title;
                // }
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction("Info", new { problemId = comment.ProblemId }));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }
예제 #5
0
        public EditCommentWindow(IssueVm issueVm, CommentVm comment)
        {
            issueVm.CancelComment += Close;
            DataContext            = issueVm;
            issueVm.CommentText    = comment.Body;

            InitializeComponent();
        }
예제 #6
0
        public IViewComponentResult Invoke(long entityId, string entityTypeId)
        {
            var model = new CommentVm
            {
                EntityId     = entityId,
                EntityTypeId = entityTypeId
            };

            return(View(this.GetViewPath(), model));
        }
        public ActionResult Index()
        {
            var posts = DataSession.Query <Comments_ByPosts.CommentBlah,
                                           Comments_ByPosts>().ProjectFromIndexFieldsInto <Comments_ByPosts.CommentBlah>()
                        .Where(o => o.IsPublished == false)
                        .ToList();

            var model = new CommentVm {
                MyTables = posts
            };

            return(View(model));
        }
예제 #8
0
        public async Task <ActionResult> PostComment(CommentVm commentVm)
        {
            var comment = new Comment
            {
                UserID    = commentVm.UserID,
                ProductID = commentVm.ProductID,
                Content   = commentVm.Content,
                Image     = commentVm.Image
            };

            _context.Comments.Add(comment);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetComment", new { id = comment.ID }, comment));
        }
        public async Task <IActionResult> DeleteComment(int knowledgeBaseId, int commentId)
        {
            //// GET COMMENT WITH ID (KEY), IF KEY IS NOT EXIST, RETURN STATUS 404
            var comment = await _context.Comments.FindAsync(commentId);

            if (comment == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found the comment with id: {commentId}")));
            }

            //// REMOVE COMMENT
            _context.Comments.Remove(comment);

            //// GET KNOWLEDGE BASE WITH ID, IF NULL, RETURN STATUS 400
            var knowledgeBase = await _context.KnowledgeBases.FindAsync(knowledgeBaseId);

            if (knowledgeBase == null)
            {
                return(BadRequest(new ApiBadRequestResponse($"Cannot found knowledge base with id: {knowledgeBaseId}")));
            }

            //// UPDATE NUMBER OF COMMENT IS DECREASE 1 AND SAVE CHANGE
            knowledgeBase.NumberOfComments = knowledgeBase.NumberOfComments.GetValueOrDefault(0) - 1;
            _context.KnowledgeBases.Update(knowledgeBase);
            var result = await _context.SaveChangesAsync();

            //// IF RESULT AFTER DELETE IS GREATER THAN 0 (TRUE), RETURN STATUS 200, ELSE RETURN STATUS 400
            if (result > 0)
            {
                //Delete cache
                await _cacheService.RemoveAsync(CacheConstants.RecentComments);

                var commentVm = new CommentVm()
                {
                    Id               = comment.Id,
                    Content          = comment.Content,
                    CreateDate       = comment.CreateDate,
                    KnowledgeBaseId  = comment.KnowledgeBaseId,
                    LastModifiedDate = comment.LastModifiedDate,
                    OwnerUserId      = comment.OwnerUserId
                };
                return(Ok(commentVm));
            }
            return(BadRequest(new ApiBadRequestResponse($"Delete comment failed")));
        }
예제 #10
0
        public CommentVm Init(long userId, long?id)
        {
            var toRet = new CommentVm
            {
                ActionMode = Enumerations.ActionMode.Add,
                Comment    = new Comment {
                }
            };

            if (id != null)
            {
                var obj = LoadSingle(userId, Convert.ToInt64(id));
                toRet.Comment = obj;

                toRet.ActionMode = Enumerations.ActionMode.Edit;
            }

            return(toRet);
        }
예제 #11
0
        public CommentVm Save(long userId, CommentVm toSave)
        {
            var obj = toSave.Comment;

            PreSave(userId, ref obj, toSave.ActionMode);
            toSave.Comment = obj;

            switch (toSave.ActionMode)
            {
            case Enumerations.ActionMode.Add:
                toSave.Comment = Create(userId, toSave.Comment);
                break;

            case Enumerations.ActionMode.Edit:
                toSave.Comment = Edit(userId, toSave.Comment);
                break;
            }

            return(Init(userId, toSave.Comment.Id));
        }
예제 #12
0
        public async Task <IActionResult> DeleteComment(int knowledgeBaseId, int commentId)
        {
            var comment = await _context.Comments.FindAsync(commentId);

            if (comment == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found the comment with id: {commentId}")));
            }

            _context.Comments.Remove(comment);

            var knowledgeBase = await _context.KnowledgeBases.FindAsync(knowledgeBaseId);

            if (knowledgeBase == null)
            {
                return(BadRequest(new ApiBadRequestResponse($"Cannot found knowledge base with id: {knowledgeBaseId}")));
            }

            knowledgeBase.NumberOfComments = knowledgeBase.NumberOfComments.GetValueOrDefault(0) - 1;
            _context.KnowledgeBases.Update(knowledgeBase);

            var result = await _context.SaveChangesAsync();

            if (result > 0)
            {
                //Delete cache
                await _cacheService.RemoveAsync(CacheConstants.RecentComments);

                var commentVm = new CommentVm()
                {
                    Id               = comment.Id,
                    Content          = comment.Content,
                    CreateDate       = comment.CreateDate,
                    KnowledgeBaseId  = comment.KnowledgeBaseId,
                    LastModifiedDate = comment.LastModifiedDate,
                    OwnerUserId      = comment.OwnerUserId
                };
                return(Ok(commentVm));
            }
            return(BadRequest(new ApiBadRequestResponse($"Delete comment failed")));
        }
예제 #13
0
        public async Task <bool> GetCommenter(CommentVm comment)
        {
            try
            {
                using (HttpResponseMessage response = await _client.GetAsync("/users/getUsername?id=" + comment.CommenterId))
                {
                    if (response.IsSuccessStatusCode)
                    {
                        var name = await response.Content.ReadAsStringAsync();

                        comment.CommenterName = name.Trim('"');
                        return(true);
                    }
                }
            }
            catch (Exception e)
            {
                //excpetion getting commenter
            }
            return(false);
        }
예제 #14
0
        public async Task <IActionResult> GetCommentDetail(int commentId)
        {
            var comment = await _context.Comments.FindAsync(commentId);

            if (comment == null)
            {
                return(NotFound(new ApiNotFoundResponse($"Cannot found comment with id: {commentId}")));
            }
            var user = await _context.Users.FindAsync(comment.OwnerUserId);

            var commentVm = new CommentVm()
            {
                Id               = comment.Id,
                Content          = comment.Content,
                CreateDate       = comment.CreateDate,
                KnowledgeBaseId  = comment.KnowledgeBaseId,
                LastModifiedDate = comment.LastModifiedDate,
                OwnerUserId      = comment.OwnerUserId,
                OwnerName        = user.FirstName + " " + user.LastName
            };

            return(Ok(commentVm));
        }
예제 #15
0
        public async Task <IActionResult> List(long entityId, string entityTypeId, int?pageNumber, int?pageSize)
        {
            var entity = _commentRepository
                         .List()
                         .FirstOrDefault();

            if (entity == null)
            {
                return(Redirect("~/Error/FindNotFound"));
            }

            var itemsPerPage   = pageSize.HasValue ? pageSize.Value : DefaultPageSize;
            var currentPageNum = pageNumber.HasValue ? pageNumber.Value : 1;
            var offset         = (itemsPerPage * currentPageNum) - itemsPerPage;

            var model = new CommentVm();

            model.EntityName = entity.EntityName;
            model.EntitySlug = entity.EntitySlug;

            var query = _commentRepository
                        .Query()
                        .Where(x => (x.EntityId == entityId) && (x.EntityTypeId == entityTypeId) && (x.Status == CommentStatus.Approved))
                        .Where(x => x.ParentId == null)
                        .OrderByDescending(x => x.CreatedOn)
                        .Select(x => new CommentItem
            {
                Id            = x.Id,
                CommentText   = x.CommentText,
                CommenterName = x.CommenterName,
                CreatedOn     = x.CreatedOn,
                Replies       = x.Replies
                                .Where(r => r.Status == CommentStatus.Approved)
                                .OrderByDescending(r => r.CreatedOn)
                                .Select(r => new CommentItem()
                {
                    CommentText   = r.CommentText,
                    CommenterName = r.CommenterName,
                    CreatedOn     = r.CreatedOn
                })
                                .ToList()
            });

            model.Items.Data = await query
                               .Skip(offset)
                               .Take(itemsPerPage)
                               .ToListAsync();

            model.Items.PageNumber = currentPageNum;
            model.Items.PageSize   = itemsPerPage;
            model.Items.TotalItems = await query.CountAsync();

            var allItems = await query.ToListAsync();

            model.CommentsCount = allItems.Count;

            model.EntityId     = entityId;
            model.EntityTypeId = entityTypeId;

            return(View(model));
        }