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
f228f27b
Commit
f228f27b
authored
Aug 21, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Add register doctor
parent
d739d7b0
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
180 additions
and
2 deletions
+180
-2
RegisterDoctorCommand.cs
...on/Users/Commands/RegisterDoctor/RegisterDoctorCommand.cs
+14
-0
RegisterDoctorHandler.cs
...on/Users/Commands/RegisterDoctor/RegisterDoctorHandler.cs
+41
-0
RegisterDoctorResponse.cs
...n/Users/Commands/RegisterDoctor/RegisterDoctorResponse.cs
+24
-0
User.cs
Clinics.Backend/Domain/Entities/Identity/Users/User.cs
+15
-0
IdentityErrors.cs
Clinics.Backend/Domain/Errors/IdentityErrors.cs
+1
-0
IUserRepository.cs
Clinics.Backend/Domain/Repositories/IUserRepository.cs
+19
-0
UserRepository.cs
....Backend/Persistence/Repositories/Users/UserRepository.cs
+50
-2
UsersController.cs
Clinics.Backend/Presentation/Controllers/UsersController.cs
+16
-0
No files found.
Clinics.Backend/Application/Users/Commands/RegisterDoctor/RegisterDoctorCommand.cs
0 → 100644
View file @
f228f27b
using
Application.Abstractions.CQRS.Commands
;
namespace
Application.Users.Commands.RegisterDoctor
;
public
class
RegisterDoctorCommand
:
ICommand
<
RegisterDoctorResponse
>
{
public
string
UserName
{
get
;
set
;
}
=
null
!;
public
string
Password
{
get
;
set
;
}
=
null
!;
public
string
FirstName
{
get
;
set
;
}
=
null
!;
public
string
MiddleName
{
get
;
set
;
}
=
null
!;
public
string
LastName
{
get
;
set
;
}
=
null
!;
}
Clinics.Backend/Application/Users/Commands/RegisterDoctor/RegisterDoctorHandler.cs
0 → 100644
View file @
f228f27b
using
Application.Abstractions.CQRS.Commands
;
using
Domain.Entities.Identity.Users
;
using
Domain.Repositories
;
using
Domain.Shared
;
using
Domain.UnitOfWork
;
namespace
Application.Users.Commands.RegisterDoctor
;
public
class
RegisterDoctorHandler
:
CommandHandlerBase
<
RegisterDoctorCommand
,
RegisterDoctorResponse
>
{
#
region
CTOR
DI
private
readonly
IUserRepository
_userRepository
;
public
RegisterDoctorHandler
(
IUnitOfWork
unitOfWork
,
IUserRepository
userRepository
)
:
base
(
unitOfWork
)
{
_userRepository
=
userRepository
;
}
#
endregion
public
override
async
Task
<
Result
<
RegisterDoctorResponse
>>
HandleHelper
(
RegisterDoctorCommand
request
,
CancellationToken
cancellationToken
)
{
#
region
1.
Create
doctor
user
Result
<
DoctorUser
>
doctorUserResult
=
DoctorUser
.
Create
(
request
.
UserName
,
request
.
Password
,
request
.
FirstName
,
request
.
MiddleName
,
request
.
LastName
);
if
(
doctorUserResult
.
IsFailure
)
return
Result
.
Failure
<
RegisterDoctorResponse
>(
doctorUserResult
.
Error
);
#
endregion
#
region
2.
Register
(
save
to
DB
)
var
registerResult
=
await
_userRepository
.
RegisterDoctorAsync
(
doctorUserResult
.
Value
);
if
(
registerResult
.
IsFailure
)
return
Result
.
Failure
<
RegisterDoctorResponse
>(
registerResult
.
Error
);
#
endregion
return
RegisterDoctorResponse
.
GetResponse
(
registerResult
.
Value
);
}
}
Clinics.Backend/Application/Users/Commands/RegisterDoctor/RegisterDoctorResponse.cs
0 → 100644
View file @
f228f27b
using
Domain.Entities.Identity.Users
;
using
Domain.Entities.People.Doctors
;
using
Domain.Errors
;
using
Domain.Shared
;
namespace
Application.Users.Commands.RegisterDoctor
;
public
class
RegisterDoctorResponse
{
public
int
Id
{
get
;
set
;
}
public
Doctor
Doctor
{
get
;
set
;
}
=
null
!;
public
static
Result
<
RegisterDoctorResponse
>
GetResponse
(
DoctorUser
doctorUser
)
{
if
(
doctorUser
is
null
)
return
Result
.
Failure
<
RegisterDoctorResponse
>(
IdentityErrors
.
NotFound
);
return
new
RegisterDoctorResponse
{
Id
=
doctorUser
.
Id
,
Doctor
=
doctorUser
.
Doctor
};
}
}
Clinics.Backend/Domain/Entities/Identity/Users/User.cs
View file @
f228f27b
...
...
@@ -27,6 +27,8 @@ public sealed class User : Entity
#
endregion
#
region
Methods
#
region
Static
factory
public
static
Result
<
User
>
Create
(
string
userName
,
string
hashedPassword
,
string
role
)
{
if
(
userName
is
null
||
hashedPassword
is
null
||
role
is
null
)
...
...
@@ -49,4 +51,17 @@ public sealed class User : Entity
return
new
User
(
0
,
userName
,
hashedPassword
,
selectedRole
.
Value
);
}
#
endregion
#
region
Set
HASHED
password
public
Result
SetHashedPassword
(
string
hashedPassword
)
{
if
(
hashedPassword
is
null
)
return
Result
.
Failure
(
DomainErrors
.
InvalidValuesError
);
HashedPassword
=
hashedPassword
;
return
Result
.
Success
();
}
#
endregion
#
endregion
}
Clinics.Backend/Domain/Errors/IdentityErrors.cs
View file @
f228f27b
...
...
@@ -7,4 +7,5 @@ public static class IdentityErrors
public
static
Error
InvalidRole
=>
new
(
"Identity.InvalidRole"
,
"Role specified for user is invalid"
);
public
static
Error
NotFound
=>
new
(
"Identity.NotFound"
,
"المستخدم غير مسجّل في النظام"
);
public
static
Error
PasswordMismatch
=>
new
(
"Identity.PasswordMismatch"
,
"كلمة السر غير صحيحة"
);
public
static
Error
UnableToRegister
=>
new
(
"Identity.UnableToRegister"
,
"تعذر تسجيل المستخدم"
);
}
Clinics.Backend/Domain/Repositories/IUserRepository.cs
View file @
f228f27b
...
...
@@ -9,8 +9,27 @@ public interface IUserRepository : IRepository<User>
public
Task
<
Result
<
User
>>
GetByUserNameFullAsync
(
string
userName
);
#
region
Verify
password
public
Task
<
Result
<
User
?>>
VerifyPasswordAsync
(
string
userName
,
string
password
);
#
endregion
#
region
Get
doctor
user
by
user
name
full
public
Task
<
Result
<
DoctorUser
>>
GetDoctorUserByUserNameFullAsync
(
string
userName
);
#
endregion
#
region
Get
receptionist
user
by
user
name
full
public
Task
<
Result
<
ReceptionistUser
>>
GetReceptionistUserByUserNameFullAsync
(
string
userName
);
#
endregion
#
region
Register
doctor
public
Task
<
Result
<
DoctorUser
>>
RegisterDoctorAsync
(
DoctorUser
doctorUser
);
#
endregion
#
region
Register
receptionist
public
Task
<
Result
<
ReceptionistUser
>>
RegisterReceptionistAsync
(
ReceptionistUser
receptionistUser
);
#
endregion
}
Clinics.Backend/Persistence/Repositories/Users/UserRepository.cs
View file @
f228f27b
...
...
@@ -21,10 +21,13 @@ public class UserRepository : Repositroy<User>, IUserRepository
#
endregion
#
region
Create
method
public
override
Task
<
Result
<
User
>>
CreateAsync
(
User
entity
)
public
override
async
Task
<
Result
<
User
>>
CreateAsync
(
User
entity
)
{
_context
.
Entry
(
entity
.
Role
).
State
=
EntityState
.
Unchanged
;
return
base
.
CreateAsync
(
entity
);
var
passwordResult
=
entity
.
SetHashedPassword
(
_passwordHasher
.
Hash
(
entity
.
HashedPassword
));
if
(
passwordResult
.
IsFailure
)
return
Result
.
Failure
<
User
>(
passwordResult
.
Error
);
return
await
base
.
CreateAsync
(
entity
);
}
#
endregion
...
...
@@ -96,4 +99,49 @@ public class UserRepository : Repositroy<User>, IUserRepository
return
result
.
First
();
}
#
endregion
#
region
Register
doctor
public
async
Task
<
Result
<
DoctorUser
>>
RegisterDoctorAsync
(
DoctorUser
doctorUser
)
{
_context
.
Entry
(
doctorUser
.
User
.
Role
).
State
=
EntityState
.
Unchanged
;
_context
.
Entry
(
doctorUser
.
Doctor
.
Status
).
State
=
EntityState
.
Unchanged
;
var
passwordResult
=
doctorUser
.
User
.
SetHashedPassword
(
_passwordHasher
.
Hash
(
doctorUser
.
User
.
HashedPassword
));
if
(
passwordResult
.
IsFailure
)
return
Result
.
Failure
<
DoctorUser
>(
passwordResult
.
Error
);
try
{
var
createdDoctorUser
=
await
_context
.
Set
<
DoctorUser
>().
AddAsync
(
doctorUser
);
await
_context
.
SaveChangesAsync
();
return
createdDoctorUser
.
Entity
;
}
catch
(
Exception
)
{
return
Result
.
Failure
<
DoctorUser
>(
IdentityErrors
.
UnableToRegister
);
}
}
#
endregion
#
region
Register
receptionist
public
async
Task
<
Result
<
ReceptionistUser
>>
RegisterReceptionistAsync
(
ReceptionistUser
receptionistUser
)
{
_context
.
Entry
(
receptionistUser
.
User
.
Role
).
State
=
EntityState
.
Unchanged
;
var
passwordResult
=
receptionistUser
.
User
.
SetHashedPassword
(
_passwordHasher
.
Hash
(
receptionistUser
.
User
.
HashedPassword
));
if
(
passwordResult
.
IsFailure
)
return
Result
.
Failure
<
ReceptionistUser
>(
passwordResult
.
Error
);
try
{
var
createdreceptionistUser
=
await
_context
.
Set
<
ReceptionistUser
>().
AddAsync
(
receptionistUser
);
await
_context
.
SaveChangesAsync
();
return
createdreceptionistUser
.
Entity
;
}
catch
(
Exception
)
{
return
Result
.
Failure
<
ReceptionistUser
>(
IdentityErrors
.
UnableToRegister
);
}
}
#
endregion
}
Clinics.Backend/Presentation/Controllers/UsersController.cs
View file @
f228f27b
using
Application.Users.Commands.Login
;
using
Application.Users.Commands.RegisterDoctor
;
using
Domain.Entities.Identity.UserRoles
;
using
MediatR
;
using
Microsoft.AspNetCore.Authorization
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Routing
;
using
Presentation.Controllers.Base
;
namespace
Presentation.Controllers
;
...
...
@@ -25,4 +29,16 @@ public class UsersController : ApiController
return
Ok
(
result
.
Value
);
}
[
Authorize
(
Roles
=
Roles
.
AdminName
)]
[
HttpPost
(
"Doctors"
)]
public
async
Task
<
IActionResult
>
RegisterDoctor
([
FromBody
]
RegisterDoctorCommand
command
)
{
var
result
=
await
_sender
.
Send
(
command
);
if
(
result
.
IsFailure
)
return
HandleFailure
(
result
);
return
Ok
(
result
.
Value
);
}
}
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