Skip to content

Commit

Permalink
Mapping between persistence and domain entities works for user and post
Browse files Browse the repository at this point in the history
  • Loading branch information
devbenho committed Jun 1, 2024
1 parent ed44ee0 commit 597da33
Show file tree
Hide file tree
Showing 18 changed files with 262 additions and 224 deletions.
Binary file modified server/db.sqlite3
Binary file not shown.
4 changes: 2 additions & 2 deletions server/src/contracts/dtos/comments/comment.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export class CommentResponseDto {
public author: UserResponseDto,
public postId: string,
public createdAt: Nullable<Date>,
) { }
) {}

public static fromEntity(entity: Comment): CommentResponseDto {
return new CommentResponseDto(
entity.id!,
entity.content,
UserResponseDto.fromEntity(entity.author),
UserResponseDto.fromEntity(entity.author as any),
entity.postId,
entity.createdAt,
);
Expand Down
2 changes: 1 addition & 1 deletion server/src/contracts/dtos/posts/post-details.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PostDetailsResponseDto {
entity.id ?? '',
entity.title,
entity.content,
UserResponseDto.fromEntity(entity.author),
UserResponseDto.fromEntity(entity.author ?? ({} as any)),
entity.createdAt,
entity.updatedAt,
);
Expand Down
2 changes: 1 addition & 1 deletion server/src/contracts/dtos/posts/post.response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PostResponseDto {
entity.id,
entity.title,
entity.content,
UserResponseDto.fromEntity(entity.author),
UserResponseDto.fromEntity(entity.author as any),
entity.comments ? entity.comments.map(CommentResponseDto.fromEntity) : [],
entity.createdAt,
entity.updatedAt,
Expand Down
2 changes: 1 addition & 1 deletion server/src/domain/entities/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Comment extends AuditableBaseEntity {
public postId: string,
public post: Post,
public authorId: string,
public author: User,
public author: Nullable<User>,
public content: string,
public replies: Reply[],
public likes: LikeComment[],
Expand Down
1 change: 0 additions & 1 deletion server/src/domain/entities/like-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class LikeComment extends AuditableBaseEntity {
public updatedBy: Nullable<string>,
public deletedAt: Nullable<Date>,
public deletedBy: Nullable<string>,

) {
super(id, createdAt, createdBy, updatedAt, updatedBy, deletedAt, deletedBy);
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/domain/entities/like-reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class LikeReply extends AuditableBaseEntity {
constructor(
public id: Nullable<string>,
public replyId: string,
public reply: Reply,
public reply: Nullable<Reply>,
public userId: string,
public user: User,
public user: Nullable<User>,
public createdAt: Date,
public createdBy: string,
public updatedAt: Nullable<Date>,
Expand Down
2 changes: 1 addition & 1 deletion server/src/domain/entities/posts/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Post extends AuditableBaseEntity {
public title: string,
public content: string,
public authorId: string,
public author: User,
public author: Nullable<User>,
public likes: LikePost[],
public comments: Comment[],
public status: POST_STATUS = POST_STATUS.DRAFT,
Expand Down
5 changes: 2 additions & 3 deletions server/src/domain/entities/reply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class Reply extends AuditableBaseEntity {
constructor(
public id: Nullable<string>,
public commentId: string,
public comment: Comment,
public comment: Nullable<Comment>,
public userId: string,
public user: User,
public user: Nullable<User>,
public content: string,
public likes: Nullable<LikeReply[]>,
public createdAt: Date,
Expand All @@ -19,7 +19,6 @@ class Reply extends AuditableBaseEntity {
public updatedBy: Nullable<string>,
public deletedAt: Nullable<Date>,
public deletedBy: Nullable<string>,

) {
super(id, createdAt, createdBy, updatedAt, updatedBy, deletedAt, deletedBy);
}
Expand Down
60 changes: 29 additions & 31 deletions server/src/infrastructure/comments/comment.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import { CommentPersistence } from '@infrastructure/comments';
import { Comment } from '@/domain/entities';
import { UserMapper } from '@infrastructure/users';
import { PostMapper } from '@infrastructure/posts';
import { ReplyMapper } from '@infrastructure/replies';
import { LikeCommentMapper } from '@infrastructure/like-comments';
import { UserMapper, UserPersistence } from '@infrastructure/users';
import { PostMapper, PostPersistence } from '@infrastructure/posts';
import { ReplyMapper, ReplyPersistence } from '@infrastructure/replies';
import {
LikeCommentMapper,
LikeCommentPersistence,
} from '@infrastructure/like-comments';

export class CommentMapper {
public static async toDomain(
public static toDomain(
commentPer: CommentPersistence,
): Promise<Comment> {
const replies = await Promise.all(
commentPer.replies.map(reply => ReplyMapper.toDomain(reply)),
);
const likes = await Promise.all(
commentPer.likes.map(like => LikeCommentMapper.toDomain(like)),
);
lazyEntities?: {
author?: UserPersistence;
user?: PostPersistence;
replies?: ReplyPersistence[];
likes?: LikeCommentPersistence[];
},
): Comment {
const replies = lazyEntities?.replies
? lazyEntities.replies.map(reply => ReplyMapper.toDomain(reply))
: [];
const likes = lazyEntities?.likes
? lazyEntities.likes.map(like => LikeCommentMapper.toDomain(like))
: [];

const author = lazyEntities?.user
? UserMapper.toDomain(lazyEntities.author as UserPersistence)
: null;

return new Comment(
commentPer.id,
commentPer.postId,
await PostMapper.toDomain(commentPer.post),
PostMapper.toDomain(commentPer.post),
commentPer.userId,
await UserMapper.toDomain(commentPer.user),
UserMapper.toDomain(commentPer.user),
commentPer.content,
replies,
likes,
Expand All @@ -31,28 +44,13 @@ export class CommentMapper {
);
}

public static async toPersistence(
comment: Comment,
): Promise<CommentPersistence> {
public static toPersistence(comment: Comment): Promise<CommentPersistence> {
const commentPersistence = new CommentPersistence();

if (comment.id != null) {
commentPersistence.id = comment.id;
}
commentPersistence.postId = comment.postId;
commentPersistence.userId = comment.authorId;
commentPersistence.content = comment.content;
commentPersistence.createdAt = comment.createdAt;
commentPersistence.deletedAt = comment.deletedAt;
commentPersistence.replies = await Promise.all(
comment.replies.map(reply => ReplyMapper.toPersistence(reply)),
);
commentPersistence.likes = await Promise.all(
comment.likes.map(like => LikeCommentMapper.toPersistence(like)),
);
commentPersistence.post = await PostMapper.toPersistence(comment.post);
commentPersistence.user = await UserMapper.toPersistence(comment.author);

return commentPersistence;
return Promise.resolve(commentPersistence);
}
}
38 changes: 30 additions & 8 deletions server/src/infrastructure/like-comments/like-comment.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { UserMapper } from '@infrastructure/users';
import { UserMapper, UserPersistence } from '@infrastructure/users';
import { LikeCommentPersistence } from './like-comment.persistence';
import { LikeComment } from '@domain/entities/like-comment';
import { CommentMapper } from '@infrastructure/comments/comment.mapper';
import { CommentMapper, CommentPersistence } from '@infrastructure/comments/';
import { User } from '@domain/entities/user';
import { Comment } from '@domain/entities/comment';

class LikeCommentMapper {
static async toDomain(
static toDomain(
likeCommentPer: LikeCommentPersistence,
): Promise<LikeComment> {
const comment = await CommentMapper.toDomain(likeCommentPer.comment);
const user = await UserMapper.toDomain(likeCommentPer.user);
lazyEntities?: {
comment?: CommentPersistence;
user?: UserPersistence;
},
): LikeComment {
const comment = lazyEntities?.comment
? CommentMapper.toDomain(lazyEntities.comment)
: CommentMapper.toDomain(likeCommentPer.comment);
const user = lazyEntities?.user
? UserMapper.toDomain(lazyEntities.user)
: UserMapper.toDomain(likeCommentPer.user);

return new LikeComment(
likeCommentPer.id,
Expand All @@ -25,8 +35,20 @@ class LikeCommentMapper {
);
}

static toPersistence(likeComment: LikeComment): LikeCommentPersistence {
return new LikeCommentPersistence();
static async toPersistence(
likeComment: LikeComment,
): Promise<LikeCommentPersistence> {
const likeCommentPersistence = new LikeCommentPersistence();

if (likeComment.id != null) {
likeCommentPersistence.id = likeComment.id;
}
likeCommentPersistence.commentId = likeComment.commentId;
likeCommentPersistence.userId = likeComment.userId;
likeCommentPersistence.createdAt = likeComment.createdAt;
likeCommentPersistence.deletedAt = likeComment.deletedAt;

return likeCommentPersistence;
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/src/infrastructure/like-posts/like-post.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { PostMapper } from '@infrastructure/posts/post.mapper';
import { LikePostPersistence } from './like-post.persistence';

class LikePostMapper {
static async toDomain(likePostPer: LikePostPersistence): Promise<LikePost> {
const post = await PostMapper.toDomain(likePostPer.post);
const user = await UserMapper.toDomain(likePostPer.user);
static toDomain(likePostPer: LikePostPersistence): LikePost {
const post = PostMapper.toDomain(likePostPer.post);
const user = UserMapper.toDomain(likePostPer.user);
return new LikePost(
likePostPer.id,
likePostPer.postId,
Expand Down
71 changes: 33 additions & 38 deletions server/src/infrastructure/like-replies/like-reply.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
import { UserMapper } from '@infrastructure/users';
import { UserMapper, UserPersistence } from '@infrastructure/users';
import { LikeReply, Reply, User } from '@domain/entities';
import { LikeReplyPersistence } from './like-reply.persistence';
import { ReplyMapper } from '@infrastructure/replies/reply.mapper';
import { ReplyMapper, ReplyPersistence } from '@infrastructure/replies';

class LikeReplyMapper {
static async toDomain(
static toDomain(
likeReplyPer: LikeReplyPersistence,
): Promise<LikeReply> {
const userPromise = likeReplyPer.user
? UserMapper.toDomain(likeReplyPer.user)
: null;
const replyPromise = likeReplyPer.reply
? await ReplyMapper.toDomain(await likeReplyPer.reply)
: null;
lazyEntities?: {
user?: UserPersistence;
reply?: ReplyPersistence;
},
): LikeReply {
const user = lazyEntities?.user
? UserMapper.toDomain(lazyEntities.user)
: likeReplyPer.user
? UserMapper.toDomain(likeReplyPer.user)
: null;

const [user, reply] = await Promise.all([userPromise, replyPromise]);
const reply = lazyEntities?.reply
? ReplyMapper.toDomain(lazyEntities.reply)
: null;

return {
id: likeReplyPer.id,
createdAt: likeReplyPer.createdAt,
updatedAt: likeReplyPer.updatedAt,
deletedAt: likeReplyPer.deletedAt,
user: user as User,
reply: reply as Reply,
replyId: likeReplyPer.replyId,
userId: likeReplyPer.userId,
createdBy: likeReplyPer.userId,
updatedBy: likeReplyPer.userId,
deletedBy: likeReplyPer.userId,
equals: (likeReply: LikeReply) => likeReply.id === likeReplyPer.id,
};
return new LikeReply(
likeReplyPer.id,
likeReplyPer.replyId,
reply,
likeReplyPer.userId,
user,
likeReplyPer.createdAt,
likeReplyPer.userId,
likeReplyPer.updatedAt,
likeReplyPer.userId,
likeReplyPer.deletedAt,
likeReplyPer.userId,
);
}

static async toPersistence(
likeReply: LikeReply,
): Promise<LikeReplyPersistence> {
return {
id: likeReply.id!,
createdAt: likeReply.createdAt,
updatedAt: likeReply.updatedAt!,
deletedAt: likeReply.deletedAt,
user: await UserMapper.toPersistence(likeReply.user),
reply: ReplyMapper.toPersistence(likeReply.reply),
replyId: likeReply.replyId,
userId: likeReply.userId,
};
static toPersistence(likeReply: LikeReply): Promise<LikeReplyPersistence> {
const likeReplyPersistence = new LikeReplyPersistence();

return Promise.resolve(likeReplyPersistence);
}
}

Expand Down
41 changes: 20 additions & 21 deletions server/src/infrastructure/posts/post.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import { PostPersistence } from '@infrastructure/posts';
import { POST_STATUS } from '@domain/eums/post-status.enum';
import { Post } from '@domain/entities';
import { UserMapper } from '@infrastructure/users';
import { CommentMapper } from '@infrastructure/comments';
import { LikePostMapper } from '@infrastructure/like-posts';
import { 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';

class PostMapper {
public static async toDomain(
public static toDomain(
persistence: PostPersistence,
config: MapperConfig = {},
): Promise<Post> {
const likes = config.includeLikes
? await (persistence.likes ?? Promise.resolve([]))
: [];
const comments = config.includeComments
? await (persistence.comments ?? Promise.resolve([]))
: [];
const author = config.includeAuthor
? await UserMapper.toDomain(await persistence.author)
: null;
lazyEntities?: {
author?: UserPersistence;
likes?: LikePostPersistence[];
comments?: CommentPersistence[];
},
): Post {
const author = lazyEntities?.author;
const likes = lazyEntities?.likes ?? [];
const comments = lazyEntities?.comments ?? [];

return Post.create(
persistence.id,
persistence.title,
persistence.content,
persistence.authorId,
author as any,
await Promise.all(likes.map(like => LikePostMapper.toDomain(like))),
await Promise.all(
comments.map(comment => CommentMapper.toDomain(comment)),
),
likes.map(like => LikePostMapper.toDomain(like)),
comments.map(comment => CommentMapper.toDomain(comment)),
POST_STATUS[persistence.status.toUpperCase() as keyof typeof POST_STATUS],
persistence.createdAt,
persistence.updatedAt,
Expand All @@ -52,7 +51,7 @@ class PostMapper {
postPersistence.authorId = domain.authorId;

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

if (config.includeLikes) {
Expand Down
Loading

0 comments on commit 597da33

Please sign in to comment.