mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 16:33:12 +00:00
160 lines
5.9 KiB
C#
160 lines
5.9 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentValidation;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Interfaces;
|
|
using SeaHaven.Services.Validation;
|
|
|
|
namespace SeaHaven.Services.Implementation
|
|
{
|
|
/// <summary>
|
|
/// Dispatch service implementation
|
|
/// Contains business logic, validation, and orchestration for Dispatch operations
|
|
/// </summary>
|
|
public class DispatchService : IDispatchService
|
|
{
|
|
private readonly IDispatchDataService _dispatchDataService;
|
|
private readonly ICreateDispatchValidation _createValidator;
|
|
private readonly IUpdateDispatchValidation _updateValidator;
|
|
|
|
public DispatchService(
|
|
IDispatchDataService dispatchDataService,
|
|
ICreateDispatchValidation createValidator,
|
|
IUpdateDispatchValidation updateValidator)
|
|
{
|
|
_dispatchDataService = dispatchDataService;
|
|
_createValidator = createValidator;
|
|
_updateValidator = updateValidator;
|
|
}
|
|
|
|
public async Task<DispatchDTO?> GetDispatchByIdAsync(int id)
|
|
{
|
|
var dispatch = await _dispatchDataService.GetByIdAsync(id);
|
|
return dispatch == null ? null : MapToDTO(dispatch);
|
|
}
|
|
|
|
public async Task<DispatchDTO?> GetDispatchByIdWithDetailsAsync(int id)
|
|
{
|
|
var dispatch = await _dispatchDataService.GetByIdWithDetailsAsync(id);
|
|
return dispatch == null ? null : MapToDTO(dispatch);
|
|
}
|
|
|
|
public async Task<IEnumerable<DispatchDTO>> GetAllDispatchesAsync()
|
|
{
|
|
var dispatches = await _dispatchDataService.GetAllAsync();
|
|
return dispatches.Select(MapToDTO);
|
|
}
|
|
|
|
public async Task<IEnumerable<DispatchDTO>> GetDispatchesByVendorIdAsync(int vendorId)
|
|
{
|
|
var dispatches = await _dispatchDataService.GetByVendorIdAsync(vendorId);
|
|
return dispatches.Select(MapToDTO);
|
|
}
|
|
|
|
public async Task<PagedResult<DispatchDTO>> GetDispatchesPagedAsync(int page, int pageSize, string? search = null, string? status = null)
|
|
{
|
|
var (items, totalCount) = await _dispatchDataService.GetPagedAsync(page, pageSize, search, status);
|
|
return new PagedResult<DispatchDTO>
|
|
{
|
|
Items = items.Select(MapToDTO).ToList(),
|
|
TotalCount = totalCount,
|
|
Page = page,
|
|
PageSize = pageSize
|
|
};
|
|
}
|
|
|
|
public async Task<DispatchDTO> CreateDispatchAsync(CreateDispatchDTO dto, string userId)
|
|
{
|
|
var validationResult = await _createValidator.ValidateAsync(dto);
|
|
if (!validationResult.IsValid)
|
|
{
|
|
throw new ValidationException(validationResult.Errors);
|
|
}
|
|
|
|
var dispatchNumber = await GenerateNextDispatchNumberAsync();
|
|
|
|
var dispatch = new Dispatch
|
|
{
|
|
VendorId = dto.VendorId,
|
|
ScheduledDate = dto.ScheduledDate,
|
|
Status = dto.Status ?? "Pending",
|
|
DispatchNumber = dispatchNumber,
|
|
CreatedDate = DateTime.UtcNow,
|
|
createdby = userId
|
|
};
|
|
|
|
var created = await _dispatchDataService.AddAsync(dispatch);
|
|
return MapToDTO(created);
|
|
}
|
|
|
|
public async Task<DispatchDTO> UpdateDispatchAsync(int id, UpdateDispatchDTO dto, string userId)
|
|
{
|
|
var validationResult = await _updateValidator.ValidateAsync(dto);
|
|
if (!validationResult.IsValid)
|
|
{
|
|
throw new ValidationException(validationResult.Errors);
|
|
}
|
|
|
|
var dispatch = await _dispatchDataService.GetByIdAsync(id);
|
|
if (dispatch == null)
|
|
throw new InvalidOperationException($"Dispatch with ID {id} not found");
|
|
|
|
if (dto.VendorId.HasValue) dispatch.VendorId = dto.VendorId.Value;
|
|
if (dto.ScheduledDate.HasValue) dispatch.ScheduledDate = dto.ScheduledDate;
|
|
if (dto.CompletedDate.HasValue) dispatch.CompletedDate = dto.CompletedDate;
|
|
if (dto.Status != null) dispatch.Status = dto.Status;
|
|
|
|
dispatch.LastModificationTime = DateTime.UtcNow;
|
|
if (int.TryParse(userId, out int userIdInt))
|
|
dispatch.LastModifierUserId = userIdInt;
|
|
|
|
await _dispatchDataService.UpdateAsync(dispatch);
|
|
return MapToDTO(dispatch);
|
|
}
|
|
|
|
public async Task DeleteDispatchAsync(int id, string userId)
|
|
{
|
|
var dispatch = await _dispatchDataService.GetByIdAsync(id);
|
|
if (dispatch == null)
|
|
throw new InvalidOperationException($"Dispatch with ID {id} not found");
|
|
|
|
dispatch.IsDeleted = true;
|
|
dispatch.DeleterUserId = userId;
|
|
dispatch.DeletionTime = DateTime.UtcNow;
|
|
|
|
await _dispatchDataService.UpdateAsync(dispatch);
|
|
}
|
|
|
|
public async Task<bool> DispatchExistsAsync(int id)
|
|
{
|
|
return await _dispatchDataService.ExistsAsync(id);
|
|
}
|
|
|
|
public async Task<string> GenerateNextDispatchNumberAsync()
|
|
{
|
|
var count = await _dispatchDataService.CountAsync();
|
|
return $"DISP-{(count + 1):D6}";
|
|
}
|
|
|
|
public async Task<int> GetTotalDispatchCountAsync()
|
|
{
|
|
return await _dispatchDataService.CountAsync();
|
|
}
|
|
|
|
private DispatchDTO MapToDTO(Dispatch dispatch)
|
|
{
|
|
return new DispatchDTO
|
|
{
|
|
Id = dispatch.Id,
|
|
DispatchNumber = dispatch.DispatchNumber,
|
|
Status = dispatch.Status,
|
|
VendorId = dispatch.VendorId,
|
|
VendorName = dispatch.Vendor?.CompanyName,
|
|
ScheduledDate = dispatch.ScheduledDate,
|
|
CompletedDate = dispatch.CompletedDate,
|
|
CreatedDate = dispatch.CreatedDate,
|
|
CreatedBy = dispatch.createdby
|
|
};
|
|
}
|
|
}
|
|
}
|