Commit 97f00868 authored by Almouhannad's avatar Almouhannad

(B) Add get by id query for employee

parent 91db3405
using Application.Abstractions.CQRS.Queries;
using Application.Employees.Queries.GetBySerialNumber;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Employees.Queries.GetById;
public class GetEmployeeByIdHandler : IQueryHandler<GetEmployeeByIdQuery, GetEmployeeByIdResponse>
{
#region CTOR DI
private readonly IEmployeesRepository _employeesRepository;
public GetEmployeeByIdHandler(IEmployeesRepository employeesRepository)
{
_employeesRepository = employeesRepository;
}
#endregion
public async Task<Result<GetEmployeeByIdResponse>> Handle(GetEmployeeByIdQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch from persistence
var employeeFromPersistenceResult = await _employeesRepository.GetByIdAsync(request.Id);
if (employeeFromPersistenceResult.IsFailure)
return Result.Failure<GetEmployeeByIdResponse>(employeeFromPersistenceResult.Error);
var employee = employeeFromPersistenceResult.Value;
#endregion
return Result.Success<GetEmployeeByIdResponse>
(GetEmployeeByIdResponse.GetResponse(employee));
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Employees.Queries.GetById;
public class GetEmployeeByIdQuery : IQuery<GetEmployeeByIdResponse>
{
public int Id { get; set; }
}
using Application.Employees.Queries.GetBySerialNumber;
using Domain.Entities.People.Employees;
namespace Application.Employees.Queries.GetById;
public class GetEmployeeByIdResponse
{
public int Id { get; set; }
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
public string Gender { get; set; } = null!;
public DateOnly DateOfBirth { get; set; }
public string SerialNumber { get; set; } = null!;
public string CenterStatus { get; set; } = null!;
public static GetEmployeeByIdResponse GetResponse(Employee employee)
{
return new GetEmployeeByIdResponse
{
Id = employee.Id,
FirstName = employee.Patient.PersonalInfo.FirstName,
MiddleName = employee.Patient.PersonalInfo.MiddleName,
LastName = employee.Patient.PersonalInfo.LastName,
Gender = employee.Patient.Gender.Name,
DateOfBirth = employee.Patient.DateOfBirth,
SerialNumber = employee.SerialNumber,
CenterStatus = employee.CenterStatus
};
}
}
......@@ -21,6 +21,23 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
}
#endregion
#region Read methods
public override async Task<Result<Employee>> GetByIdAsync(int id)
{
try
{
var query = _context.Set<Employee>()
.Where(employee => employee.Id == id);
var result = await query.FirstAsync();
return await GetEmployeeBySerialNumberAsync(result.SerialNumber);
}
catch (Exception)
{
return Result.Failure<Employee>(PersistenceErrors.NotFound);
}
}
#endregion
#region Get by serial Number
public async Task<Result<Employee>> GetEmployeeBySerialNumberAsync(string serialNumber)
{
......@@ -36,5 +53,6 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
return Result.Failure<Employee>(DomainErrors.SerialNumberNotFound);
return Result.Success(result.First());
}
#endregion
}
using Application.Employees.Commands.AttachFamilyMemberToEmployee;
using Application.Employees.Commands.CreateEmployee;
using Application.Employees.Queries.GetById;
using Application.Employees.Queries.GetBySerialNumber;
using Domain.Entities.Identity.UserRoles;
using MediatR;
......@@ -39,6 +40,18 @@ public class EmployeesController : ApiController
return Ok(result.Value);
}
//[Authorize(Roles = Roles.ReceptionistName)]
[HttpGet("{id:int}")] // It's a get, but serial number shouldn't be sent via routing, so we'll use post
public async Task<IActionResult> GetById([FromRoute] int id)
{
var query = new GetEmployeeByIdQuery { Id = id };
var result = await _sender.Send(query);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
//[Authorize(Roles = Roles.ReceptionistName)]
[HttpPut("FamilyMembers")]
public async Task<IActionResult> AttachFamilyMember([FromBody] AttachFamilyMemberToEmployeeCommand command)
......
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