Commit 9c1bae96 authored by Almouhannad's avatar Almouhannad

(B) Add attach family member to employee feature

parent f55bf66c
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Employees\Commands\AttachFamilyMemberToEmployee\" />
<Folder Include="Employees\Queries\" /> <Folder Include="Employees\Queries\" />
</ItemGroup> </ItemGroup>
</Project> </Project>
using Application.Abstractions.CQRS.Commands;
namespace Application.Employees.Commands.AttachFamilyMemberToEmployee;
public class AttachFamilyMemberToEmployeeCommand : ICommand
{
public string EmployeeSerialNumber { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
public DateOnly DateOfBirth { get; set; }
public string Gender { get; set; } = null!;
public string FamilyRole { get; set; } = null!;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees;
using Domain.Entities.People.FamilyMembers;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Employees.Commands.AttachFamilyMemberToEmployee;
public class AttachFamilyMemberToEmployeeCommandHandler : ICommandHandler<AttachFamilyMemberToEmployeeCommand>
{
#region CTOR DI
private readonly IEmployeesRepository _employeesRepository;
private readonly IUnitOfWork _unitOfWork;
public AttachFamilyMemberToEmployeeCommandHandler(IEmployeesRepository employeesRepository, IUnitOfWork unitOfWork)
{
_employeesRepository = employeesRepository;
_unitOfWork = unitOfWork;
}
#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
}
}
using Application.Employees.Commands.CreateEmployee; using Application.Employees.Commands.AttachFamilyMemberToEmployee;
using Application.Employees.Commands.CreateEmployee;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Presentation.Controllers.Base; using Presentation.Controllers.Base;
...@@ -17,11 +18,20 @@ public class EmployeesController : ApiController ...@@ -17,11 +18,20 @@ public class EmployeesController : ApiController
[HttpPost] [HttpPost]
public async Task<IActionResult> Create(CreateEmployeeCommand command) public async Task<IActionResult> Create([FromBody] CreateEmployeeCommand command)
{ {
var result = await _sender.Send(command); var result = await _sender.Send(command);
if (result.IsFailure) if (result.IsFailure)
return HandleFailure(result); return HandleFailure(result);
return Created(); return Created();
} }
[HttpPut("FamilyMembers")]
public async Task<IActionResult> AttachFamilyMember([FromBody] AttachFamilyMemberToEmployeeCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok();
}
} }
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