using Dapper;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeLibrary.Data;

public class SqlDataAccess : ISqlDataAccess
{
    private readonly IConfiguration _config;

    public SqlDataAccess(IConfiguration config)
    {
        _config = config;
    }

    public async Task<List<T>> LoadDataAsync<T, U>(string sql,
                                            U parameters,
                                            string connectionStringName = "Default")
    {

        using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionStringName));
        var rows = await connection.QueryAsync<T>(sql, parameters, commandType: CommandType.StoredProcedure);
        return rows.ToList();
    }
}
