public async Task SubmitAsyncShould_SetsTheCorrectPropsAnd_SavesEntityInDB() { const string commentContent = "Content"; //Arrange var user = UserCreator.Create(); this.db.Add(user); var article = ArticleCreator.Create(user.Id); this.db.Add(article); await this.db.SaveChangesAsync(); var service = new ArticleCommentService(this.db); //Act var result = await service.SubmitAsync <CommentListingModel>(article.Id, user.Id, commentContent); result.Should().BeOfType <CommentListingModel>(); result.ArticleAuthor.Should().BeEquivalentTo(user.UserName); result.ArticleId.Should().Be(article.Id); result.Id.Should().Be(result.Id); }
private ArticleViewModel GetFullArticle(Article article) { var likes = ArticleLikeService.GetArticleLikes(article); var comments = ArticleCommentService.GetArticleComments(article); return(new ArticleViewModel(article, likes, comments)); }
public async Task GetAsyncShould_ReturnsCorrectModel() { //Arrange var user = UserCreator.Create(); this.db.Add(user); var article = ArticleCreator.Create(user.Id); this.db.Add(article); var comment = this.CreateArticleComment(article.Id, user.Id); this.db.Add(comment); await this.db.SaveChangesAsync(); var service = new ArticleCommentService(this.db); //Act var result = await service.GetAsync <CommentListingModel>(article.Id); //Assert result.Should().BeOfType <CommentListingModel>(); result.Id.Should().Be(article.Id); }
public async Task DeleteAsyncShould_DeletesCorrectCommentIf_CommentIdExists() { //Arrange var user = UserCreator.Create(); this.db.Add(user); var article = ArticleCreator.Create(user.Id); this.db.Add(article); var comment = this.CreateArticleComment(article.Id, user.Id); var secondComment = this.CreateArticleComment(article.Id, user.Id); this.db.AddRange(comment, secondComment); await this.db.SaveChangesAsync(); var service = new ArticleCommentService(this.db); var commentsCount = this.db.Comments.Count(); //Act var result = await service.DeleteAsync(comment.Id); //Assert result.Should().BeTrue(); this.db.Comments.Count().Should().Be(commentsCount - 1); this.db.Find <Comment>(comment.Id).Should().BeNull(); }
public HomeController(ArticleService articleService, UserManager <ApplicationUser> userManager, UserLikeService userLikeService, ArticleCommentService articleCommentService) { _articleService = articleService; _userLikeService = userLikeService; _articleCommentService = articleCommentService; _userManager = userManager; }
public ArticleController(ArticleContentService _articleContentService, ArticleCommentService _articleCommentService, ArticleAttentionService _articleAttentionService, ArticleCollectService _articleCollectService, ArticleCategoryService _articleCategoryService) { this.articleContentService = _articleContentService; this.articleCommentService = _articleCommentService; this.articleAttentionService = _articleAttentionService; this.articleCollectService = _articleCollectService; this.articleCategoryService = _articleCategoryService; }
public async Task DeleteAsyncShould_ReturnsFalseIf_CommentIdDoesNotExists() { const int notExistingCommentId = 556698; var service = new ArticleCommentService(this.db); var result = await service.DeleteAsync(notExistingCommentId); result.Should().Be(false); }
private List <ArticleViewModel> GetFullArticles(GetList query) { var isLoggedIn = false; User user = null; if (User.Identity.Name != null) { user = UserService.Get(long.Parse(User.Identity.Name)); if (user != null) { isLoggedIn = true; } } var result = new List <ArticleViewModel>(); var articles = ArticleService.GetAllArticles() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList(); articles.ForEach(article => { var likes = ArticleLikeService.GetAll() .Where(x => x.Article == article) .ToList(); var comments = ArticleCommentService.GetAll() .Where(x => x.Article == article) .ToList(); var isLiked = false; if (isLoggedIn) { if (likes.FirstOrDefault(x => x.Author == user) != null) { isLiked = true; } } var articleViewModel = new ArticleViewModel(article, likes, comments, isLiked); articleViewModel.Author.Rating = ReviewService.GetSpecialistRating(article.Author); result.Add(articleViewModel); }); return(result); }
private ArticleViewModel GetFullArticle(long id) { var article = ArticleService.Get(id); if (article == null) { return(null); } var isLoggedIn = false; User user = null; if (User.Identity.Name != null) { user = UserService.Get(long.Parse(User.Identity.Name)); if (user != null) { isLoggedIn = true; } } var likes = ArticleLikeService.GetAll() .Where(x => x.Article == article) .ToList(); var comments = ArticleCommentService.GetArticleComments(article); var isLiked = false; if (isLoggedIn) { if (likes.FirstOrDefault(x => x.Author == user) != null) { isLiked = true; } } var result = new ArticleViewModel(article, likes, comments, isLiked); result.Author.Rating = ReviewService.GetSpecialistRating(article.Author); return(result); }
public async Task SubmitAsyncShould_ReturnsNullIf_NoArticleFound() { const string commentContent = "Content"; //Arrange var user = UserCreator.Create(); this.db.Add(user); await this.db.SaveChangesAsync(); var service = new ArticleCommentService(this.db); //Act var result = await service.SubmitAsync <CommentListingModel>(NotExistingArticleId, user.Id, commentContent); //Assert result.Should().BeNull(); }
public async Task AllAsyncShould_ReturnsCorrectComments() { //Arrange var user = UserCreator.Create(); this.db.Add(user); var article = ArticleCreator.Create(user.Id); this.db.Add(article); var comment = this.CreateArticleComment(article.Id, user.Id); var secondComment = this.CreateArticleComment(NotExistingArticleId, user.Id); this.db.AddRange(comment, secondComment); await this.db.SaveChangesAsync(); var service = new ArticleCommentService(this.db); //Act var result = (await service.AllAsync <CommentListingModel>(article.Id)).ToList(); var expectedCommentsIds = await this.db.Comments .Where(c => c.ArticleId == article.Id) .OrderByDescending(c => c.PostedOn) .Select(x => x.Id).ToListAsync(); var expectedCount = await this.db.Comments.Where(c => c.ArticleId == article.Id).CountAsync(); //Assert result.Should().AllBeOfType <CommentListingModel>(); result.Should().HaveCount(expectedCount); result.Select(x => x.Id).Should().BeEquivalentTo(expectedCommentsIds); }
public IActionResult CommentArticle([FromBody] CreateArticleCommentRequest request, long id) { var article = ArticleService.Get(id); if (article == null) { return(NotFound(new ResponseModel { Success = false, Message = "Статья не найдена" })); } User user = null; if (User.Identity.Name != null) { user = UserService.Get(long.Parse(User.Identity.Name)); } if (user == null) { return(NotFound(new ResponseModel { Success = false, Message = "Пользователь не найден" })); } ArticleComment parentComment = null; if (request.IsReply && request.ParentCommentID != 0) { parentComment = ArticleCommentService.Get(request.ParentCommentID); if (parentComment == null) { return(NotFound(new ResponseModel { Success = false, Message = "Комментарий не найден" })); } } if (request.Text.Length > 65535) { return(BadRequest(new ResponseModel { Success = false, Message = "Длина комментария больше допустимой" })); } var comment = new ArticleComment { Article = article, Author = user, IsReply = request.IsReply, ParentComment = parentComment, Text = request.Text, Date = DateTime.UtcNow }; ArticleCommentService.Create(comment); return(Ok(new DataResponse <ArticleCommentViewModel> { Data = new ArticleCommentViewModel(comment) })); }
private List <ArticleViewModel> GetFullArticles(GetArticlesList query) { var isLoggedIn = false; User user = null; if (User.Identity.Name != null) { user = UserService.Get(long.Parse(User.Identity.Name)); if (user != null) { isLoggedIn = true; } } var result = new List <ArticleViewModel>(); var articles = new List <Article>(); if (query.OrderBy == OrderBy.ASC) { switch (query.SortBy) { case ArticlesSort.Comments: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderBy(x => ArticleCommentService.GetArticleCommentsCount(x)) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } case ArticlesSort.Likes: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderBy(x => ArticleLikeService.GetArticleLikesCount(x)) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } case ArticlesSort.Date: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderBy(x => x.Date) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } default: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderBy(x => x.Date) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } } } else { switch (query.SortBy) { case ArticlesSort.Comments: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderByDescending(x => ArticleCommentService.GetArticleCommentsCount(x)) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } case ArticlesSort.Likes: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderByDescending(x => ArticleLikeService.GetArticleLikesCount(x)) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } case ArticlesSort.Date: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderByDescending(x => x.Date) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } default: { articles.AddRange(ArticleService.GetAll() .Where(article => article.ModerationStatus == ArticleModerationStatus.Accepted).ToList() .OrderByDescending(x => x.Date) .Skip((query.PageNumber - 1) * query.PageSize) .Take(query.PageSize) .ToList()); break; } } } articles.ForEach(article => { var likes = ArticleLikeService.GetAll() .Where(x => x.Article == article) .ToList(); var comments = ArticleCommentService.GetAll() .Where(x => x.Article == article) .ToList(); var isLiked = false; if (isLoggedIn) { if (likes.FirstOrDefault(x => x.Author == user) != null) { isLiked = true; } } var articleViewModel = new ArticleViewModel(article, likes, comments, isLiked); articleViewModel.Author.Rating = ReviewService.GetSpecialistRating(article.Author); result.Add(articleViewModel); }); return(result); }