public CreationResultDto <long> CreateCommentary(CommentaryCreationDto newCommentary, long userId)
        {
            if (newCommentary == null)
            {
                throw new CommentaryException(CommentaryError.RequestIsEmpty);
            }
            if (string.IsNullOrWhiteSpace(newCommentary.Content))
            {
                throw new CommentaryException(CommentaryError.ContentIsNullOrWhitespace);
            }
            if (newCommentary.Content.Length > CommentaryRestrictions.ContentMaxLength)
            {
                throw new CommentaryException(CommentaryError.ContentMaxLengthExceeded);
            }

            var user = _dbContext.Users.FirstOrDefault(x => x.UserId == userId);

            if (user == null)
            {
                throw new UserException(UserError.DoesNotExist);
            }

            var post = _dbContext.Posts.Include(x => x.Author).FirstOrDefault(x => x.PostId == newCommentary.PostId);


            if (post == null)
            {
                throw new PostException(PostError.PostNotFound);
            }

            //Create
            var commentary = new Commentary
            {
                Author       = user,
                Content      = newCommentary.Content,
                Post         = post,
                CreationDate = DateTime.UtcNow
            };

            if (newCommentary.ResponseTo.HasValue)
            {
                var responseTo = _dbContext.Commentaries.FirstOrDefault(x => x.CommentaryId == newCommentary.ResponseTo.Value);
                if (responseTo == null)
                {
                    throw new CommentaryException(CommentaryError.DoesNotExist);
                }
                commentary.ResponseTo = responseTo;
            }

            _dbContext.Commentaries.Add(commentary);
            _dbContext.SaveChanges();

            //Crear notificación
            _notificationBusiness.CreateNotification(NotificationKind.Commentary, post.Author.UserId);


            return(new CreationResultDto <long>
            {
                Id = commentary.CommentaryId
            });
        }
Exemple #2
0
 public CreationResultDto <long> CreateCommentary(CommentaryCreationDto newCommentary)
 {
     return(_commentaryBusiness.CreateCommentary(newCommentary, UserId));
 }