示例#1
0
        public async Task GetNewsFeedPostShouldReturnCorrectlyAsync()
        {
            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 post = new NewsFeedPost
            {
                Id      = 1,
                Content = "test",
                UserId  = "1",
            };


            await repositoryNewsFeedPost.AddAsync(post);

            await repositoryNewsFeedPost.SaveChangesAsync();

            var result = await service.GetNewsFeedPostAsync(post.Id);

            Assert.IsType <NewsFeedPost>(result);
        }
示例#2
0
        public async Task GetByIdNewsFeedPostAsyncShouldReturnCorrectPost()
        {
            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 newsFeedPost = new NewsFeedPost
            {
                Id      = 1,
                Content = "test",
                UserId  = "1",
            };
            await repositoryNewsFeedPost.AddAsync(newsFeedPost);

            await repositoryNewsFeedPost.SaveChangesAsync();

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

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

            var post = await service.GetByIdNewsFeedPostAsync <NewsFeedPostByIdViewModelTest>(newsFeedPost.Id);

            Assert.Equal(newsFeedPost.Id, post.Id);
        }
示例#3
0
        public async Task GetByIdShouldReturnCorrectPost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repositoryNewsFeedPost = new EfDeletableEntityRepository <NewsFeedPost>(dbContext);
            var applicationUser        = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var pictureRepository      = new EfRepository <UserProfilePicture>(dbContext);
            var repositoryComment      = new EfDeletableEntityRepository <NewsFeedComment>(dbContext);
            var coverPictureRepository = new EfRepository <UserCoverPicture>(dbContext);
            var friendRepository       = new EfDeletableEntityRepository <Friend>(dbContext);

            var post = new NewsFeedPost
            {
                Id      = 1,
                Content = "test",
                UserId  = "1",
            };

            await repositoryNewsFeedPost.AddAsync(post);

            await repositoryNewsFeedPost.SaveChangesAsync();

            var service = new NewsFeedService(repositoryNewsFeedPost, applicationUser, pictureRepository, repositoryComment, coverPictureRepository, friendRepository);

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

            var postResult = await service.GetByIdAsync <GetByIdTestViewModel>(post.Id);

            Assert.Equal(post.Id, postResult.Id);
        }
示例#4
0
        public void GetPostsCountByUserShouldReturnCorrectCount()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;
            var dbContext = new ApplicationDbContext(options);

            var repositoryNewsFeedPost = new EfDeletableEntityRepository <NewsFeedPost>(dbContext);
            var applicationUser        = new EfDeletableEntityRepository <ApplicationUser>(dbContext);
            var pictureRepository      = new EfRepository <UserProfilePicture>(dbContext);
            var repositoryComment      = new EfDeletableEntityRepository <NewsFeedComment>(dbContext);
            var coverPictureRepository = new EfRepository <UserCoverPicture>(dbContext);
            var friendRepository       = new EfDeletableEntityRepository <Friend>(dbContext);

            var post = new NewsFeedPost
            {
                Content = "test",
                UserId  = "1",
            };

            var secondPost = new NewsFeedPost
            {
                Content = "testTwo",
                UserId  = "1",
            };

            var service = new NewsFeedService(repositoryNewsFeedPost, applicationUser, pictureRepository, repositoryComment, coverPictureRepository, friendRepository);

            var postResult = service.CreateAsync(post.Content, post.UserId);

            var secondPostResult = service.CreateAsync(secondPost.Content, secondPost.UserId);

            var postCount = service.GetPostsCountByUser(post.UserId);

            Assert.Equal(2, postCount);
        }
示例#5
0
        public async Task <HttpResult> DeleteNewsfeedPost([FromRoute] int postID)
        {
            try
            {
                if (!Functions.AdminLoggedIn(Request, out _))
                {
                    throw new Exception("Not logged in!");
                }

                NewsFeedPost deleting = await db.NewsFeedPost.FindAsync(postID);

                if (deleting == null)
                {
                    throw new Exception("Post not found!");
                }

                db.NewsFeedPost.Remove(deleting);

                await db.SaveChangesAsync();

                return(new HttpResult(true, null, ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
        public async Task <NewsFeedPost> UpdateNewsFeedPostAsync(NewsFeedPost post)
        {
            var posts = await this.newsFeedRepository.All().FirstOrDefaultAsync(b => b.Id == post.Id);

            posts.Content = post.Content;

            await this.articleRepository.SaveChangesAsync();

            return(posts);
        }
        public async Task <NewsFeedPost> UpdateAsync(NewsFeedPost newsFeedPost)
        {
            var newPosts = await this.newsFeedRepository.All().FirstOrDefaultAsync(a => a.Id == newsFeedPost.Id);

            newPosts.Content = newsFeedPost.Content;

            await this.newsFeedRepository.SaveChangesAsync();

            return(newsFeedPost);
        }
        public async Task <int> CreateAsync(string content, string userId)
        {
            var timeLinePost = new NewsFeedPost
            {
                Content = content,
                UserId  = userId,
            };

            await this.newsFeedRepository.AddAsync(timeLinePost);

            await this.newsFeedRepository.SaveChangesAsync();

            return(timeLinePost.Id);
        }
示例#9
0
        public async Task <IActionResult> EditNewsFeedPost(EditPostInputModel model)
        {
            var sanitizer = new HtmlSanitizer();

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

            var post = new NewsFeedPost
            {
                Id      = model.Id,
                Content = content,
            };

            await this.administratorService.UpdateNewsFeedPostAsync(post);

            return(this.RedirectToAction(nameof(this.NewsFeedPost)));
        }
示例#10
0
        public async Task <HttpResult> EditNewsfeedPost([FromBody] NewsFeedPost post)
        {
            try
            {
                if (!Functions.AdminLoggedIn(Request, out _))
                {
                    throw new Exception("Not logged in!");
                }

                DateTime now = DateTime.Now;

                if (post.Id > 1)
                {
                    NewsFeedPost updating = db.NewsFeedPost.Find(post.Id);
                    Functions.CheckNull(updating);

                    updating.ModifiedDate = now;
                    updating.Title        = post.Title;
                    updating.ImageUrl     = post.ImageUrl;
                    updating.VideoUrl     = post.VideoUrl;
                    updating.Content      = post.Content;
                    updating.PostDate     = post.PostDate;
                    updating.Status       = post.Status;

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

                    await db.SaveChangesAsync();
                }
                else
                {
                    post.CreationDate = now;
                    post.ModifiedDate = now;
                    post.Status       = (int)Enums.ItemStatus.Live;

                    db.NewsFeedPost.Add(post);

                    await db.SaveChangesAsync();
                }

                return(new HttpResult(true, post, ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
示例#11
0
        public IEnumerable <NewsFeedPost> NewsFeedPostSeeder(int id, DateTime createdOn)
        {
            List <NewsFeedPost> newsFeedPosts = new List <NewsFeedPost>();

            for (int i = 1; i <= id; i++)
            {
                var newsFeedPost = new NewsFeedPost
                {
                    Id        = i,
                    Content   = i.ToString(),
                    UserId    = i.ToString(),
                    CreatedOn = createdOn,
                };

                newsFeedPosts.Add(newsFeedPost);
            }

            return(newsFeedPosts);
        }
示例#12
0
        public async Task UpdateNewsFeedPostShouldWorkCorrectlyAsync()
        {
            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 post = new NewsFeedPost
            {
                Id      = 1,
                Content = "test",
                UserId  = "1",
            };

            await repositoryNewsFeedPost.AddAsync(post);

            await repositoryNewsFeedPost.SaveChangesAsync();

            var postUpdate = new NewsFeedPost
            {
                Id      = 1,
                Content = "testtttt",
                UserId  = "1",
            };

            await service.UpdateNewsFeedPostAsync(postUpdate);

            var postAfterUpdate = await repositoryNewsFeedPost.All().FirstOrDefaultAsync(a => a.Id == postUpdate.Id);

            Assert.Equal(postUpdate.Content, postAfterUpdate.Content);
        }
        public async Task <IActionResult> EditNewsFeedPost(EditPostInputModel model)
        {
            var user = await this.userManager.GetUserAsync(this.User);

            if (!await this.newsFeedService.ExistsAndOwnerAsync(model.Id, user.Id))
            {
                return(this.RedirectToAction(nameof(this.NotOwner)));
            }

            var sanitizer = new HtmlSanitizer();

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

            var post = new NewsFeedPost
            {
                Id      = model.Id,
                Content = content,
            };

            await this.newsFeedService.UpdateAsync(post);

            return(this.RedirectToAction(nameof(this.NewsFeedContent)));
        }
示例#14
0
        public HttpResult NewsFeedPost([FromQuery] int postID)
        {
            try
            {
                bool adminLoggedIn = Functions.AdminLoggedIn(Request, out _);

                NewsFeedPost post = db.NewsFeedPost.Find(postID);

                if (post == null)
                {
                    throw new Exception("Post not found!");
                }

                if (post.Status != (int)ItemStatus.Live && !adminLoggedIn)
                {
                    throw new Exception("Denied!");
                }

                List <NewsFeedCommentPublic> comments = (from c in db.NewsFeedComment
                                                         join u in db.User on c.AuthorId equals u.Id
                                                         where c.PostId == post.Id
                                                         orderby c.CreationDate descending
                                                         select new { comment = c, author = u })
                                                        .AsEnumerable()
                                                        .Select(s => new NewsFeedCommentPublic(s.comment, s.author))
                                                        .ToList();

                return(new HttpResult(true, new NewsFeedPostSingle {
                    NewsFeedPost = post, Comments = comments
                }, ""));
            }
            catch (Exception e)
            {
                return(new HttpResult(false, null, Functions.ErrorMessage(e)));
            }
        }
        public async Task DeleteNewsFeedPostAsync(NewsFeedPost newsFeedPost)
        {
            this.newsFeedRepository.Delete(newsFeedPost);

            await this.newsFeedRepository.SaveChangesAsync();
        }