Commit 400a6cbb authored by Almouhannad's avatar Almouhannad

(B) update attach family member to employee feature

parent c339ee49
......@@ -4,12 +4,12 @@ using Domain.UnitOfWork;
namespace Application.Abstractions.CQRS.Commands;
public abstract class CommandHandler<TCommand> : ICommandHandler<TCommand>
public abstract class CommandHandlerBase<TCommand> : ICommandHandler<TCommand>
where TCommand : ICommand
{
protected readonly IUnitOfWork _unitOfWork;
protected CommandHandler(IUnitOfWork unitOfWork)
protected CommandHandlerBase(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
......@@ -36,11 +36,11 @@ public abstract class CommandHandler<TCommand> : ICommandHandler<TCommand>
}
public abstract class CommandHandler<TCommand, TResponse> : ICommandHandler<TCommand, TResponse>
public abstract class CommandHandlerBase<TCommand, TResponse> : ICommandHandler<TCommand, TResponse>
where TCommand : ICommand<TResponse>
{
protected readonly IUnitOfWork _unitOfWork;
protected CommandHandler(IUnitOfWork unitOfWork)
protected CommandHandlerBase(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
......
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees;
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers;
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>
public class AttachFamilyMemberToEmployeeCommandHandler : CommandHandlerBase<AttachFamilyMemberToEmployeeCommand>
{
#region CTOR DI
private readonly IFamilyMembersRepository _familyMembersRepository;
private readonly IEmployeeFamilyMembersRepository _employeeFamilyMembersRepository;
private readonly IEmployeesRepository _employeesRepository;
private readonly IUnitOfWork _unitOfWork;
public AttachFamilyMemberToEmployeeCommandHandler(IEmployeesRepository employeesRepository, IUnitOfWork unitOfWork)
public AttachFamilyMemberToEmployeeCommandHandler(IUnitOfWork unitOfWork, IFamilyMembersRepository familyMembersRepository, IEmployeesRepository employeesRepository, IEmployeeFamilyMembersRepository employeeFamilyMembersRepository) : base(unitOfWork)
{
_familyMembersRepository = familyMembersRepository;
_employeesRepository = employeesRepository;
_unitOfWork = unitOfWork;
_employeeFamilyMembersRepository = employeeFamilyMembersRepository;
}
#endregion
public async Task<Result> Handle(AttachFamilyMemberToEmployeeCommand request, CancellationToken cancellationToken)
public override async Task<Result> HandleHelper(AttachFamilyMemberToEmployeeCommand request, CancellationToken cancellationToken)
{
#region 1. Create family member
var createdFamilyMemberResult
= FamilyMember
.Create(
request.FirstName, request.MiddleName,
request.LastName, request.DateOfBirth, request.Gender
);
if (createdFamilyMemberResult.IsFailure)
return Result.Failure(createdFamilyMemberResult.Error);
#endregion
#region 2. Add family member to DB
var addedFamilyMemberResult
= await _familyMembersRepository.CreateAsync(createdFamilyMemberResult.Value);
if (addedFamilyMemberResult.IsFailure)
return Result.Failure(addedFamilyMemberResult.Error);
#endregion
#region 3. Get employee from DB
var employeeResult = await _employeesRepository.GetEmployeeBySerialNumberAsync(request.EmployeeSerialNumber);
if (employeeResult.IsFailure)
return Result.Failure(employeeResult.Error);
#endregion
#region 4. Create relation
var employee = employeeResult.Value;
var familyMember = addedFamilyMemberResult.Value;
var employeeFamilyMemberResult = EmployeeFamilyMember.Create(employee, familyMember, request.FamilyRole);
if (employeeFamilyMemberResult.IsFailure)
return Result.Failure(employeeFamilyMemberResult.Error);
#endregion
#region 5. Add reltion to DB
var addedEmployeeFamilyMemberResult
= await _employeeFamilyMembersRepository.CreateAsync(employeeFamilyMemberResult.Value);
if (addedEmployeeFamilyMemberResult.IsFailure)
return Result.Failure(addedEmployeeFamilyMemberResult.Error);
#endregion
#region 6. Attach family member to employee
var employeeFamilyMember = addedEmployeeFamilyMemberResult.Value;
var attachResult = employee.AddFamilyMember(employeeFamilyMember);
if (attachResult.IsFailure)
return Result.Failure(attachResult.Error);
#endregion
#region 7. Confirm update
var updateResult = await _employeesRepository.UpdateAsync(employee);
if (updateResult.IsFailure)
return Result.Failure(updateResult.Error);
#endregion
return Result.Success();
}
......
......@@ -7,7 +7,7 @@ using Domain.UnitOfWork;
namespace Application.Employees.Commands.CreateEmployee;
public class CreateEmployeeCommandHandler : CommandHandler<CreateEmployeeCommand>
public class CreateEmployeeCommandHandler : CommandHandlerBase<CreateEmployeeCommand>
{
#region CTOR DI
private readonly IEmployeesRepository _employeesRepository;
......
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers;
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers.FamilyRoleValues;
using Domain.Entities.People.Employees.Shared;
using Domain.Entities.People.FamilyMembers;
using Domain.Entities.People.Patients;
using Domain.Entities.People.Shared.GenderValues;
using Domain.Primitives;
......@@ -103,32 +102,24 @@ public sealed class Employee : Entity
#endregion
#region Add family member
public Result AddFamilyMember(FamilyMember familyMember, string role)
public Result AddFamilyMember(EmployeeFamilyMember employeeFamilyMember)
{
#region Create family member to attach
Result<EmployeeFamilyMember> employeeFamilyMember =
EmployeeFamilyMember.Create(Id, familyMember.Id, role);
if (employeeFamilyMember.IsFailure)
return Result.Failure(employeeFamilyMember.Error);
#endregion
#region Check valid relation
if (role == FamilyRoles.Husband.Name && Patient.Gender == Genders.Male)
if (employeeFamilyMember.Role == FamilyRoles.Husband
&& employeeFamilyMember.FamilyMember.Patient.Gender == Genders.Male)
return Result.Failure(Errors.DomainErrors.InvalidHusbandRole);
if (role == FamilyRoles.Wife.Name && Patient.Gender == Genders.Female)
return Result.Failure(Errors.DomainErrors.InvalidWifeRole);
if (employeeFamilyMember.Role == FamilyRoles.Wife
&& Patient.Gender == Genders.Female)
#endregion
return Result.Failure(Errors.DomainErrors.InvalidWifeRole);
#region Check duplicate
if (FamilyMembers.Where(fm => fm.FamilyMember == familyMember).ToList().Count > 0)
return Result.Failure(Errors.DomainErrors.RelationAlreadyExist);
#endregion
_familyMembers.Add(employeeFamilyMember.Value);
_familyMembers.Add(employeeFamilyMember);
IsMarried = true;
return Result.Success();
}
......
......@@ -11,10 +11,10 @@ public sealed class EmployeeFamilyMember : Entity
private EmployeeFamilyMember(int id) : base(id)
{
}
private EmployeeFamilyMember(int id, int employeeId, int familyMemberId, FamilyRole role) : base(id)
private EmployeeFamilyMember(int id, Employee employee, FamilyMember familyMember, FamilyRole role) : base(id)
{
EmployeeId = employeeId;
FamilyMemberId = familyMemberId;
Employee = employee;
FamilyMember = familyMember;
Role = role;
}
#endregion
......@@ -46,9 +46,9 @@ public sealed class EmployeeFamilyMember : Entity
#region Methods
#region Static factory
public static Result<EmployeeFamilyMember> Create(int employeeId, int familyMemberId, string role)
public static Result<EmployeeFamilyMember> Create(Employee employee, FamilyMember familyMember, string role)
{
if (employeeId <= 0 || familyMemberId <= 0 || role is null)
if (employee is null || familyMember is null || role is null)
return Result.Failure<EmployeeFamilyMember>(Errors.DomainErrors.InvalidValuesError);
#region Check role
......@@ -73,7 +73,7 @@ public sealed class EmployeeFamilyMember : Entity
#endregion
return new EmployeeFamilyMember(0, employeeId, familyMemberId, selectedRole.Value);
return new EmployeeFamilyMember(0, employee, familyMember, selectedRole.Value);
}
#endregion
......
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers;
using Domain.Repositories.Base;
namespace Domain.Repositories;
public interface IEmployeeFamilyMembersRepository : IRepository<EmployeeFamilyMember>
{
}
using Domain.Entities.People.Employees.Relations.EmployeeFamilyMembers;
using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
namespace Persistence.Repositories;
public class EmployeeFamilyMembersRepository : Repositroy<EmployeeFamilyMember>, IEmployeeFamilyMembersRepository
{
#region CTOR DI for context
public EmployeeFamilyMembersRepository(ClinicsDbContext context) : base(context)
{
}
#endregion
#region Create method
public override Task<Result<EmployeeFamilyMember>> CreateAsync(EmployeeFamilyMember entity)
{
_context.Entry(entity.Role).State = EntityState.Unchanged;
return base.CreateAsync(entity);
}
#endregion
}
using Domain.Entities.People.FamilyMembers;
using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
......@@ -11,4 +13,12 @@ public class FamilyMembersRepository : Repositroy<FamilyMember>, IFamilyMembersR
public FamilyMembersRepository(ClinicsDbContext context) : base(context) {}
#endregion
#region Create method
public override Task<Result<FamilyMember>> CreateAsync(FamilyMember entity)
{
_context.Entry(entity.Patient.Gender).State = EntityState.Unchanged;
return base.CreateAsync(entity);
}
#endregion
}
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