Unverified Commit e615d150 authored by Almouhannad Hafez's avatar Almouhannad Hafez Committed by GitHub

Merge pull request #9 from Almouhannad/B_Add-fluent-validation

(B) Validate duplicate serial number in create employee method
parents 9dc19d17 5e265cb2
using Application.Abstractions.CQRS.Commands; using Application.Abstractions.CQRS.Commands;
using Domain.Entities.People.Employees; using Domain.Entities.People.Employees;
using Domain.Errors;
using Domain.Repositories; using Domain.Repositories;
using Domain.Shared; using Domain.Shared;
using Domain.UnitOfWork; using Domain.UnitOfWork;
...@@ -33,6 +34,14 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman ...@@ -33,6 +34,14 @@ public class CreateEmployeeCommandHandler : ICommandHandler<CreateEmployeeComman
if (employeeResult.IsFailure) if (employeeResult.IsFailure)
return Result.Failure(employeeResult.Error); 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 try
{ {
_employeesRepository.Create(employeeResult.Value); _employeesRepository.Create(employeeResult.Value);
......
...@@ -25,6 +25,9 @@ public static class DomainErrors ...@@ -25,6 +25,9 @@ public static class DomainErrors
public static Error PhoneAlreadyExist => public static Error PhoneAlreadyExist =>
new("Domain.PhoneAlreadyExist", "رقم الهاتف موجود بالفعل"); new("Domain.PhoneAlreadyExist", "رقم الهاتف موجود بالفعل");
public static Error EmployeeAlreadyExist =>
new("Domain.EmployeeAlreadyExist", "الموظف موجود بالفعل");
public static Error VisitAlreadyHasThisMedicine => public static Error VisitAlreadyHasThisMedicine =>
new("Domain.VisitAlreadyHasThisMedicine", "تحتوي الوصفة الطبية بالفعل على الدواء الذي تحاول اضافته"); new("Domain.VisitAlreadyHasThisMedicine", "تحتوي الوصفة الطبية بالفعل على الدواء الذي تحاول اضافته");
......
...@@ -6,4 +6,7 @@ public static class PersistenceErrors ...@@ -6,4 +6,7 @@ public static class PersistenceErrors
{ {
public static Error UnableToCompleteTransaction => public static Error UnableToCompleteTransaction =>
new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات"); new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
public static Error NotFound =>
new("Persistence.NotFound", "الغرض المطلوب غير موجود");
} }
using Domain.Entities.People.Employees; using Domain.Entities.People.Employees;
using Domain.Repositories.Base; using Domain.Repositories.Base;
using Domain.Shared;
namespace Domain.Repositories; namespace Domain.Repositories;
public interface IEmployeesRepository : IRepository<Employee> 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.Entities.People.Employees;
using Domain.Errors;
using Domain.Repositories; using Domain.Repositories;
using Domain.Shared;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Persistence.Context; using Persistence.Context;
using Persistence.Repositories.Base; using Persistence.Repositories.Base;
...@@ -18,4 +20,14 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository ...@@ -18,4 +20,14 @@ public class EmployeesRepository : Repositroy<Employee>, IEmployeesRepository
} }
#endregion #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