Commit 5e265cb2 authored by Almouhannad's avatar Almouhannad

(B) Validate duplicate serial number in create employee method

parent 041d69cc
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
......@@ -33,6 +34,14 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman
if (employeeResult.IsFailure)
return Result.Failure(employeeResult.Error);
#region Check existed serial number
Result<Employee> existedResult = await _employeesRepository.GetEmployeeBySerialNumberAsync(request.SerialNumber);
if (existedResult.IsSuccess)
return Result.Failure(DomainErrors.EmployeeAlreadyExist);
#endregion
try
{
_employeesRepository.Create(employeeResult.Value);
......
......@@ -25,6 +25,9 @@ public static class DomainErrors
public static Error PhoneAlreadyExist =>
new("Domain.PhoneAlreadyExist", "رقم الهاتف موجود بالفعل");
public static Error EmployeeAlreadyExist =>
new("Domain.EmployeeAlreadyExist", "الموظف موجود بالفعل");
public static Error VisitAlreadyHasThisMedicine =>
new("Domain.VisitAlreadyHasThisMedicine", "تحتوي الوصفة الطبية بالفعل على الدواء الذي تحاول اضافته");
......@@ -32,6 +35,6 @@ public static class DomainErrors
new("Domain.VisitAlreadyHasThisMedicalTest", "تحتوي هذه الزيارة بالفعل على التحليل الطبي الذي تحاول اضافته");
public static Error VisitAlreadyHasThisMedicalImage =>
new("Domain.PatientAlreadyHasThisMedicine", "تحتوي هذه الزيارة بالفعل على الصورة التي تحاول اضافتها");
new("Domain.PatientAlreadyHasThisMedicine", "تحتوي هذه الزيارة بالفعل على الصورة التي تحاول اضافتها");
}
......@@ -6,4 +6,7 @@ public static class PersistenceErrors
{
public static Error UnableToCompleteTransaction =>
new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
public static Error NotFound =>
new("Persistence.NotFound", "الغرض المطلوب غير موجود");
}
using Domain.Entities.People.Employees;
using Domain.Repositories.Base;
using Domain.Shared;
namespace Domain.Repositories;
public interface IEmployeesRepository : IRepository<Employee>
{
#region Get by serial number
public Task<Result<Employee>> GetEmployeeBySerialNumberAsync(string serialNumber);
#endregion
}
using Domain.Entities.People.Employees;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Persistence.Context;
using Persistence.Repositories.Base;
......@@ -18,4 +20,14 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
}
#endregion
#region Get by serial Number
public async Task<Result<Employee>> GetEmployeeBySerialNumberAsync (string serialNumber)
{
var all = await _context.Set<Employee>().Where(employee => employee.SerialNumber == serialNumber).ToListAsync();
if (all.Count != 1)
return Result.Failure<Employee>(PersistenceErrors.NotFound);
return Result.Success<Employee>(all.First());
}
#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