using IWS5.Core.DTO;
using IWS5.Core.Models;
using IWS5.Core.Models.Request;
using IWS5.Core.Services;
using IWS5.DatabaseAccess.Interfaces;

namespace IWS5.Services;

public class ConnectionService(IConnectionRepository connectionRepository) : IConnectionService
{
    public async Task<Response<ConnectionListItemDTO[]>> ListConnections(ListConnectionRequest request)
    {
        var connections = await connectionRepository.ListAsync(connection => connection.Type == (int)request.ATConnectionType);

        var dtos = connections.Select(connection => new ConnectionListItemDTO(
            connection.Id,
            connection.Name,
            connection.Description ?? string.Empty,
            connection.ConnectionString,
            connection.UserName,
            connection.Password));

        return new Response<ConnectionListItemDTO[]>(dtos.ToArray(), true);
    }
}
