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
f3f4bf51
Commit
f3f4bf51
authored
Aug 20, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Seed admin user
parent
45a07430
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
72 additions
and
0 deletions
+72
-0
Program.cs
Clinics.Backend/API/Program.cs
+1
-0
SeedAdminUserHelper.cs
...ics.Backend/API/SeedDatabaseHelper/SeedAdminUserHelper.cs
+21
-0
ISeedAdminUser.cs
...kend/Persistence/SeedDatabase/AdminUser/ISeedAdminUser.cs
+6
-0
SeedAdminUser.cs
...ckend/Persistence/SeedDatabase/AdminUser/SeedAdminUser.cs
+44
-0
No files found.
Clinics.Backend/API/Program.cs
View file @
f3f4bf51
...
...
@@ -73,6 +73,7 @@ var app = builder.Build();
#region Seed database
await
SeedHelper
.
Seed
(
app
);
await
SeedAdminUserHelper
.
Seed
(
app
);
#endregion
// Configure the HTTP request pipeline.
...
...
Clinics.Backend/API/SeedDatabaseHelper/SeedAdminUserHelper.cs
0 → 100644
View file @
f3f4bf51
using
Domain.Entities.Identity.UserRoles
;
using
Domain.Entities.Medicals.Medicines.MedicineFormValues
;
using
Domain.Entities.People.Doctors.Shared.Constants.DoctorStatusValues
;
using
Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues
;
using
Domain.Entities.People.Shared.GenderValues
;
using
Persistence.SeedDatabase
;
using
Persistence.SeedDatabase.AdminUser
;
namespace
API.SeedDatabaseHelper
;
public
class
SeedAdminUserHelper
{
public
static
async
Task
Seed
(
IApplicationBuilder
applicationBuilder
)
{
using
(
var
serviceScope
=
applicationBuilder
.
ApplicationServices
.
CreateScope
())
{
var
seedAdminUser
=
serviceScope
.
ServiceProvider
.
GetRequiredService
<
ISeedAdminUser
>();
await
seedAdminUser
.
Seed
();
}
}
}
Clinics.Backend/Persistence/SeedDatabase/AdminUser/ISeedAdminUser.cs
0 → 100644
View file @
f3f4bf51
namespace
Persistence.SeedDatabase.AdminUser
;
public
interface
ISeedAdminUser
{
public
Task
Seed
();
}
Clinics.Backend/Persistence/SeedDatabase/AdminUser/SeedAdminUser.cs
0 → 100644
View file @
f3f4bf51
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
();
}
}
}
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