Skip to content

Commit

Permalink
Post mapper handles promises
Browse files Browse the repository at this point in the history
  • Loading branch information
devbenho committed May 30, 2024
1 parent 12034d4 commit b769c5e
Showing 1 changed file with 34 additions and 26 deletions.
60 changes: 34 additions & 26 deletions server/src/infrastructure/posts/post.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,90 @@
import { LOGGER } from '@/web/rest/logger';
import { Comment, LikePost, Post } from '@domain/entities';
import { POST_STATUS } from '@domain/eums/post-status.enum';
import { Post } from '@domain/entities';
import { PostPersistence } from '@infrastructure/posts';
import { UserMapper } from '@infrastructure/users';
import { CommentMapper } from '@infrastructure/comments';
import { LikePostMapper } from '@infrastructure/like-posts';
import { log } from 'console';

class PostMapper {
public static toDomain(postPersistence: PostPersistence): Post {
public static async toDomain(
postPersistence: PostPersistence,
): Promise<Post> {
const {
id,
title,
content,
authorId,
author,
comments = [], // Default to empty array
likes = [], // Default to empty array
comments = [],
likes = [],
createdAt,
deletedAt,
updatedAt,
status,
} = postPersistence;

LOGGER.info('PostMapper.toDomain', postPersistence);

const resolvedComments = await comments;
const resolvedLikes = await likes;

return new Post(
id,
title,
content,
authorId,
UserMapper.toDomain(author),
likes.length ? likes.map(like => LikePostMapper.toDomain(like)) : [],
comments.length
? comments.map(comment => CommentMapper.toDomain(comment))
: [],
status as POST_STATUS,
await UserMapper.toDomain(author),
await Promise.all(
resolvedLikes.map(like => LikePostMapper.toDomain(like)),
),
await Promise.all(
resolvedComments.map(comment => CommentMapper.toDomain(comment)),
),
POST_STATUS[status.toUpperCase() as keyof typeof POST_STATUS], // Convert to POST_STATUS enum
createdAt,
updatedAt,
deletedAt,
);
}

public static toPersistence(post: Post): PostPersistence {
public static async toPersistence(post: Post): Promise<PostPersistence> {
const {
id,
title,
content,
authorId,
author,
comments = [], // Default to empty array
likes = [], // Default to empty array
comments = [],
likes = [],
createdAt,
deletedAt,
updatedAt,
status,
} = post;

const postPersistence = new PostPersistence();

if (id != null) {
postPersistence.id = id;
}
postPersistence.title = title;
postPersistence.content = content;
postPersistence.authorId = authorId;
postPersistence.author = UserMapper.toPersistence(author);
postPersistence.author = await UserMapper.toPersistence(author);

if (post.comments.length) {
postPersistence.comments = comments.map(comment =>
CommentMapper.toPersistence(comment),
);
}
if (post.likes.length) {
postPersistence.likes = likes.map(like =>
LikePostMapper.toPersistence(like),
);
}
postPersistence.comments = Promise.resolve(
await Promise.all(
comments.map(comment => CommentMapper.toPersistence(comment)),
),
);
postPersistence.likes = Promise.resolve(
await Promise.all(likes.map(like => LikePostMapper.toPersistence(like))),
);

postPersistence.createdAt = createdAt;
postPersistence.deletedAt = deletedAt;
postPersistence.status = status as POST_STATUS;
postPersistence.status = status.toLowerCase(); // Convert to string
return postPersistence;
}
}
Expand Down

0 comments on commit b769c5e

Please sign in to comment.