예제 #1
0
        public async Task <Unit> Handle(CreatePostCommentCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <PostComment>(request);

            entity.UserId = int.Parse(_userService.GetUserId());

            _context.PostComments.Add(entity);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                var item = _context.PostComments
                           .Where(x => x.PostId == entity.PostId && x.UserId == entity.UserId && x.CreatedAt == entity.CreatedAt)
                           .FirstOrDefault();

                await _wallService.SendPostComment(entity.UserId, item.Id, item.PostId, item.Content);

                var recipientId = _context.Posts.Find(entity.PostId)?.UserId;

                if (recipientId != null)
                {
                    var setting = _context.UserNotificationSettings
                                  .Where(x => x.UserId == recipientId)
                                  .FirstOrDefault()
                                  .PostComments;

                    if (setting)
                    {
                        if (entity.UserId != recipientId)
                        {
                            var command = new CreateNotificationCommand
                            {
                                UserId           = entity.UserId,
                                RecipientId      = recipientId.Value,
                                NotificationType = NotificationType.PostComment
                            };

                            await _mediator.Send(command);
                        }
                    }
                }
            }

            return(Unit.Value);
        }