Skip to content

Commit d97b9f6

Browse files
fix: seeding logic in UserSeeder
1 parent 1ce5821 commit d97b9f6

3 files changed

Lines changed: 182 additions & 145 deletions

File tree

src/Domain/Aggregates/Roles/Role.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,11 @@ public Role(string name, string? description = null)
2222
Description = description;
2323
}
2424

25-
// for seeding purpose
26-
public Role(
27-
Ulid id,
28-
string name,
29-
List<RolePermission> permissions,
30-
string? description,
31-
string createdBy
32-
)
25+
public Role(Ulid id, string name, string? description, string createdBy)
3326
{
3427
Id = id;
3528
Name = Guard.Against.NullOrEmpty(name, nameof(name));
3629
Description = description;
37-
Permissions = [.. Permissions.Concat(permissions)];
3830
CreatedBy = createdBy;
3931
}
4032

src/Domain/Aggregates/Users/User.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,45 @@ public User(
5454
Avatar = avatar;
5555
}
5656

57-
public void InitializeIdentity(Ulid id, string createdBy)
57+
public User(
58+
Ulid id,
59+
string firstName,
60+
string lastName,
61+
string username,
62+
string password,
63+
string email,
64+
IEnumerable<Ulid> roles,
65+
string createdBy,
66+
IEnumerable<Ulid>? permissions = null,
67+
string? phoneNumber = null,
68+
DateTime? dateOfBirth = null,
69+
Gender? gender = null,
70+
string? avatar = null
71+
)
5872
{
5973
Id = id;
74+
FirstName = Guard.Against.NullOrEmpty(firstName, nameof(FirstName));
75+
LastName = Guard.Against.NullOrEmpty(lastName, nameof(LastName));
76+
Username = Guard.Against.NullOrEmpty(username, nameof(Username));
77+
Password = Guard.Against.NullOrEmpty(password, nameof(Password));
78+
Email = Guard.Against.NullOrEmpty(email, nameof(Email));
79+
PhoneNumber = phoneNumber;
80+
DateOfBirth = dateOfBirth;
81+
Gender = gender;
82+
Avatar = avatar;
6083
CreatedBy = createdBy;
84+
Roles = [.. roles.Select(id => new UserRole() { RoleId = id, UserId = Id })];
85+
if (permissions?.Any() == true)
86+
{
87+
Permissions =
88+
[
89+
.. permissions.Select(id => new UserPermission()
90+
{
91+
PermissionId = id,
92+
UserId = Id,
93+
}),
94+
];
95+
}
6196
}
6297

6398
public void HasPasswordAsync(string password) =>

0 commit comments

Comments
 (0)