Commit caf7e1cb authored by Almouhannad's avatar Almouhannad

(B) Add create employee command

parent a23dc773
......@@ -13,4 +13,8 @@
<ItemGroup>
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Employees\Queries\" />
</ItemGroup>
</Project>
using Application.Abstractions.CQRS.Commands;
namespace Application.Employees.Commands.Create;
public class CreateEmployeeCommand : ICommand
{
// Future update: Employees data will be fetched from ID system's API
#region Personal info
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
#endregion
#region Patient info
public DateOnly DateOfBirth { get; set; }
public string Gender { get; set; } = null!;
#endregion
#region Employee info
public string SerialNumber { get; set; } = null!;
public string CenterStatus { get; set; } = null!;
#endregion
#region Optional fields
public DateOnly? StartDate { get; set; } = null;
public string? AcademicQualification { get; set; } = null;
public string? WorkPhone { get; set; } = null;
public string? Location { get; set; } = null;
public string? Specialization { get; set; } = null;
public string? JobStatus { get; set; } = null;
#endregion
}
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees;
using Domain.Repositories;
using Domain.UnitOfWork;
namespace Application.Employees.Commands.Create;
public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeCommand>
{
#region CTOR DI
private readonly IEmployeesRepository _employeesRepository;
private readonly IUnitOfWork _unitOfWork;
public CreateEmployeeCommandHandler(IEmployeesRepository employeesRepository, IUnitOfWork unitOfWork)
{
_employeesRepository = employeesRepository;
_unitOfWork = unitOfWork;
}
#endregion
public async Task Handle(CreateEmployeeCommand request, CancellationToken cancellationToken)
{
#region Create new employee
Employee employee;
try
{
employee = Employee.Create(
request.FirstName, request.MiddleName, request.LastName, // Personal info
request.DateOfBirth, request.Gender, // Patient info
request.SerialNumber, request.CenterStatus, false, // Employee info
request.StartDate, request.AcademicQualification, request.WorkPhone, // additional
request.Location, request.Specialization, request.JobStatus);
}
catch (Exception)
{
throw;
}
#endregion
#region Add to DB
try
{
_employeesRepository.Create(employee);
await _unitOfWork.SaveChangesAsync();
}
catch (Exception)
{
throw;
}
#endregion
}
}
......@@ -95,7 +95,7 @@ public sealed class Employee : Entity
#endregion
#region Create additional info
EmployeeAdditionalInfo additionalInfo;
EmployeeAdditionalInfo? additionalInfo;
try
{
additionalInfo = EmployeeAdditionalInfo.Create(startDate, academicQualification,
......
......@@ -54,7 +54,7 @@ public sealed class EmployeeAdditionalInfo : Entity
#region Methods
#region Static factory
public static EmployeeAdditionalInfo Create(DateOnly? startDate,
public static EmployeeAdditionalInfo? Create(DateOnly? startDate,
string? academicQualification,
string? workPhone,
string? location,
......@@ -62,6 +62,14 @@ public sealed class EmployeeAdditionalInfo : Entity
string? jobStatus,
string? imageUrl)
{
if (startDate is null &&
academicQualification is null &&
workPhone is null &&
location is null &&
specialization is null &&
jobStatus is null &&
imageUrl is null)
return null;
return new EmployeeAdditionalInfo(0,
startDate, academicQualification, workPhone, location, specialization, jobStatus, imageUrl);
}
......
using Domain.Entities.People.Employees;
using Domain.Repositories.Base;
namespace Domain.Repositories;
public interface IEmployeesRepository : IRepository<Employee>
{
}
using Domain.Primitives;
using Domain.Repositories.Base;
using Domain.UnitOfWork;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Specifications.Base;
......@@ -15,7 +14,7 @@ public class Repositroy<TEntity> : IRepository<TEntity> where TEntity : Entity
protected readonly ClinicsDbContext _context;
public Repositroy(ClinicsDbContext context, IUnitOfWork unitOfWork)
public Repositroy(ClinicsDbContext context)
{
_context = context;
}
......
using Domain.Entities.People.Employees;
using Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
namespace Persistence.Repositories;
public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
{
public EmployeesRepository(ClinicsDbContext context) : base(context) {}
#region Create method
public override void Create(Employee entity)
{
_context.Entry(entity.Patient.Gender).State = EntityState.Unchanged;
_context.Set<Employee>().Add(entity);
}
#endregion
}
using Application.Employees.Commands.Create;
using MediatR;
using Microsoft.AspNetCore.Mvc;
namespace Presentation.Controllers;
[Route("api/[controller]")]
[ApiController]
public class EmployeesController : ControllerBase
{
#region DI for MeditR sender
private readonly ISender _sender;
public EmployeesController(ISender sender)
{
_sender = sender;
}
#endregion
[HttpPost]
public async Task<IActionResult> Create(CreateEmployeeCommand command)
{
await _sender.Send(command);
return Ok();
}
}
using Microsoft.AspNetCore.Mvc;
namespace Presentation.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TestsController : ControllerBase
{
[HttpGet]
public ActionResult Index()
{
object respone = new { Result = "Hello world!" };
return Ok(respone);
}
}
}
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