Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
H
HIAST-Clinics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
almohanad.hafez
HIAST-Clinics
Commits
8deabac6
Commit
8deabac6
authored
Aug 31, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add seed for users, fix constant values issues
parent
0df5c192
Changes
18
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
18 changed files
with
1334 additions
and
147 deletions
+1334
-147
SeedAdminUserHelper.cs
...ics.Backend/API/SeedDatabaseHelper/SeedAdminUserHelper.cs
+1
-1
SeedHelper.cs
Clinics.Backend/API/SeedDatabaseHelper/SeedHelper.cs
+1
-1
LoginCommandHandler.cs
...d/Application/Users/Commands/Login/LoginCommandHandler.cs
+3
-3
UserRole.cs
...cs.Backend/Domain/Entities/Identity/UserRoles/UserRole.cs
+5
-5
UsersRoles.cs
....Backend/Domain/Entities/Identity/UserRoles/UsersRoles.cs
+30
-0
DoctorUser.cs
Clinics.Backend/Domain/Entities/Identity/Users/DoctorUser.cs
+1
-1
ReceptionistUser.cs
...ackend/Domain/Entities/Identity/Users/ReceptionistUser.cs
+1
-1
User.cs
Clinics.Backend/Domain/Entities/Identity/Users/User.cs
+5
-5
DoctorStatuses.cs
...eople/Doctors/Shared/DoctorStatusValues/DoctorStatuses.cs
+6
-30
FamilyRoles.cs
...ons/EmployeeFamilyMembers/FamilyRoleValues/FamilyRoles.cs
+13
-43
RoleConfiguration.cs
...stence/Configurations/Identity/Roles/RoleConfiguration.cs
+3
-3
20240831184313_Update_User_Roles.Designer.cs
...e/Migrations/20240831184313_Update_User_Roles.Designer.cs
+1089
-0
20240831184313_Update_User_Roles.cs
...ersistence/Migrations/20240831184313_Update_User_Roles.cs
+84
-0
ClinicsDbContextModelSnapshot.cs
...d/Persistence/Migrations/ClinicsDbContextModelSnapshot.cs
+3
-3
SeedAdminUser.cs
...ckend/Persistence/SeedDatabase/AdminUser/SeedAdminUser.cs
+0
-44
UserRoles.cs
Clinics.Backend/Persistence/SeedDatabase/UserRoles.cs
+6
-6
ISeedUsers.cs
Clinics.Backend/Persistence/SeedDatabase/Users/ISeedUsers.cs
+1
-1
SeedUsers.cs
Clinics.Backend/Persistence/SeedDatabase/Users/SeedUsers.cs
+82
-0
No files found.
Clinics.Backend/API/SeedDatabaseHelper/SeedAdminUserHelper.cs
View file @
8deabac6
...
...
@@ -14,7 +14,7 @@ public class SeedAdminUserHelper
{
using
(
var
serviceScope
=
applicationBuilder
.
ApplicationServices
.
CreateScope
())
{
var
seedAdminUser
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
AdminUser
>();
var
seedAdminUser
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
Users
>();
await
seedAdminUser
.
Seed
();
}
}
...
...
Clinics.Backend/API/SeedDatabaseHelper/SeedHelper.cs
View file @
8deabac6
...
...
@@ -25,7 +25,7 @@ public class SeedHelper
var
seedMedicineForms
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
<
MedicineForm
>>();
await
seedMedicineForms
.
Seed
();
var
seedUserRoles
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
<
Role
>>();
var
seedUserRoles
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeed
<
User
Role
>>();
await
seedUserRoles
.
Seed
();
}
}
...
...
Clinics.Backend/Application/Users/Commands/Login/LoginCommandHandler.cs
View file @
8deabac6
...
...
@@ -38,7 +38,7 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
#
region
2.
Generate
Response
#
region
2.1
.
Admin
if
(
user
.
Role
==
Roles
.
Admin
)
if
(
user
.
Role
==
Users
Roles
.
Admin
)
{
var
token
=
_jwtProvider
.
Generate
(
user
);
return
LoginResponse
.
GetResponse
(
token
);
...
...
@@ -46,7 +46,7 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
#
endregion
#
region
2.2
.
Doctor
if
(
user
.
Role
==
Roles
.
Doctor
)
if
(
user
.
Role
==
Users
Roles
.
Doctor
)
{
var
doctorUserResult
=
await
_userRepository
.
GetDoctorUserByUserNameFullAsync
(
user
.
UserName
);
if
(
doctorUserResult
.
IsFailure
)
...
...
@@ -58,7 +58,7 @@ public class LoginCommandHandler : CommandHandlerBase<LoginCommand, LoginRespons
#
endregion
#
region
2.3
.
Receptionist
user
if
(
user
.
Role
==
Roles
.
Receptionist
)
if
(
user
.
Role
==
Users
Roles
.
Receptionist
)
{
var
receptionistUser
=
await
_userRepository
.
GetReceptionistUserByUserNameFullAsync
(
user
.
UserName
);
if
(
receptionistUser
.
IsFailure
)
...
...
Clinics.Backend/Domain/Entities/Identity/UserRoles/Role.cs
→
Clinics.Backend/Domain/Entities/Identity/UserRoles/
User
Role.cs
View file @
8deabac6
...
...
@@ -2,14 +2,14 @@
namespace
Domain.Entities.Identity.UserRoles
;
public
sealed
class
Role
:
Entity
public
sealed
class
User
Role
:
Entity
{
#
region
Private
ctor
private
Role
(
int
id
)
:
base
(
id
)
private
User
Role
(
int
id
)
:
base
(
id
)
{
}
private
Role
(
int
id
,
string
name
)
:
base
(
id
)
private
User
Role
(
int
id
,
string
name
)
:
base
(
id
)
{
Name
=
name
;
}
...
...
@@ -22,9 +22,9 @@ public sealed class Role : Entity
#
region
Methods
#
region
Static
factory
internal
static
Role
Create
(
int
id
,
string
name
)
internal
static
User
Role
Create
(
int
id
,
string
name
)
{
return
new
Role
(
id
,
name
);
return
new
User
Role
(
id
,
name
);
}
#
endregion
...
...
Clinics.Backend/Domain/Entities/Identity/UserRoles/Roles.cs
→
Clinics.Backend/Domain/Entities/Identity/UserRoles/
Users
Roles.cs
View file @
8deabac6
namespace
Domain.Entities.Identity.UserRoles
;
public
static
class
Roles
public
static
class
Users
Roles
{
#
region
Constant
values
public
static
int
Count
=>
3
;
...
...
@@ -8,13 +8,18 @@ public static class Roles
public
const
string
DoctorName
=
"doctor"
;
public
const
string
ReceptionistName
=
"receptionist"
;
public
static
Role
Admin
=>
Role
.
Create
(
1
,
AdminName
);
public
static
Role
Doctor
=>
Role
.
Create
(
2
,
DoctorName
);
public
static
Role
Receptionist
=>
Role
.
Create
(
3
,
ReceptionistName
);
private
static
readonly
UserRole
_admin
=
UserRole
.
Create
(
1
,
AdminName
);
public
static
UserRole
Admin
=>
_admin
;
public
static
List
<
Role
>
GetAll
()
private
static
readonly
UserRole
_doctor
=
UserRole
.
Create
(
2
,
DoctorName
);
public
static
UserRole
Doctor
=>
_doctor
;
private
static
readonly
UserRole
_receptionist
=
UserRole
.
Create
(
3
,
ReceptionistName
);
public
static
UserRole
Receptionist
=>
_receptionist
;
public
static
List
<
UserRole
>
GetAll
()
{
List
<
Role
>
roles
=
new
();
List
<
User
Role
>
roles
=
new
();
roles
.
Add
(
Admin
);
roles
.
Add
(
Doctor
);
roles
.
Add
(
Receptionist
);
...
...
Clinics.Backend/Domain/Entities/Identity/Users/DoctorUser.cs
View file @
8deabac6
...
...
@@ -37,7 +37,7 @@ public sealed class DoctorUser : Entity
if
(
doctorResult
.
IsFailure
)
return
Result
.
Failure
<
DoctorUser
>(
doctorResult
.
Error
);
Result
<
User
>
userResult
=
User
.
Create
(
username
,
hashedPassword
,
Roles
.
Doctor
.
Name
);
Result
<
User
>
userResult
=
User
.
Create
(
username
,
hashedPassword
,
Users
Roles
.
Doctor
.
Name
);
if
(
userResult
.
IsFailure
)
return
Result
.
Failure
<
DoctorUser
>(
userResult
.
Error
);
...
...
Clinics.Backend/Domain/Entities/Identity/Users/ReceptionistUser.cs
View file @
8deabac6
...
...
@@ -37,7 +37,7 @@ public sealed class ReceptionistUser : Entity
if
(
personalInfoResult
.
IsFailure
)
return
Result
.
Failure
<
ReceptionistUser
>(
personalInfoResult
.
Error
);
Result
<
User
>
userResult
=
User
.
Create
(
userName
,
hashedPassword
,
Roles
.
Receptionist
.
Name
);
Result
<
User
>
userResult
=
User
.
Create
(
userName
,
hashedPassword
,
Users
Roles
.
Receptionist
.
Name
);
if
(
userResult
.
IsFailure
)
return
Result
.
Failure
<
ReceptionistUser
>(
userResult
.
Error
);
...
...
Clinics.Backend/Domain/Entities/Identity/Users/User.cs
View file @
8deabac6
...
...
@@ -12,7 +12,7 @@ public sealed class User : Entity
{
}
private
User
(
int
id
,
string
userName
,
string
hashedPassword
,
Role
role
)
:
base
(
id
)
private
User
(
int
id
,
string
userName
,
string
hashedPassword
,
User
Role
role
)
:
base
(
id
)
{
UserName
=
userName
;
HashedPassword
=
hashedPassword
;
...
...
@@ -23,7 +23,7 @@ public sealed class User : Entity
#
region
Properties
public
string
UserName
{
get
;
private
set
;
}
=
null
!;
public
string
HashedPassword
{
get
;
private
set
;
}
=
null
!;
public
Role
Role
{
get
;
private
set
;
}
=
null
!;
public
User
Role
Role
{
get
;
private
set
;
}
=
null
!;
#
endregion
#
region
Methods
...
...
@@ -37,9 +37,9 @@ public sealed class User : Entity
}
#
region
Check
role
Result
<
Role
>
selectedRole
=
Result
.
Failure
<
Role
>(
IdentityErrors
.
InvalidRole
);
List
<
Role
>
roles
=
Roles
.
GetAll
();
foreach
(
Role
roleItem
in
roles
)
Result
<
UserRole
>
selectedRole
=
Result
.
Failure
<
User
Role
>(
IdentityErrors
.
InvalidRole
);
List
<
UserRole
>
roles
=
Users
Roles
.
GetAll
();
foreach
(
User
Role
roleItem
in
roles
)
{
if
(
roleItem
.
Name
==
role
)
selectedRole
=
roleItem
;
...
...
Clinics.Backend/Domain/Entities/People/Doctors/Shared/DoctorStatusValues/DoctorStatuses.cs
View file @
8deabac6
...
...
@@ -9,38 +9,14 @@ public static class DoctorStatuses
public
static
int
Count
=>
3
;
public
static
DoctorStatus
Available
{
get
{
var
result
=
DoctorStatus
.
Create
(
"متاح"
,
1
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
DoctorStatus
>();
return
result
.
Value
;
}
}
private
readonly
static
DoctorStatus
_available
=
DoctorStatus
.
Create
(
"متاح"
,
1
).
Value
;
public
static
DoctorStatus
Available
=>
_available
;
public
static
DoctorStatus
Working
{
get
{
var
result
=
DoctorStatus
.
Create
(
"لديه مريض"
,
2
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
DoctorStatus
>();
return
result
.
Value
;
}
}
private
readonly
static
DoctorStatus
_working
=
DoctorStatus
.
Create
(
"لديه مريض"
,
2
).
Value
;
public
static
DoctorStatus
Working
=>
_working
;
public
static
DoctorStatus
Busy
{
get
{
var
result
=
DoctorStatus
.
Create
(
"مشغول"
,
3
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
DoctorStatus
>();
return
result
.
Value
;
}
}
private
readonly
static
DoctorStatus
_busy
=
DoctorStatus
.
Create
(
"مشغول"
,
3
).
Value
;
public
static
DoctorStatus
Busy
=>
_busy
;
#
endregion
}
Clinics.Backend/Domain/Entities/People/Employees/Relations/EmployeeFamilyMembers/FamilyRoleValues/FamilyRoles.cs
View file @
8deabac6
...
...
@@ -7,49 +7,19 @@ public static class FamilyRoles
#
region
Constant
id
values
public
static
int
Count
=>
4
;
public
static
FamilyRole
Husband
{
get
{
var
result
=
FamilyRole
.
Create
(
"زوج"
,
1
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
FamilyRole
>();
return
result
.
Value
;
}
}
public
static
FamilyRole
Wife
{
get
{
var
result
=
FamilyRole
.
Create
(
"زوجة"
,
2
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
FamilyRole
>();
return
result
.
Value
;
}
}
public
static
FamilyRole
Son
{
get
{
var
result
=
FamilyRole
.
Create
(
"ابن"
,
3
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
FamilyRole
>();
return
result
.
Value
;
}
}
public
static
FamilyRole
Daughter
{
get
{
var
result
=
FamilyRole
.
Create
(
"ابنة"
,
4
);
if
(
result
.
IsFailure
)
throw
new
InvalidValuesDomainException
<
FamilyRole
>();
return
result
.
Value
;
}
}
public
readonly
static
FamilyRole
_husband
=
FamilyRole
.
Create
(
"زوج"
,
1
).
Value
;
public
static
FamilyRole
Husband
=>
_husband
;
public
readonly
static
FamilyRole
_wife
=
FamilyRole
.
Create
(
"زوجة"
,
2
).
Value
;
public
static
FamilyRole
Wife
=>
_wife
;
public
readonly
static
FamilyRole
_son
=
FamilyRole
.
Create
(
"ابن"
,
3
).
Value
;
public
static
FamilyRole
Son
=>
_son
;
public
readonly
static
FamilyRole
_daughter
=
FamilyRole
.
Create
(
"ابنة"
,
4
).
Value
;
public
static
FamilyRole
Daughter
=>
_daughter
;
#
endregion
}
Clinics.Backend/Persistence/Configurations/Identity/Roles/RoleConfiguration.cs
View file @
8deabac6
...
...
@@ -4,11 +4,11 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace
Persistence.Configurations.Identity.Roles
;
public
class
RoleConfiguration
:
IEntityTypeConfiguration
<
Role
>
public
class
RoleConfiguration
:
IEntityTypeConfiguration
<
User
Role
>
{
public
void
Configure
(
EntityTypeBuilder
<
Role
>
builder
)
public
void
Configure
(
EntityTypeBuilder
<
User
Role
>
builder
)
{
builder
.
ToTable
(
nameof
(
Role
));
builder
.
ToTable
(
nameof
(
User
Role
));
builder
.
Property
(
role
=>
role
.
Id
).
ValueGeneratedNever
();
...
...
Clinics.Backend/Persistence/Migrations/20240831184313_Update_User_Roles.Designer.cs
0 → 100644
View file @
8deabac6
This diff is collapsed.
Click to expand it.
Clinics.Backend/Persistence/Migrations/20240831184313_Update_User_Roles.cs
0 → 100644
View file @
8deabac6
using
Microsoft.EntityFrameworkCore.Migrations
;
#
nullable
disable
namespace
Persistence.Migrations
{
/// <inheritdoc />
public
partial
class
Update_User_Roles
:
Migration
{
/// <inheritdoc />
protected
override
void
Up
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
DropForeignKey
(
name
:
"FK_User_Role_RoleId"
,
table
:
"User"
);
migrationBuilder
.
DropTable
(
name
:
"Role"
);
migrationBuilder
.
CreateTable
(
name
:
"UserRole"
,
columns
:
table
=>
new
{
Id
=
table
.
Column
<
int
>(
type
:
"int"
,
nullable
:
false
),
Name
=
table
.
Column
<
string
>(
type
:
"nvarchar(50)"
,
maxLength
:
50
,
nullable
:
false
)
},
constraints
:
table
=>
{
table
.
PrimaryKey
(
"PK_UserRole"
,
x
=>
x
.
Id
);
});
migrationBuilder
.
CreateIndex
(
name
:
"IX_UserRole_Name"
,
table
:
"UserRole"
,
column
:
"Name"
,
unique
:
true
);
migrationBuilder
.
AddForeignKey
(
name
:
"FK_User_UserRole_RoleId"
,
table
:
"User"
,
column
:
"RoleId"
,
principalTable
:
"UserRole"
,
principalColumn
:
"Id"
,
onDelete
:
ReferentialAction
.
Cascade
);
}
/// <inheritdoc />
protected
override
void
Down
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
DropForeignKey
(
name
:
"FK_User_UserRole_RoleId"
,
table
:
"User"
);
migrationBuilder
.
DropTable
(
name
:
"UserRole"
);
migrationBuilder
.
CreateTable
(
name
:
"Role"
,
columns
:
table
=>
new
{
Id
=
table
.
Column
<
int
>(
type
:
"int"
,
nullable
:
false
),
Name
=
table
.
Column
<
string
>(
type
:
"nvarchar(50)"
,
maxLength
:
50
,
nullable
:
false
)
},
constraints
:
table
=>
{
table
.
PrimaryKey
(
"PK_Role"
,
x
=>
x
.
Id
);
});
migrationBuilder
.
CreateIndex
(
name
:
"IX_Role_Name"
,
table
:
"Role"
,
column
:
"Name"
,
unique
:
true
);
migrationBuilder
.
AddForeignKey
(
name
:
"FK_User_Role_RoleId"
,
table
:
"User"
,
column
:
"RoleId"
,
principalTable
:
"Role"
,
principalColumn
:
"Id"
,
onDelete
:
ReferentialAction
.
Cascade
);
}
}
}
Clinics.Backend/Persistence/Migrations/ClinicsDbContextModelSnapshot.cs
View file @
8deabac6
...
...
@@ -22,7 +22,7 @@ namespace Persistence.Migrations
SqlServerModelBuilderExtensions
.
UseIdentityColumns
(
modelBuilder
);
modelBuilder
.
Entity
(
"Domain.Entities.Identity.UserRoles.Role"
,
b
=>
modelBuilder
.
Entity
(
"Domain.Entities.Identity.UserRoles.
User
Role"
,
b
=>
{
b
.
Property
<
int
>(
"Id"
)
.
HasColumnType
(
"int"
);
...
...
@@ -37,7 +37,7 @@ namespace Persistence.Migrations
b
.
HasIndex
(
"Name"
)
.
IsUnique
();
b
.
ToTable
(
"Role"
,
(
string
)
null
);
b
.
ToTable
(
"
User
Role"
,
(
string
)
null
);
});
modelBuilder
.
Entity
(
"Domain.Entities.Identity.Users.DoctorUser"
,
b
=>
...
...
@@ -763,7 +763,7 @@ namespace Persistence.Migrations
modelBuilder
.
Entity
(
"Domain.Entities.Identity.Users.User"
,
b
=>
{
b
.
HasOne
(
"Domain.Entities.Identity.UserRoles.Role"
,
"Role"
)
b
.
HasOne
(
"Domain.Entities.Identity.UserRoles.
User
Role"
,
"Role"
)
.
WithMany
()
.
HasForeignKey
(
"RoleId"
)
.
OnDelete
(
DeleteBehavior
.
Cascade
)
...
...
Clinics.Backend/Persistence/SeedDatabase/AdminUser/SeedAdminUser.cs
deleted
100644 → 0
View file @
0df5c192
using
Domain.Entities.Identity.UserRoles
;
using
Domain.Entities.Identity.Users
;
using
Domain.Exceptions.InvalidValue
;
using
Domain.Shared
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Context
;
using
Persistence.Identity.PasswordsHashing
;
namespace
Persistence.SeedDatabase.AdminUser
;
public
class
SeedAdminUser
:
ISeedAdminUser
{
#
region
CTOR
DI
private
readonly
ClinicsDbContext
_context
;
private
readonly
IPasswordHasher
_passwordHasher
;
public
SeedAdminUser
(
ClinicsDbContext
context
,
IPasswordHasher
passwordHasher
)
{
_context
=
context
;
_passwordHasher
=
passwordHasher
;
}
#
endregion
public
async
Task
Seed
()
{
DbSet
<
User
>
users
=
_context
.
Set
<
User
>();
Result
<
User
>
adminUserResult
=
User
.
Create
(
"admin"
,
_passwordHasher
.
Hash
(
"123"
),
Roles
.
Admin
.
Name
);
if
(
adminUserResult
.
IsFailure
)
throw
new
Exception
(
"Unable to seed admin user"
);
if
(
users
.
Include
(
user
=>
user
.
Role
).
Where
(
user
=>
user
.
Role
==
Roles
.
Admin
).
ToList
().
Count
!=
1
)
{
var
adminUser
=
adminUserResult
.
Value
;
_context
.
Entry
(
adminUser
.
Role
).
State
=
EntityState
.
Unchanged
;
users
.
Add
(
adminUserResult
.
Value
);
await
_context
.
SaveChangesAsync
();
}
}
}
Clinics.Backend/Persistence/SeedDatabase/UserRoles.cs
View file @
8deabac6
...
...
@@ -4,7 +4,7 @@ using Persistence.Context;
namespace
Persistence.SeedDatabase
;
public
class
UserRoles
:
ISeed
<
Role
>
public
class
UserRoles
:
ISeed
<
User
Role
>
{
#
region
CTOR
DI
private
readonly
ClinicsDbContext
_context
;
...
...
@@ -16,19 +16,19 @@ public class UserRoles : ISeed<Role>
#
endregion
public
async
Task
Seed
()
{
DbSet
<
Role
>
roles
=
_context
.
Set
<
Role
>();
DbSet
<
UserRole
>
roles
=
_context
.
Set
<
User
Role
>();
var
current
=
await
roles
.
ToListAsync
();
// TODO: perform deep check on all seed operations
if
(
current
.
Count
!=
Roles
.
Count
)
if
(
current
.
Count
!=
Users
Roles
.
Count
)
{
roles
.
RemoveRange
(
current
);
roles
.
Add
(
Roles
.
Admin
);
roles
.
Add
(
Users
Roles
.
Admin
);
roles
.
Add
(
Roles
.
Doctor
);
roles
.
Add
(
Users
Roles
.
Doctor
);
roles
.
Add
(
Roles
.
Receptionist
);
roles
.
Add
(
Users
Roles
.
Receptionist
);
await
_context
.
SaveChangesAsync
();
}
...
...
Clinics.Backend/Persistence/SeedDatabase/
AdminUser/ISeedAdminUser
.cs
→
Clinics.Backend/Persistence/SeedDatabase/
Users/ISeedUsers
.cs
View file @
8deabac6
namespace
Persistence.SeedDatabase.AdminUser
;
public
interface
ISeed
AdminUser
public
interface
ISeed
Users
{
public
Task
Seed
();
}
Clinics.Backend/Persistence/SeedDatabase/Users/SeedUsers.cs
0 → 100644
View file @
8deabac6
using
Domain.Entities.Identity.UserRoles
;
using
Domain.Entities.Identity.Users
;
using
Domain.Shared
;
using
Microsoft.EntityFrameworkCore
;
using
Persistence.Context
;
using
Persistence.Identity.PasswordsHashing
;
namespace
Persistence.SeedDatabase.AdminUser
;
public
class
SeedUsers
:
ISeedUsers
{
#
region
CTOR
DI
private
readonly
ClinicsDbContext
_context
;
private
readonly
IPasswordHasher
_passwordHasher
;
public
SeedUsers
(
ClinicsDbContext
context
,
IPasswordHasher
passwordHasher
)
{
_context
=
context
;
_passwordHasher
=
passwordHasher
;
}
#
endregion
public
async
Task
Seed
()
{
var
currentCount
=
_context
.
Set
<
User
>().
ToList
().
Count
;
if
(
currentCount
==
0
)
{
#
region
Seed
admin
Result
<
User
>
adminUserResult
=
User
.
Create
(
"admin"
,
_passwordHasher
.
Hash
(
"123"
),
UsersRoles
.
Admin
.
Name
);
if
(
adminUserResult
.
IsFailure
)
throw
new
Exception
(
"Unable to seed admin user"
);
var
adminUser
=
adminUserResult
.
Value
;
_context
.
Entry
(
adminUser
.
Role
).
State
=
EntityState
.
Unchanged
;
_context
.
Set
<
User
>().
Add
(
adminUserResult
.
Value
);
#
endregion
#
region
Seed
doctor
var
doctorUserCreateResult
=
DoctorUser
.
Create
(
"doctor"
,
_passwordHasher
.
Hash
(
"123"
),
"محمد"
,
"صالح"
,
"التركي"
);
if
(
doctorUserCreateResult
.
IsFailure
)
throw
new
Exception
(
"Unable to seed doctor user"
);
var
doctorUser
=
doctorUserCreateResult
.
Value
;
_context
.
Entry
(
doctorUser
.
User
.
Role
).
State
=
EntityState
.
Unchanged
;
_context
.
Entry
(
doctorUser
.
Doctor
.
Status
).
State
=
EntityState
.
Unchanged
;
_context
.
Set
<
DoctorUser
>().
Add
(
doctorUser
);
#
endregion
#
region
Seed
receptionist
var
receptionistUserCreateResult
=
ReceptionistUser
.
Create
(
"receptionist"
,
_passwordHasher
.
Hash
(
"123"
),
"موفق"
,
"سامي"
,
"الحسين"
);
if
(
receptionistUserCreateResult
.
IsFailure
)
throw
new
Exception
(
"Unable to seed receptionist user"
);
var
receptionistUser
=
receptionistUserCreateResult
.
Value
;
_context
.
Entry
(
receptionistUser
.
User
.
Role
).
State
=
EntityState
.
Unchanged
;
_context
.
Set
<
ReceptionistUser
>().
Add
(
receptionistUser
);
#
endregion
await
_context
.
SaveChangesAsync
();
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment