public static Post ToPost(PostEntity postEntity) { postEntity.NullCheck(nameof(postEntity)); postEntity.Content.EnforceNoSpecialCharacters(nameof(postEntity.Content)); var result = new Post { Id = postEntity.Id, Content = postEntity.Content, Picture = postEntity.Picture, CreatedAt = postEntity.CreatedAt, User = new User() { Id = postEntity.User.Id, FirstName = postEntity.User.FirstName, LastName = postEntity.User.LastName, ProfilePictureUrl = postEntity.User.ProfilePictureUrl }, LikedByUsers = new List <User>(), Comments = new List <Comment>() }; if (postEntity.Comments != null) { var comments = postEntity.Comments; foreach (var comment in comments) // See if there are any comments for the post { var newComment = new Comment() { Id = comment.Id, Content = comment.Content, User = new User() { Id = comment.User.Id, FirstName = comment.User.FirstName, LastName = comment.User.LastName, ProfilePictureUrl = comment.User.ProfilePictureUrl }, CreatedAt = comment.CreatedAt, ChildrenComments = new List <Comment>() }; if (comment.ChildrenComments != null) { foreach (var child in comment.ChildrenComments) { var newChild = new Comment() { Id = child.Id, Content = child.Content, User = new User() { Id = child.User.Id, ProfilePictureUrl = child.User.ProfilePictureUrl, FirstName = child.User.FirstName, LastName = child.User.LastName } }; newComment.ChildrenComments.Add(newChild); } } result.Comments.Add(newComment); } } if (postEntity.Likes != null) { var likes = postEntity.Likes; foreach (var like in likes) { var newUser = new User() { Id = like.User.Id, ProfilePictureUrl = like.User.ProfilePictureUrl, FirstName = like.User.FirstName, LastName = like.User.LastName }; result.LikedByUsers.Add(newUser); } ; } return(result); }