Exemple #1
0
        public async Task GetNewsFeedCommentByIdShouldReturnCorrectlyAsync()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repositoryArticle         = new EfDeletableEntityRepository <Article>(dbContext);
            var repositoryNewsFeedPost    = new EfDeletableEntityRepository <NewsFeedPost>(dbContext);
            var repositoryNewsFeedComment = new EfDeletableEntityRepository <NewsFeedComment>(dbContext);
            var repositoryCategory        = new EfDeletableEntityRepository <Category>(dbContext);
            var repositoryComment         = new EfDeletableEntityRepository <Comment>(dbContext);

            var service = new AdministratorService(repositoryNewsFeedPost, repositoryArticle, repositoryNewsFeedComment, repositoryCategory, repositoryComment);

            var newsFeedComment = new NewsFeedComment
            {
                Id      = 1,
                Content = "test",
                UserId  = "1",
            };
            await repositoryNewsFeedComment.AddAsync(newsFeedComment);

            await repositoryNewsFeedComment.SaveChangesAsync();

            var comment = await service.GetNewsFeedCommentAsync(newsFeedComment.Id);

            Assert.IsType <NewsFeedComment>(comment);
            Assert.Equal(newsFeedComment, comment);
        }
Exemple #2
0
        public async Task GetByIdCommentAsyncShouldReturnCorrectComment()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repositoryArticle         = new EfDeletableEntityRepository <Article>(dbContext);
            var repositoryNewsFeedPost    = new EfDeletableEntityRepository <NewsFeedPost>(dbContext);
            var repositoryNewsFeedComment = new EfDeletableEntityRepository <NewsFeedComment>(dbContext);
            var repositoryCategory        = new EfDeletableEntityRepository <Category>(dbContext);
            var repositoryComment         = new EfDeletableEntityRepository <Comment>(dbContext);

            var commentOne = new NewsFeedComment
            {
                Content        = "Test",
                UserId         = "1",
                NewsFeedPostId = 1,
            };

            await repositoryNewsFeedComment.AddAsync(commentOne);

            await repositoryNewsFeedComment.SaveChangesAsync();

            AutoMapperConfig.RegisterMappings(typeof(NewsFeedPostCommentViewModelTest).GetTypeInfo().Assembly);

            var service = new AdministratorService(repositoryNewsFeedPost, repositoryArticle, repositoryNewsFeedComment, repositoryCategory, repositoryComment);

            var comment = service.GetByIdCommentAsync <NewsFeedPostCommentViewModelTest>(commentOne.Id);

            Assert.Equal(commentOne.Id, comment.Id);
        }
        public async Task <NewsFeedComment> UpdateCommentAsync(NewsFeedComment comment)
        {
            var currentComment = await this.newsFeedCommentRepository.All().FirstOrDefaultAsync(b => b.Id == comment.Id);

            currentComment.Content = comment.Content;

            await this.newsFeedCommentRepository.SaveChangesAsync();

            return(currentComment);
        }
Exemple #4
0
        public async Task <HttpResult> AddEditComment([FromBody] NewsFeedComment comment)
        {
            try
            {
                if (!Functions.UserLoggedIn(Request, out User user))
                {
                    throw new Exception("Not logged in!");
                }

                if (comment.AuthorId != user.Id)
                {
                    throw new Exception("Denied!");
                }

                if (comment.Comment.Length > 1024)
                {
                    throw new Exception("Comment too long!");
                }

                if (CheckSpam(user.Id, comment.PostId))
                {
                    throw new Exception("Spam Blocker: No more than 3 posts in a row!");
                }

                DateTime now = DateTime.Now;

                if (comment.Id > 1)
                {
                    NewsFeedComment updating = db.NewsFeedComment.Find(comment.Id);
                    Functions.CheckNull(updating);

                    updating.Comment = comment.Comment;

                    db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                    await db.SaveChangesAsync();
                }
                else
                {
                    comment.CreationDate = now;
                    comment.AuthorId     = user.Id;
                    comment.Status       = (int)PostStatus.Live;

                    db.NewsFeedComment.Add(comment);

                    await db.SaveChangesAsync();
                }

                return(new HttpResult(true, new NewsFeedCommentPublic(comment, user), ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
Exemple #5
0
        public async Task <HttpResult> ToggleDeleteComment([FromBody] NewsFeedCommentPublic comment)
        {
            try
            {
                bool isAdmin = Functions.AdminLoggedIn(Request, out _);

                //reject if no user logged in
                if (!Functions.UserLoggedIn(Request, out User author) && !isAdmin)
                {
                    throw new Exception("Not logged in!");
                }

                //reject if user is not admin and post does not belong to user
                if (!isAdmin && author != null && comment.AuthorId != author.Id)
                {
                    throw new Exception("Denied!");
                }

                NewsFeedComment updating = db.NewsFeedComment.Find(comment.Id);
                if (updating == null)
                {
                    throw new Exception("Nothing found!");
                }

                if (isAdmin)
                {
                    updating.Status = updating.Status == (int)PostStatus.DeletedByModerator ? (int)PostStatus.Live : (int)PostStatus.DeletedByModerator;
                }
                else
                {
                    updating.Status = updating.Status == (int)PostStatus.DeletedByUser ? (int)PostStatus.Live : (int)PostStatus.DeletedByUser;
                }

                db.Entry(updating).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

                await db.SaveChangesAsync();

                //if admin, need to get comment author for creating NewsFeedCommentPublic
                if (isAdmin)
                {
                    author = db.User.Find(updating.AuthorId);
                }

                return(new HttpResult(true, new NewsFeedCommentPublic(updating, author), ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
        public async Task CreateAsync(int newsFeedPostId, string userId, string content, int?parentId = null)
        {
            var comment = new NewsFeedComment
            {
                NewsFeedPostId = newsFeedPostId,
                UserId         = userId,
                Content        = content,
                ParentId       = parentId,
                CreatedOn      = DateTime.UtcNow,
            };

            await this.commentsRepository.AddAsync(comment);

            await this.commentsRepository.SaveChangesAsync();
        }
Exemple #7
0
        public async Task <IActionResult> EditNewsFeedComment(EditCommentViewModel model)
        {
            var sanitizer = new HtmlSanitizer();

            var content = sanitizer.Sanitize(model.Content);

            var comment = new NewsFeedComment
            {
                Id      = model.Id,
                Content = model.Content,
            };

            await this.administratorService.UpdateCommentAsync(comment);

            return(this.RedirectToAction(nameof(this.NewsFeedComments)));
        }
        public NewsFeedCommentPublic(NewsFeedComment c, User u)
        {
            CreationDate = c.CreationDate;
            Id           = c.Id;
            AuthorId     = c.AuthorId;
            Comment      = c.Comment;
            PostId       = c.PostId;
            Status       = c.Status;
            AuthorName   = u.Username;
            AuthorPic    = string.IsNullOrEmpty(u.ProfilePic) ? "/dist/images/users/default-user.jpg" : u.ProfilePic;
            Upvotes      = c.Upvotes ?? "";

            if (c.Status == (int)PostStatus.DeletedByModerator)
            {
                Comment = "Deleted by moderator";
            }
            else if (c.Status == (int)PostStatus.DeletedByUser)
            {
                Comment = "Deleted by user";
            }
        }
        public async Task DeleteNewsFeedCommentAsync(NewsFeedComment comment)
        {
            this.newsFeedCommentRepository.Delete(comment);

            await this.newsFeedCommentRepository.SaveChangesAsync();
        }