public ActionResult Index(CommentItemModel NewComment) { Db db = new Db(); if (NewComment != null) { db.addComment(NewComment); } return(View(db.GetLastPost())); }
public async Task <IActionResult> CommentItem(int itemId, string value) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); int.TryParse(userId, out int requesterId); var model = new CommentItemModel { ItemId = itemId, UserId = requesterId, Value = value }; var result = await commentsManager.CommentItemAsync(model); return(Json(result)); }
public async Task SendComment(string itemId, string value) { if (!int.TryParse(itemId, out int parsedId)) { return; } var model = new CommentItemModel { ItemId = parsedId, UserId = sessionHelper.GetCurrentUserId(), Value = value }; var result = await commentsManager.CommentItemAsync(model); await Clients.Group(itemId.ToString()).SendAsync("OnCommentMade", result, Context.ConnectionId); }
public async Task <IActionResult> Index(int id) { var topic = await this.TopicService.Get(id); if (topic == null) { return(this.NotFound()); } ViewBag.Title = topic.Title; await this.TopicService.IncreaseVisitCount(id); var vm = new IndexViewModel(); vm.Topic = topic; vm.CommentList = new List <CommentItemModel>(); vm.CanOperate = this.ClientManager.IsAdmin || (this.ClientManager.IsLogin && topic.CreateUserID == this.ClientManager.CurrentUser.ID); var commentEntityList = await this.CommentService.QueryByTopic(id); List <UserVote> userVoteList = new List <UserVote>(); if (this.ClientManager.IsLogin) { vm.IsCollected = await this.UserCollectService.IsCollected(topic.ID, this.ClientManager.CurrentUser.ID); var commentIDList = commentEntityList.Select(t => t.ID).ToArray(); userVoteList = await this.UserVoteService.QueryByCommentAndUser(commentIDList, this.ClientManager.CurrentUser.ID); } foreach (var commentEntity in commentEntityList) { var model = new CommentItemModel(commentEntity); model.CanOperate = this.ClientManager.IsAdmin || (this.ClientManager.IsLogin && commentEntity.CreateUserID == this.ClientManager.CurrentUser.ID); model.Voted = userVoteList.Any(t => t.CommentID == commentEntity.ID); vm.CommentList.Add(model); } return(this.View(vm)); }
public async Task <CommentItemResult> CommentItemAsync(CommentItemModel commentItemModel) { var validRes = await validatorsStore.CommentItemModelValidator.ValidateAsync(commentItemModel); if (!validRes.Succeed) { return(new CommentItemResult(validRes)); } var comment = mapper.Map <CommentModel>(commentItemModel); await commentsCrudService.CreateAsync(comment); var itemComment = new ItemCommentModel { CommentId = comment.Id, ItemId = commentItemModel.ItemId }; await itemCommentCrudService.CreateAsync(itemComment); var result = mapper.Map <CommentItemResult>(comment); return(result); }
public async Task <List <CommentItemModel> > QueryLatest(int count) { string cacheKey = "Cache_Comment_Latest"; var result = await this.Cache.RetriveCacheAsync(cacheKey, async() => { var entityList = await this.BlogContext.Comments.AsNoTracking().Where(t => t.Status == Enums.CommentStatus.Approved) .OrderByDescending(t => t.ID) .Take(count) .ToListAsync(); if (entityList.Count == 0) { return(new List <CommentItemModel>()); } var topicIDList = entityList.Select(t => t.TopicID).ToArray(); var topicList = await this.BlogContext.Topics .Where(t => topicIDList.Contains(t.ID)) .Select(t => new TopicBasicModel { ID = t.ID, Title = t.Title, Alias = t.Alias }).ToListAsync(); var topicComments = await this.BlogContext.Comments.AsNoTracking().Where(t => topicIDList.Contains(t.TopicID)) .GroupBy(t => t.TopicID) .Select(g => new { TopicID = g.Key, Total = g.Count(), Approved = g.Count(t => t.Status == Enums.CommentStatus.Approved), Pending = g.Count(t => t.Status == Enums.CommentStatus.Pending), Reject = g.Count(t => t.Status == Enums.CommentStatus.Reject) }).ToListAsync(); var modelList = entityList.Select(entity => { var commentModel = new CommentItemModel { ID = entity.ID, Name = entity.Name, Content = entity.Content }; var topic = topicList.SingleOrDefault(t => t.ID == entity.TopicID); commentModel.Topic = new TopicBasicModel { ID = topic.ID, Title = topic.Title, Alias = topic.Alias, Comments = new CommentCountModel() }; var commentCount = topicComments.SingleOrDefault(t => t.TopicID == entity.TopicID); if (commentCount != null) { commentModel.Topic.Comments.Approved = commentCount.Approved; commentModel.Topic.Comments.Reject = commentCount.Reject; commentModel.Topic.Comments.Pending = commentCount.Pending; commentModel.Topic.Comments.Total = commentCount.Total; } return(commentModel); }); return(modelList.ToList()); }); return(result); }