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
6fea5553
Commit
6fea5553
authored
Aug 18, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Apply result pattern on CQRS
parent
de273e03
Changes
8
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
48 additions
and
36 deletions
+48
-36
ICommand.cs
...ackend/Application/Abstractions/CQRS/Commands/ICommand.cs
+4
-3
ICommandHandler.cs
...Application/Abstractions/CQRS/Commands/ICommandHandler.cs
+4
-3
IQuery.cs
...s.Backend/Application/Abstractions/CQRS/Queries/IQuery.cs
+3
-2
IQueryHandler.cs
...nd/Application/Abstractions/CQRS/Queries/IQueryHandler.cs
+3
-2
CreateEmployeeCommandHandler.cs
...Employees/Commands/Create/CreateEmployeeCommandHandler.cs
+20
-23
PersistenceErrors.cs
Clinics.Backend/Domain/Errors/PersistenceErrors.cs
+9
-0
UnitOfWork.cs
Clinics.Backend/Persistence/UnitOfWork/UnitOfWork.cs
+1
-1
EmployeesController.cs
...s.Backend/Presentation/Controllers/EmployeesController.cs
+4
-2
No files found.
Clinics.Backend/Application/Abstractions/CQRS/Commands/ICommand.cs
View file @
6fea5553
using
MediatR
;
using
Domain.Shared
;
using
MediatR
;
namespace
Application.Abstractions.CQRS.Commands
;
// No response
public
interface
ICommand
:
IRequest
public
interface
ICommand
:
IRequest
<
Result
>
{
}
// With response
public
interface
ICommand
<
TResponse
>
:
IRequest
<
TResponse
>
public
interface
ICommand
<
TResponse
>
:
IRequest
<
Result
<
TResponse
>
>
{
}
Clinics.Backend/Application/Abstractions/CQRS/Commands/ICommandHandler.cs
View file @
6fea5553
using
MediatR
;
using
Domain.Shared
;
using
MediatR
;
namespace
Application.Abstractions.CQRS.Commands
;
// No response
public
interface
ICommandHandler
<
TCommand
>
:
IRequestHandler
<
TCommand
>
public
interface
ICommandHandler
<
TCommand
>
:
IRequestHandler
<
TCommand
,
Result
>
where
TCommand
:
ICommand
{
}
// With response
public
interface
ICommandHandler
<
TCommand
,
TResponse
>
:
IRequestHandler
<
TCommand
,
TResponse
>
:
IRequestHandler
<
TCommand
,
Result
<
TResponse
>
>
where
TCommand
:
ICommand
<
TResponse
>
{
}
\ No newline at end of file
Clinics.Backend/Application/Abstractions/CQRS/Queries/IQuery.cs
View file @
6fea5553
using
MediatR
;
using
Domain.Shared
;
using
MediatR
;
namespace
Application.Abstractions.CQRS.Queries
;
public
interface
IQuery
<
TResponse
>
:
IRequest
<
TResponse
>
public
interface
IQuery
<
TResponse
>
:
IRequest
<
Result
<
TResponse
>
>
{
}
Clinics.Backend/Application/Abstractions/CQRS/Queries/IQueryHandler.cs
View file @
6fea5553
using
MediatR
;
using
Domain.Shared
;
using
MediatR
;
namespace
Application.Abstractions.CQRS.Queries
;
public
interface
IQueryHandler
<
TQuery
,
TResponse
>
:
IRequestHandler
<
TQuery
,
TResponse
>
:
IRequestHandler
<
TQuery
,
Result
<
TResponse
>
>
where
TQuery
:
IQuery
<
TResponse
>
{
}
Clinics.Backend/Application/Employees/Commands/Create/CreateEmployeeCommandHandler.cs
View file @
6fea5553
using
Application.Abstractions.CQRS.Commands
;
using
Domain.Entities.People.Employees
;
using
Domain.Repositories
;
using
Domain.Shared
;
using
Domain.UnitOfWork
;
namespace
Application.Employees.Commands.Create
;
...
...
@@ -17,39 +18,35 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman
}
#
endregion
public
async
Task
Handle
(
CreateEmployeeCommand
request
,
CancellationToken
cancellationToken
)
public
async
Task
<
Result
>
Handle
(
CreateEmployeeCommand
request
,
CancellationToken
cancellationToken
)
{
#
region
Create
new
employee
Employee
employee
;
try
{
employee
=
Employee
.
Create
(
request
.
FirstName
,
request
.
MiddleName
,
request
.
LastName
,
// Personal info
Result
<
Employee
>
employeeResult
=
Employee
.
Create
(
request
.
FirstName
,
request
.
MiddleName
,
request
.
LastName
,
request
.
DateOfBirth
,
request
.
Gender
,
// Patient info
request
.
DateOfBirth
,
request
.
Gender
,
request
.
SerialNumber
,
request
.
CenterStatus
,
false
,
request
.
SerialNumber
,
request
.
CenterStatus
,
false
,
// Employee info
request
.
StartDate
,
request
.
AcademicQualification
,
request
.
WorkPhone
,
request
.
Location
,
request
.
Specialization
,
request
.
JobStatus
);
if
(
employeeResult
.
IsFailure
)
return
Result
.
Failure
(
employeeResult
.
Error
);
request
.
StartDate
,
request
.
AcademicQualification
,
request
.
WorkPhone
,
// additional
request
.
Location
,
request
.
Specialization
,
request
.
JobStatus
);
}
catch
(
Exception
)
{
throw
;
}
#
endregion
#
region
Add
to
DB
try
{
_employeesRepository
.
Create
(
employee
);
_employeesRepository
.
Create
(
employee
Result
.
Value
);
await
_unitOfWork
.
SaveChangesAsync
();
}
catch
(
Exception
)
catch
(
Exception
exp
)
{
throw
;
// For debugging
//return Result.Failure(new Error("Persistence.UnableToSaveTransaction", exp.Message));
// For deployment
return
Result
.
Failure
(
Domain
.
Errors
.
PersistenceErrors
.
UnableToCompleteTransaction
);
}
#
endregion
return
Result
.
Success
();
}
}
Clinics.Backend/Domain/Errors/PersistenceErrors.cs
0 → 100644
View file @
6fea5553
using
Domain.Shared
;
namespace
Domain.Errors
;
public
static
class
PersistenceErrors
{
public
static
Error
UnableToCompleteTransaction
=>
new
(
"Persistence.UnableToCompleteTransaction"
,
"حدثت مشكلة عند الاتصال مع قاعدة البيانات"
);
}
Clinics.Backend/Persistence/UnitOfWork/UnitOfWork.cs
View file @
6fea5553
...
...
@@ -21,7 +21,7 @@ public class UnitOfWork : IUnitOfWork
catch
(
Exception
)
{
// TODO: Log errors using ILogger
//
throw;
throw
;
}
}
}
Clinics.Backend/Presentation/Controllers/EmployeesController.cs
View file @
6fea5553
...
...
@@ -20,7 +20,9 @@ public class EmployeesController : ControllerBase
[
HttpPost
]
public
async
Task
<
IActionResult
>
Create
(
CreateEmployeeCommand
command
)
{
await
_sender
.
Send
(
command
);
return
Ok
();
var
result
=
await
_sender
.
Send
(
command
);
if
(
result
.
IsSuccess
)
return
Created
();
else
return
BadRequest
(
result
.
Error
.
Message
);
}
}
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