shoc-backend/SeaHaven.Services/Implementation/DropdownOptionsService.cs
2026-07-24 10:27:35 -03:00

88 lines
3.3 KiB
C#

using Data.SeaHavenIndustries;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
namespace SeaHaven.Services.Implementation
{
public class DropdownOptionsService : IDropdownOptionsService
{
private readonly IDropdownOptionsDataService _dataService;
public DropdownOptionsService(IDropdownOptionsDataService dataService)
{
_dataService = dataService;
}
public async Task<IEnumerable<DropdownOptionDTO>> GetAllAsync(string? category, CancellationToken cancellationToken)
{
var data = await _dataService.GetAllActiveAsync(category, cancellationToken);
return data.Select(MapToDTO);
}
public async Task<IEnumerable<DropdownOptionDTO>> GetByCategoryAsync(string category, string? parent, CancellationToken cancellationToken)
{
var data = await _dataService.GetByCategoryAsync(category, parent, cancellationToken);
return data.Select(MapToDTO);
}
public async Task<DropdownOptionDTO> CreateAsync(DropdownOptionDTO option, CancellationToken cancellationToken)
{
if (string.IsNullOrWhiteSpace(option.Category) || string.IsNullOrWhiteSpace(option.Value))
throw new InvalidOperationException("Category and Value are required");
if (await _dataService.ExistsAsync(option.Category, option.Value, option.ParentValue, cancellationToken))
throw new InvalidOperationException("This option already exists");
var entity = new DropdownOption
{
Id = 0,
Category = option.Category,
Value = option.Value,
ParentValue = option.ParentValue,
SortOrder = option.SortOrder,
IsActive = true
};
var created = await _dataService.AddAsync(entity, cancellationToken);
return MapToDTO(created);
}
public async Task<DropdownOptionDTO> UpdateAsync(int id, DropdownOptionDTO option, CancellationToken cancellationToken)
{
var item = await _dataService.GetByIdAsync(id, cancellationToken);
if (item == null)
throw new KeyNotFoundException("Option not found");
item.Value = option.Value;
item.ParentValue = option.ParentValue;
item.SortOrder = option.SortOrder;
item.IsActive = option.IsActive;
await _dataService.UpdateAsync(item, cancellationToken);
return MapToDTO(item);
}
public async Task DeleteAsync(int id, CancellationToken cancellationToken)
{
var item = await _dataService.GetByIdAsync(id, cancellationToken);
if (item == null)
throw new KeyNotFoundException("Option not found");
await _dataService.RemoveAsync(item, cancellationToken);
}
private static DropdownOptionDTO MapToDTO(DropdownOption option)
{
return new DropdownOptionDTO
{
Id = option.Id,
Category = option.Category,
Value = option.Value,
ParentValue = option.ParentValue,
SortOrder = option.SortOrder,
IsActive = option.IsActive
};
}
}
}