Skip to content

Commit

Permalink
Modifying post mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
devbenho committed Jun 1, 2024
1 parent 0209b3f commit edfa547
Showing 1 changed file with 10 additions and 25 deletions.
35 changes: 10 additions & 25 deletions server/src/infrastructure/posts/post.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { PostPersistence } from '@infrastructure/posts';
import { POST_STATUS } from '@domain/eums/post-status.enum';
import { Post, User } from '@domain/entities';
import { Comment, LikePost, Post, User } from '@domain/entities';
import { UserMapper, UserPersistence } from '@infrastructure/users';
import { CommentMapper, CommentPersistence } from '@infrastructure/comments';
import {
LikePostMapper,
LikePostPersistence,
} from '@infrastructure/like-posts';
import { MapperConfig } from '@infrastructure/shared/persistence/mapper.config';
import { Nullable } from '@domain/shared/types';

class PostMapper {
public static toDomain(
Expand Down Expand Up @@ -37,44 +38,28 @@ class PostMapper {
);
}

public static async toPersistence(
public static toPersistence(
domain: Post,
config: MapperConfig = {},
domainEntities?: {
author?: User;
likes?: LikePost[];
comments?: Comment[];
},
): Promise<PostPersistence> {
const postPersistence = new PostPersistence();

if (domain.id) {
postPersistence.id = domain.id;
}

postPersistence.title = domain.title;
postPersistence.content = domain.content;
postPersistence.authorId = domain.authorId;

if (config.includeAuthor) {
postPersistence.author = UserMapper.toPersistence(domain.author as User);
}

if (config.includeLikes) {
postPersistence.likes = Promise.resolve(
await Promise.all(
domain.likes.map(like => LikePostMapper.toPersistence(like)),
),
);
}

if (config.includeComments) {
postPersistence.comments = Promise.resolve(
await Promise.all(
domain.comments.map(comment => CommentMapper.toPersistence(comment)),
),
);
}

postPersistence.status = domain.status.toLowerCase();
postPersistence.createdAt = domain.createdAt;
postPersistence.deletedAt = domain.deletedAt;

return postPersistence;
return Promise.resolve(postPersistence);
}
}

Expand Down

0 comments on commit edfa547

Please sign in to comment.