101 lines
3.2 KiB
C#
101 lines
3.2 KiB
C#
using Distribution.Features.Stock;
|
|
using Distribution.Infra.UoW;
|
|
|
|
namespace Distribution.Features.DriverProfile;
|
|
|
|
public class DriverProfileService(IDriverProfileRepository repo, IUnitOfWork uow)
|
|
{
|
|
public async Task<PagedResponse<DriverProfileResponse>> GetAllAsync(int page, int pageSize)
|
|
{
|
|
var total = await repo.GetCountAsync();
|
|
var items = await repo.GetAllAsync(page, pageSize);
|
|
return new PagedResponse<DriverProfileResponse>(page, pageSize, total,
|
|
items.Select(ToResponse).ToList());
|
|
}
|
|
|
|
public async Task<DriverProfileResponse?> GetByIdAsync(Guid id)
|
|
{
|
|
var profile = await repo.GetByIdAsync(id);
|
|
return profile is null || !profile.IsActive ? null : ToResponse(profile);
|
|
}
|
|
|
|
public async Task<DriverProfileResponse> CreateAsync(CreateDriverProfileRequest request, CancellationToken ct = default)
|
|
{
|
|
var existing = await repo.GetByUserIdAsync(request.FK_UserId);
|
|
if (existing is not null && existing.IsActive)
|
|
throw new InvalidOperationException("An active driver profile already exists for this user");
|
|
|
|
var profile = new global::Distribution.Models.DriverProfile
|
|
{
|
|
FK_UserId = request.FK_UserId,
|
|
LicenseNumber = request.LicenseNumber,
|
|
LicenseExpiry = request.LicenseExpiry,
|
|
MedicalClearanceExpiry = request.MedicalClearanceExpiry
|
|
};
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
await repo.AddAsync(profile);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
|
|
var saved = await repo.GetByIdAsync(profile.Id);
|
|
return ToResponse(saved!);
|
|
}
|
|
|
|
public async Task<DriverProfileResponse?> UpdateAsync(Guid id, UpdateDriverProfileRequest request, CancellationToken ct = default)
|
|
{
|
|
var profile = await repo.GetByIdAsync(id);
|
|
if (profile is null || !profile.IsActive) return null;
|
|
|
|
profile.LicenseNumber = request.LicenseNumber;
|
|
profile.LicenseExpiry = request.LicenseExpiry;
|
|
profile.MedicalClearanceExpiry = request.MedicalClearanceExpiry;
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Update(profile);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
|
|
return ToResponse(profile);
|
|
}
|
|
|
|
public async Task<bool> DeactivateAsync(Guid id, CancellationToken ct = default)
|
|
{
|
|
var profile = await repo.GetByIdAsync(id);
|
|
if (profile is null || !profile.IsActive) return false;
|
|
|
|
await uow.BeginAsync(ct);
|
|
try
|
|
{
|
|
repo.Deactivate(profile);
|
|
await uow.CommitAsync(ct);
|
|
}
|
|
catch
|
|
{
|
|
await uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static DriverProfileResponse ToResponse(global::Distribution.Models.DriverProfile p) =>
|
|
new(p.Id, p.FK_UserId, p.User?.FullName ?? string.Empty,
|
|
p.LicenseNumber, p.LicenseExpiry, p.MedicalClearanceExpiry,
|
|
p.IsActive, p.CreatedAt);
|
|
}
|