shoc-backend/SeaHaven.Services/Implementation/AssetService.cs
2026-05-14 11:00:12 -05:00

144 lines
4.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>
/// Asset service implementation
/// Contains business logic, validation, and orchestration for Asset operations
/// </summary>
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<AssetDTO?> GetAssetByIdAsync(int id)
{
var asset = await _assetDataService.GetByIdAsync(id);
return asset == null ? null : MapToDTO(asset);
}
public async Task<AssetDTO?> GetAssetByIdWithDetailsAsync(int id)
{
var asset = await _assetDataService.GetByIdWithDetailsAsync(id);
return asset == null ? null : MapToDTO(asset);
}
public async Task<IEnumerable<AssetDTO>> GetAllAssetsAsync()
{
var assets = await _assetDataService.GetAllAsync();
return assets.Select(MapToDTO);
}
public async Task<IEnumerable<AssetDTO>> GetAssetsByLocationIdAsync(int locationId)
{
var assets = await _assetDataService.GetAllAsync();
return assets.Where(a => a.LocationId == locationId).Select(MapToDTO);
}
public async Task<PagedResult<AssetDTO>> GetAssetsPagedAsync(int page, int pageSize, string? search = null)
{
var (items, totalCount) = await _assetDataService.GetPagedAsync(page, pageSize, search);
return new PagedResult<AssetDTO>
{
Items = items.Select(MapToDTO).ToList(),
TotalCount = totalCount,
Page = page,
PageSize = pageSize
};
}
public async Task<AssetDTO> 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<AssetDTO> 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<bool> AssetExistsAsync(int id)
{
return await _assetDataService.ExistsAsync(id);
}
public async Task<int> 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
};
}
}
}