Skip to content

Commit

Permalink
ToEntity() method added to CreateUserDto
Browse files Browse the repository at this point in the history
  • Loading branch information
devbenho committed May 24, 2024
1 parent 3666efd commit 24547fb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 49 deletions.
18 changes: 16 additions & 2 deletions server/src/application/auth/register/create-user.request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { UseCaseRequest } from '@application/shared';
import { TriggeredBy, TriggeredByUser } from '@domain/shared/entities';

import { User } from '@domain/entities';
import { Role } from '@domain/eums/role.enum';
class CreateUserDto extends UseCaseRequest {
constructor(
public triggeredBy: TriggeredBy,
Expand All @@ -10,6 +11,7 @@ class CreateUserDto extends UseCaseRequest {
public password: string,
public username: string,
public profilePicture?: string,
public role: Role = Role.USER,
) {
super(triggeredBy);
this.validate();
Expand Down Expand Up @@ -54,7 +56,19 @@ class CreateUserDto extends UseCaseRequest {
);
}


public static toEntity(dto: CreateUserDto): User {
return User.create(
undefined,
dto.firstName,
dto.lastName,
dto.email,
dto.username,
dto.password,
Role.USER,
new Date(),
dto.triggeredBy.who,
);
}
}

export { CreateUserDto };
39 changes: 14 additions & 25 deletions server/src/domain/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { Post } from './posts';
import { Reply } from './reply';

class User extends AuditableBaseEntity {

constructor(
public id: Nullable<string>,
public firstName: string,
Expand All @@ -25,47 +24,38 @@ class User extends AuditableBaseEntity {
public deletedAt: Nullable<Date>,
public deletedBy: Nullable<string>,
public comments: Nullable<Comment[]>,
public likeComments: Nullable<LikeComment[]>,
public likedComments: Nullable<LikeComment[]>,
public posts: Nullable<Post[]>,
public likedPosts: Nullable<LikePost[]>,
public replies: Nullable<Reply[]>,
public likeReplies: Nullable<LikeReply[]>,
public likedReplies: Nullable<LikeReply[]>,
) {
super(
id,
createdAt,
createdBy,
updatedAt,
updatedBy,
deletedAt,
deletedBy,
);
super(id, createdAt, createdBy, updatedAt, updatedBy, deletedAt, deletedBy);
}
isPasswordMatched(password: string): boolean {
return this.password === password;
}

public static create(
id: Nullable<string>,
id: Nullable<string> = undefined,
firstName: string,
lastName: string,
email: string,
username: string,
password: string,
role: Role = Role.USER,
profilePicture: Nullable<string>,
createdAt: Date,
createdBy: string,
updatedAt: Nullable<Date>,
updatedBy: Nullable<string>,
deletedAt: Nullable<Date>,
deletedBy: Nullable<string>,
comments: Nullable<Comment[]>,
likeComments: Nullable<LikeComment[]>,
posts: Nullable<Post[]>,
likedPosts: Nullable<LikePost[]>,
replies: Nullable<Reply[]>,
likeReplies: Nullable<LikeReply[]>,
updatedAt: Nullable<Date> = undefined,
updatedBy: Nullable<string> = undefined,
deletedAt: Nullable<Date> = undefined,
deletedBy: Nullable<string> = undefined,
comments: Nullable<Comment[]> = [],
likeComments: Nullable<LikeComment[]> = [],
posts: Nullable<Post[]> = [],
likedPosts: Nullable<LikePost[]> = [],
replies: Nullable<Reply[]> = [],
likeReplies: Nullable<LikeReply[]> = [],
): User {
return new User(
id,
Expand All @@ -89,7 +79,6 @@ class User extends AuditableBaseEntity {
likeReplies,
);
}

}

export { User };
55 changes: 39 additions & 16 deletions server/src/infrastructure/users/user.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { User } from '@domain/entities/user';
import { UserPersistence } from './user.persistence';
import { injectable } from 'inversify';
import { CommentMapper, CommentPersistence, LikeCommentMapper, LikeReplyMapper, PostMapper, ReplyMapper } from '..';
import { Comment } from '@domain/entities';
import {
CommentMapper,
CommentPersistence,
LikeCommentMapper,
LikeReplyMapper,
PostMapper,
ReplyMapper,
} from '..';
import { LikePostMapper } from '@infrastructure/like-posts/like-post.mapper';
@injectable()
class UserMapper {
public static toDomain(userPersistenceModel: UserPersistence): User {
const { id, } = userPersistenceModel;
const { id } = userPersistenceModel;
return {
id: userPersistenceModel.id,
firstName: userPersistenceModel.firstName,
Expand All @@ -18,21 +24,30 @@ class UserMapper {
createdAt: userPersistenceModel.createdAt,
updatedAt: userPersistenceModel.updatedAt,
deletedAt: userPersistenceModel.deletedAt,
comments: userPersistenceModel.comments.map(comment => CommentMapper.toDomain(comment)),
comments: userPersistenceModel.comments.map(comment =>
CommentMapper.toDomain(comment),
),
likedPosts: userPersistenceModel.likedPosts.map(like => {
return LikePostMapper.toDomain(like);
}),
likeComments: userPersistenceModel.likedComments.map(like => LikeCommentMapper.toDomain(like)),
likedComments: userPersistenceModel.likedComments.map(like =>
LikeCommentMapper.toDomain(like),
),
equals: (user: User) => user.id === id,
role: userPersistenceModel.role,
createdBy: userPersistenceModel.id as string,
updatedBy: userPersistenceModel.id,
deletedBy: userPersistenceModel.id,
isPasswordMatched: (password: string) => userPersistenceModel.password === password,
isPasswordMatched: (password: string) =>
userPersistenceModel.password === password,
posts: userPersistenceModel.posts.map(post => PostMapper.toDomain(post)),
replies: userPersistenceModel.replies.map(reply => ReplyMapper.toDomain(reply)),
likeReplies: userPersistenceModel.likedReplies.map(like => LikeReplyMapper.toDomain(like))
}
replies: userPersistenceModel.replies.map(reply =>
ReplyMapper.toDomain(reply),
),
likedReplies: userPersistenceModel.likedReplies.map(like =>
LikeReplyMapper.toDomain(like),
),
};
}

public static toPersistence(user: User): UserPersistence {
Expand All @@ -47,15 +62,23 @@ class UserMapper {
createdAt: user.createdAt,
updatedAt: user.updatedAt,
deletedAt: user.deletedAt,
likedReplies: user.likeReplies?.map(
like => LikeReplyMapper.toPersistence(like)
likedReplies: user.likedReplies?.map(like =>
LikeReplyMapper.toPersistence(like),
) as any[], // Add likedReplies property
comments: user.comments?.map(comment => CommentMapper.toPersistence(comment)) as CommentPersistence[],
likedPosts: user.likedPosts?.map(like => LikePostMapper.toPersistence(like)) as any[],
likedComments: user.likeComments?.map(like => LikeCommentMapper.toPersistence(like)) as any[],
comments: user.comments?.map(comment =>
CommentMapper.toPersistence(comment),
) as CommentPersistence[],
likedPosts: user.likedPosts?.map(like =>
LikePostMapper.toPersistence(like),
) as any[],
likedComments: user.likedComments?.map(like =>
LikeCommentMapper.toPersistence(like),
) as any[],
posts: user.posts?.map(post => PostMapper.toPersistence(post)) as any[],
replies: user.replies?.map(reply => ReplyMapper.toPersistence(reply)) as any[],
}
replies: user.replies?.map(reply =>
ReplyMapper.toPersistence(reply),
) as any[],
};
}
}

Expand Down
12 changes: 6 additions & 6 deletions server/src/web/process.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{"level":30,"time":1716524984596,"msg":"Application is running on port 2105 🚀"}
{"level":30,"time":1716524984605,"msg":"Environment: development"}
{"level":30,"time":1716524984668,"msg":"Loaded entities"}
{"level":30,"time":1716524984668,"msg":7}
{"level":30,"time":1716524984668,"msg":1}
{"level":30,"time":1716524984668,"msg":"Data source initialized"}
{"level":30,"time":1716567617876,"msg":"Application is running on port 2105 🚀"}
{"level":30,"time":1716567617878,"msg":"Environment: development"}
{"level":30,"time":1716567617916,"msg":"Loaded entities"}
{"level":30,"time":1716567617916,"msg":7}
{"level":30,"time":1716567617916,"msg":1}
{"level":30,"time":1716567617916,"msg":"Data source initialized"}

0 comments on commit 24547fb

Please sign in to comment.