示例#1
0
        public static Post ToPost(IUserRepo userRepo, ICommentRepo commentRepo, PostApiModel apiModel)
        {
            apiModel.Content.EnforceNoSpecialCharacters(nameof(apiModel.Content));

            // if status is not null, filter out any non-file allowed characters
            if (!apiModel.PictureUrl.IsNullOrEmpty())
            {
                apiModel.PictureUrl.EnforceNoSpecialCharacters(nameof(apiModel.PictureUrl));
            }
            var            user         = userRepo.GetUserByIdAsync(apiModel.User.Id).Result;
            List <Comment> comments     = null;
            List <User>    likedByUsers = null;

            if (apiModel.Comments is not null && apiModel.Comments.Any())
            {
                var commentIds = apiModel
                                 .Comments
                                 .Select(c => c.Id)
                                 .ToList();

                comments = commentRepo.GetCommentsByIdsAsync(commentIds)
                           .Result
                           .ToList();
            }

            if (apiModel.LikedByUserIds is not null && apiModel.LikedByUserIds.Any())
            {
                likedByUsers = userRepo.GetUsersByIdsAsync(apiModel.LikedByUserIds)
                               .Result
                               .ToList();
            }

            comments ??= new List <Comment>();
            likedByUsers ??= new List <User>();

            return(new Post
            {
                Id = apiModel.Id,
                Content = apiModel.Content,
                CreatedAt = apiModel.CreatedAt,
                Picture = apiModel.PictureUrl,
                User = user,
                LikedByUsers = likedByUsers,
                Comments = comments
            });
        }
示例#2
0
        public static Comment ToComment(ICommentRepo commentRepo, IUserRepo userRepo, IPostRepo postRepo, CommentApiModel apiModel)
        {
            // no special characters are allowed
            apiModel.Content.EnforceNoSpecialCharacters(nameof(apiModel.Content));

            var user = userRepo.GetUserByIdAsync(apiModel.User.Id)
                       .Result;

            user.NullCheck(nameof(user));

            var post = postRepo.GetPostByIdAsync(apiModel.PostId)
                       .Result;

            post.NullCheck(nameof(user));

            List <Comment> childComments = null;

            if (apiModel.ChildCommentIds is not null && apiModel.ChildCommentIds.Any())
            {
                childComments = commentRepo.GetCommentsByIdsAsync(apiModel.ChildCommentIds)
                                .Result
                                .ToList();
            }

            childComments ??= new List <Comment>();

            return(new Comment
            {
                Id = apiModel.Id,
                Content = apiModel.Content,
                CreatedAt = apiModel.CreatedAt,
                User = user,
                Post = post,
                ChildrenComments = childComments
            });
        }