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
{
///
/// Asset service implementation
/// Contains business logic, validation, and orchestration for Asset operations
///
public class AssetService : IAssetService
{
private readonly IAssetDataService _assetDataService;
private readonly ICreateAssetValidation _createValidator;
private readonly IUpdateAssetValidation _updateValidator;
public AssetService(
IAssetDataService assetDataService,
ICreateAssetValidation createValidator,
IUpdateAssetValidation updateValidator)
{
_assetDataService = assetDataService;
_createValidator = createValidator;
_updateValidator = updateValidator;
}
public async Task GetAssetByIdAsync(int id)
{
var asset = await _assetDataService.GetByIdAsync(id);
return asset == null ? null : MapToDTO(asset);
}
public async Task GetAssetByIdWithDetailsAsync(int id)
{
var asset = await _assetDataService.GetByIdWithDetailsAsync(id);
return asset == null ? null : MapToDTO(asset);
}
public async Task> GetAllAssetsAsync()
{
var assets = await _assetDataService.GetAllAsync();
return assets.Select(MapToDTO);
}
public async Task> GetAssetsByLocationIdAsync(int locationId)
{
var assets = await _assetDataService.GetAllAsync();
return assets.Where(a => a.LocationId == locationId).Select(MapToDTO);
}
public async Task> GetAssetsPagedAsync(int page, int pageSize, string? search = null)
{
var (items, totalCount) = await _assetDataService.GetPagedAsync(page, pageSize, search);
return new PagedResult
{
Items = items.Select(MapToDTO).ToList(),
TotalCount = totalCount,
Page = page,
PageSize = pageSize
};
}
public async Task CreateAssetAsync(CreateAssetDTO dto, string userId)
{
var validationResult = await _createValidator.ValidateAsync(dto);
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
var asset = new Assets
{
Name = dto.Name,
LocationId = dto.LocationId,
CreatedDate = DateTime.UtcNow,
createdby = userId
};
var created = await _assetDataService.AddAsync(asset);
return MapToDTO(created);
}
public async Task UpdateAssetAsync(int id, UpdateAssetDTO dto, string userId)
{
var validationResult = await _updateValidator.ValidateAsync(dto);
if (!validationResult.IsValid)
{
throw new ValidationException(validationResult.Errors);
}
var asset = await _assetDataService.GetByIdAsync(id);
if (asset == null)
throw new InvalidOperationException($"Asset with ID {id} not found");
if (dto.Name != null) asset.Name = dto.Name;
if (dto.LocationId.HasValue) asset.LocationId = dto.LocationId;
asset.LastModificationTime = DateTime.UtcNow;
if (int.TryParse(userId, out int userIdInt))
asset.LastModifierUserId = userIdInt;
await _assetDataService.UpdateAsync(asset);
return MapToDTO(asset);
}
public async Task DeleteAssetAsync(int id, string userId)
{
var asset = await _assetDataService.GetByIdAsync(id);
if (asset == null)
throw new InvalidOperationException($"Asset with ID {id} not found");
asset.IsDeleted = true;
asset.DeleterUserId = userId;
asset.DeletionTime = DateTime.UtcNow;
await _assetDataService.UpdateAsync(asset);
}
public async Task AssetExistsAsync(int id)
{
return await _assetDataService.ExistsAsync(id);
}
public async Task GetTotalAssetCountAsync()
{
return await _assetDataService.CountAsync();
}
private AssetDTO MapToDTO(Assets asset)
{
return new AssetDTO
{
Id = asset.Id,
Name = asset.Name,
LocationId = asset.LocationId,
LocationName = asset.Location?.Name,
CreatedDate = asset.CreatedDate
};
}
}
}