private async Task PushNumberOfPostReactions(SetPostReactionViewModel postReaction) { var connectionID = NotificationHub.GetConnectionID(postReaction.User.UserName); var reactionsCount = _postReactionService.GetReactionCount(postReaction.PostId); if (connectionID != null) { await _notificationHub.Clients.All.SendAsync("ReactionsCount", JsonConvert.SerializeObject(reactionsCount)); } }
private async Task UpdatePostReaction(SetPostReactionViewModel model, PostReaction userPostReaction) { var postReactions = _dataService.GetSet <PostReaction>(); if (userPostReaction.Type == model.Type) { postReactions.Remove(userPostReaction); } else { userPostReaction.Type = model.Type; } await _dataService.SaveDbAsync(); }
private async Task CreatePostReaction(SetPostReactionViewModel model) { var postReactions = _dataService.GetSet <PostReaction>(); var newReaction = new PostReaction { User = model.User, PostId = model.PostId, Type = model.Type }; await postReactions.AddAsync(newReaction); await _dataService.SaveDbAsync(); }
public async Task <int> SetReaction([FromBody] SetPostReactionViewModel model) { model.User = await _userService.GetUserByName(HttpContext.User.Identity.Name); await _postReactionService.SetReaction(model); var post = await _postsService.GetPost(model.PostId); if (post.Reactions.FirstOrDefault(x => x.UserId == model.User.Id) == null) { return(post.Reactions?.GroupBy(x => x.Type) .Sum(x => x.Count()) ?? 0); } //await PushNumberOfPostReactions(model); if (model.User.Id != post.CreatedBy.Id) { await _notificationService.CreateNotification( $"{model.User.GetDisplayName()} reacted to your post!", NotificationType.NewReaction, post.CreatedBy, model.PostId.ToString()); await PushNumberOfNotifications(post.CreatedBy); } if (model.User.Id != post.TargetUser.Id && post.TargetUser.Id != post.CreatedBy.Id) { await _notificationService.CreateNotification( $"{model.User.GetDisplayName()} reacted to a post on your wall!", NotificationType.NewReaction, post.TargetUser, model.PostId.ToString()); await PushNumberOfNotifications(post.TargetUser); } return(post.Reactions?.GroupBy(x => x.Type) .Sum(x => x.Count()) ?? 0); }
public async Task SetReaction(SetPostReactionViewModel model) { var post = await _dataService .GetSet <Post>() .Include(x => x.Reactions) .FirstOrDefaultAsync(x => x.Id == model.PostId); if (post == null) { return; // TODO: add error handling } var userPostReaction = post.Reactions.FirstOrDefault(x => x.UserId == model.User.Id); if (userPostReaction != null) { await UpdatePostReaction(model, userPostReaction); } else { await CreatePostReaction(model); } }