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
c339ee49
Commit
c339ee49
authored
Aug 19, 2024
by
Almouhannad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
(B) Update create employee command
parent
9ed541c5
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
88 additions
and
64 deletions
+88
-64
CommandHandler.cs
.../Application/Abstractions/CQRS/Commands/CommandHandler.cs
+67
-0
AttachFamilyMemberToEmployeeCommandHandler.cs
...rToEmployee/AttachFamilyMemberToEmployeeCommandHandler.cs
+0
-36
CreateEmployeeCommandHandler.cs
...s/Commands/CreateEmployee/CreateEmployeeCommandHandler.cs
+14
-21
Repositroy.cs
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
+5
-5
EmployeesRepository.cs
...s.Backend/Persistence/Repositories/EmployeesRepository.cs
+2
-2
No files found.
Clinics.Backend/Application/Abstractions/CQRS/Commands/CommandHandler.cs
0 → 100644
View file @
c339ee49
using
Domain.Errors
;
using
Domain.Shared
;
using
Domain.UnitOfWork
;
namespace
Application.Abstractions.CQRS.Commands
;
public
abstract
class
CommandHandler
<
TCommand
>
:
ICommandHandler
<
TCommand
>
where
TCommand
:
ICommand
{
protected
readonly
IUnitOfWork
_unitOfWork
;
protected
CommandHandler
(
IUnitOfWork
unitOfWork
)
{
_unitOfWork
=
unitOfWork
;
}
public
async
Task
<
Result
>
Handle
(
TCommand
request
,
CancellationToken
cancellationToken
)
{
using
var
transaction
=
_unitOfWork
.
BeginTransaction
();
try
{
var
result
=
await
HandleHelper
(
request
,
cancellationToken
);
if
(
result
.
IsSuccess
)
{
transaction
.
Commit
();
}
return
result
;
}
catch
(
Exception
)
{
return
Result
.
Failure
(
PersistenceErrors
.
UnableToCompleteTransaction
);
}
}
public
abstract
Task
<
Result
>
HandleHelper
(
TCommand
request
,
CancellationToken
cancellationToken
);
}
public
abstract
class
CommandHandler
<
TCommand
,
TResponse
>
:
ICommandHandler
<
TCommand
,
TResponse
>
where
TCommand
:
ICommand
<
TResponse
>
{
protected
readonly
IUnitOfWork
_unitOfWork
;
protected
CommandHandler
(
IUnitOfWork
unitOfWork
)
{
_unitOfWork
=
unitOfWork
;
}
public
async
Task
<
Result
<
TResponse
>>
Handle
(
TCommand
request
,
CancellationToken
cancellationToken
)
{
using
var
transaction
=
_unitOfWork
.
BeginTransaction
();
try
{
var
result
=
await
HandleHelper
(
request
,
cancellationToken
);
if
(
result
.
IsSuccess
)
{
transaction
.
Commit
();
}
return
result
;
}
catch
(
Exception
)
{
return
Result
.
Failure
<
TResponse
>(
PersistenceErrors
.
UnableToCompleteTransaction
);
}
}
public
abstract
Task
<
Result
<
TResponse
>>
HandleHelper
(
TCommand
request
,
CancellationToken
cancellationToken
);
}
Clinics.Backend/Application/Employees/Commands/AttachFamilyMemberToEmployee/AttachFamilyMemberToEmployeeCommandHandler.cs
View file @
c339ee49
...
...
@@ -22,43 +22,7 @@ public class AttachFamilyMemberToEmployeeCommandHandler : ICommandHandler<Attach
#
endregion
public
async
Task
<
Result
>
Handle
(
AttachFamilyMemberToEmployeeCommand
request
,
CancellationToken
cancellationToken
)
{
#
region
1.
Create
family
member
Result
<
FamilyMember
>
familyMemberResult
=
FamilyMember
.
Create
(
request
.
FirstName
,
request
.
MiddleName
,
request
.
LastName
,
request
.
DateOfBirth
,
request
.
Gender
);
if
(
familyMemberResult
.
IsFailure
)
return
Result
.
Failure
(
familyMemberResult
.
Error
);
#
endregion
#
region
2.
Get
employee
by
serial
number
Result
<
Employee
>
employeeResult
=
await
_employeesRepository
.
GetEmployeeBySerialNumberAsync
(
request
.
EmployeeSerialNumber
);
if
(
employeeResult
.
IsFailure
)
return
Result
.
Failure
(
employeeResult
.
Error
);
#
endregion
#
region
3.
Attach
family
member
to
employee
Result
attachFamilyMemberResult
=
employeeResult
.
Value
.
AddFamilyMember
(
familyMemberResult
.
Value
,
request
.
FamilyRole
);
if
(
attachFamilyMemberResult
.
IsFailure
)
return
Result
.
Failure
(
attachFamilyMemberResult
.
Error
);
#
endregion
#
region
4.
Confirm
update
try
{
_employeesRepository
.
Update
(
employeeResult
.
Value
);
await
_unitOfWork
.
SaveChangesAsync
();
}
catch
(
Exception
)
{
return
Result
.
Failure
(
PersistenceErrors
.
UnableToCompleteTransaction
);
}
return
Result
.
Success
();
#
endregion
}
}
Clinics.Backend/Application/Employees/Commands/CreateEmployee/CreateEmployeeCommandHandler.cs
View file @
c339ee49
...
...
@@ -7,20 +7,22 @@ using Domain.UnitOfWork;
namespace
Application.Employees.Commands.CreateEmployee
;
public
class
CreateEmployeeCommandHandler
:
I
CommandHandler
<
CreateEmployeeCommand
>
public
class
CreateEmployeeCommandHandler
:
CommandHandler
<
CreateEmployeeCommand
>
{
#
region
CTOR
DI
private
readonly
IEmployeesRepository
_employeesRepository
;
private
readonly
IUnitOfWork
_unitOfWork
;
public
CreateEmployeeCommandHandler
(
I
EmployeesRepository
employeesRepository
,
IUnitOfWork
unitOfWork
)
public
CreateEmployeeCommandHandler
(
I
UnitOfWork
unitOfWork
,
IEmployeesRepository
employeesRepository
)
:
base
(
unitOfWork
)
{
_employeesRepository
=
employeesRepository
;
_unitOfWork
=
unitOfWork
;
}
#
endregion
public
async
Task
<
Result
>
Handle
(
CreateEmployeeCommand
request
,
CancellationToken
cancellationToken
)
public
override
async
Task
<
Result
>
HandleHelper
(
CreateEmployeeCommand
request
,
CancellationToken
cancellationToken
)
{
#
region
1.
Create
employee
Result
<
Employee
>
employeeResult
=
Employee
.
Create
(
...
...
@@ -33,28 +35,19 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman
);
if
(
employeeResult
.
IsFailure
)
return
Result
.
Failure
(
employeeResult
.
Error
);
#
endregion
#
region
Check
existed
serial
number
#
region
2.
Check
existed
serial
number
Result
<
Employee
>
existedResult
=
await
_employeesRepository
.
GetEmployeeBySerialNumberAsync
(
request
.
SerialNumber
);
if
(
existedResult
.
IsSuccess
)
return
Result
.
Failure
(
DomainErrors
.
EmployeeAlreadyExist
);
#
endregion
try
{
_employeesRepository
.
Create
(
employeeResult
.
Value
);
await
_unitOfWork
.
SaveChangesAsync
();
}
catch
(
Exception
exp
)
{
// For debugging
//return Result.Failure(new Error("Persistence.UnableToSaveTransaction", exp.Message));
// For deployment
return
Result
.
Failure
(
PersistenceErrors
.
UnableToCompleteTransaction
);
}
#
region
3.
Add
to
DB
var
createResult
=
await
_employeesRepository
.
CreateAsync
(
employeeResult
.
Value
);
if
(
createResult
.
IsFailure
)
return
Result
.
Failure
(
createResult
.
Error
);
#
endregion
return
Result
.
Success
();
}
...
...
Clinics.Backend/Persistence/Repositories/Base/Repositroy.cs
View file @
c339ee49
...
...
@@ -34,7 +34,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
region
Create
operation
public
async
Task
<
Result
<
TEntity
>>
CreateAsync
(
TEntity
entity
)
public
virtual
async
Task
<
Result
<
TEntity
>>
CreateAsync
(
TEntity
entity
)
{
try
{
...
...
@@ -52,7 +52,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
region
Read
operations
public
async
Task
<
Result
<
TEntity
>>
GetByIdAsync
(
int
id
)
public
virtual
async
Task
<
Result
<
TEntity
>>
GetByIdAsync
(
int
id
)
{
try
{
...
...
@@ -66,7 +66,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
}
public
async
Task
<
Result
<
ICollection
<
TEntity
>>>
GetAllAsync
()
public
virtual
async
Task
<
Result
<
ICollection
<
TEntity
>>>
GetAllAsync
()
{
try
{
...
...
@@ -83,7 +83,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
endregion
#
region
Update
operation
public
async
Task
<
Result
>
UpdateAsync
(
TEntity
entity
)
public
virtual
async
Task
<
Result
>
UpdateAsync
(
TEntity
entity
)
{
try
{
...
...
@@ -99,7 +99,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
#
endregion
#
region
Delete
operation
public
async
Task
<
Result
>
DeleteAsync
(
TEntity
entity
)
public
virtual
async
Task
<
Result
>
DeleteAsync
(
TEntity
entity
)
{
try
{
...
...
Clinics.Backend/Persistence/Repositories/EmployeesRepository.cs
View file @
c339ee49
...
...
@@ -13,10 +13,10 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
public
EmployeesRepository
(
ClinicsDbContext
context
)
:
base
(
context
)
{}
#
region
Create
method
public
override
void
Create
(
Employee
entity
)
public
override
Task
<
Result
<
Employee
>>
CreateAsync
(
Employee
entity
)
{
_context
.
Entry
(
entity
.
Patient
.
Gender
).
State
=
EntityState
.
Unchanged
;
_context
.
Set
<
Employee
>().
Add
(
entity
);
return
base
.
CreateAsync
(
entity
);
}
#
endregion
...
...
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