2026-03-31 17:36:04 -03:00
|
|
|
using System.Data;
|
|
|
|
|
using Dapper;
|
|
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using PruebaGentle.Core.Entities;
|
|
|
|
|
using PruebaGentle.Core.Interfaces;
|
|
|
|
|
|
|
|
|
|
namespace PruebaGentle.Infrastructure.Repositories;
|
|
|
|
|
|
|
|
|
|
public class UserRepository : IUserRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly string _connectionString;
|
|
|
|
|
|
|
|
|
|
public UserRepository(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
_connectionString = configuration.GetConnectionString("DefaultConnection")
|
|
|
|
|
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IDbConnection CreateConnection() => new SqlConnection(_connectionString);
|
|
|
|
|
|
|
|
|
|
public async Task<User> CreateAsync(User user)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
var result = await connection.QuerySingleAsync<User>(
|
|
|
|
|
"sp_User_Create",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
user.Username,
|
|
|
|
|
user.PasswordHash,
|
|
|
|
|
user.Email,
|
|
|
|
|
user.NombreCompleto
|
|
|
|
|
},
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<User?> GetByIdAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<User>(
|
|
|
|
|
"sp_User_GetById",
|
|
|
|
|
new { Id = id },
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetAllAsync()
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
return await connection.QueryAsync<User>(
|
|
|
|
|
"sp_User_GetAll",
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<User?> UpdateAsync(User user)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<User>(
|
|
|
|
|
"sp_User_Update",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
user.Id,
|
|
|
|
|
user.Username,
|
|
|
|
|
user.Email,
|
|
|
|
|
user.NombreCompleto
|
|
|
|
|
},
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> DeleteAsync(int id)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
var affected = await connection.ExecuteAsync(
|
|
|
|
|
"sp_User_Delete",
|
|
|
|
|
new { Id = id },
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
return affected > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<User?> GetByUsernameAsync(string username)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
return await connection.QuerySingleOrDefaultAsync<User>(
|
|
|
|
|
"sp_User_GetByUsername",
|
|
|
|
|
new { Username = username },
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
2026-04-01 13:15:22 -03:00
|
|
|
|
|
|
|
|
public async Task<User> RegisterAsync(User user)
|
|
|
|
|
{
|
|
|
|
|
using var connection = CreateConnection();
|
|
|
|
|
var result = await connection.QuerySingleAsync<User>(
|
|
|
|
|
"sp_User_Register",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
user.Username,
|
|
|
|
|
user.PasswordHash,
|
|
|
|
|
user.Email,
|
|
|
|
|
user.NombreCompleto
|
|
|
|
|
},
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2026-03-31 17:36:04 -03:00
|
|
|
}
|