Commit 6fea5553 authored by Almouhannad's avatar Almouhannad

(B) Apply result pattern on CQRS

parent de273e03
using MediatR; using Domain.Shared;
using MediatR;
namespace Application.Abstractions.CQRS.Commands; namespace Application.Abstractions.CQRS.Commands;
// No response // No response
public interface ICommand : IRequest public interface ICommand : IRequest<Result>
{ {
} }
// With response // With response
public interface ICommand<TResponse> : IRequest<TResponse> public interface ICommand<TResponse> : IRequest<Result<TResponse>>
{ {
} }
using MediatR; using Domain.Shared;
using MediatR;
namespace Application.Abstractions.CQRS.Commands; namespace Application.Abstractions.CQRS.Commands;
// No response // No response
public interface ICommandHandler<TCommand> : IRequestHandler<TCommand> public interface ICommandHandler<TCommand> : IRequestHandler<TCommand, Result>
where TCommand : ICommand where TCommand : ICommand
{ {
} }
// With response // With response
public interface ICommandHandler<TCommand, TResponse> public interface ICommandHandler<TCommand, TResponse>
: IRequestHandler<TCommand, TResponse> : IRequestHandler<TCommand, Result<TResponse>>
where TCommand : ICommand<TResponse> where TCommand : ICommand<TResponse>
{ {
} }
\ No newline at end of file
using MediatR; using Domain.Shared;
using MediatR;
namespace Application.Abstractions.CQRS.Queries; namespace Application.Abstractions.CQRS.Queries;
public interface IQuery<TResponse> : IRequest<TResponse> public interface IQuery<TResponse> : IRequest<Result<TResponse>>
{ {
} }
using MediatR; using Domain.Shared;
using MediatR;
namespace Application.Abstractions.CQRS.Queries; namespace Application.Abstractions.CQRS.Queries;
public interface IQueryHandler<TQuery, TResponse> public interface IQueryHandler<TQuery, TResponse>
: IRequestHandler<TQuery, TResponse> : IRequestHandler<TQuery, Result<TResponse>>
where TQuery : IQuery<TResponse> where TQuery : IQuery<TResponse>
{ {
} }
using Application.Abstractions.CQRS.Commands; using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees; using Domain.Entities.People.Employees;
using Domain.Repositories; using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork; using Domain.UnitOfWork;
namespace Application.Employees.Commands.Create; namespace Application.Employees.Commands.Create;
...@@ -17,39 +18,35 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman ...@@ -17,39 +18,35 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman
} }
#endregion #endregion
public async Task Handle(CreateEmployeeCommand request, CancellationToken cancellationToken) public async Task<Result> Handle(CreateEmployeeCommand request, CancellationToken cancellationToken)
{ {
#region Create new employee Result<Employee> employeeResult =
Employee employee; Employee
try .Create(
{ request.FirstName, request.MiddleName, request.LastName,
employee = Employee.Create(
request.FirstName, request.MiddleName, request.LastName, // Personal info
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 try
{ {
_employeesRepository.Create(employee); _employeesRepository.Create(employeeResult.Value);
await _unitOfWork.SaveChangesAsync(); 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();
} }
} }
using Domain.Shared;
namespace Domain.Errors;
public static class PersistenceErrors
{
public static Error UnableToCompleteTransaction =>
new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
}
...@@ -21,7 +21,7 @@ public class UnitOfWork : IUnitOfWork ...@@ -21,7 +21,7 @@ public class UnitOfWork : IUnitOfWork
catch (Exception) catch (Exception)
{ {
// TODO: Log errors using ILogger // TODO: Log errors using ILogger
//throw; throw;
} }
} }
} }
...@@ -20,7 +20,9 @@ public class EmployeesController : ControllerBase ...@@ -20,7 +20,9 @@ public class EmployeesController : ControllerBase
[HttpPost] [HttpPost]
public async Task<IActionResult> Create(CreateEmployeeCommand command) public async Task<IActionResult> Create(CreateEmployeeCommand command)
{ {
await _sender.Send(command); var result = await _sender.Send(command);
return Ok(); if (result.IsSuccess)
return Created();
else return BadRequest(result.Error.Message);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment