Files
MIESYSTEM/MieSystem/Data/Repositories/ExpedienteRepository.cs
2025-12-25 13:54:49 -06:00

179 lines
5.6 KiB
C#

using Dapper;
using Dapper.Contrib.Extensions;
using MieSystem.Data.Interfaces;
using MieSystem.Models;
namespace MieSystem.Data.Repositories
{
public class ExpedienteRepository : IExpedienteRepository
{
private readonly IDatabaseConnectionFactory _connectionFactory;
public ExpedienteRepository(IDatabaseConnectionFactory databaseConnectionFactory) {
_connectionFactory = databaseConnectionFactory;
}
public async Task<int> CreateAsync(Expediente expediente)
{
using var connection = await _connectionFactory.CreateConnectionAsync();
var sql = @"
INSERT INTO expedientes (
nombre,
apellidos,
fecha_nacimiento,
sexo,
nombre_padre,
nombre_madre,
nombre_responsable,
parentesco_responsable,
direccion,
telefono,
observaciones,
foto_url,
fecha_creacion,
fecha_actualizacion,
activo
) VALUES (
@Nombre,
@Apellidos,
@FechaNacimiento,
@Sexo,
@NombrePadre,
@NombreMadre,
@NombreResponsable,
@ParentescoResponsable,
@Direccion,
@Telefono,
@Observaciones,
@FotoUrl,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP,
@Activo
)
RETURNING id";
var parameters = new
{
expediente.Nombre,
expediente.Apellidos,
FechaNacimiento = expediente.FechaNacimiento.Date, // Solo fecha
expediente.Sexo,
expediente.NombrePadre,
expediente.NombreMadre,
expediente.NombreResponsable,
expediente.ParentescoResponsable,
expediente.Direccion,
expediente.Telefono,
expediente.Observaciones,
expediente.FotoUrl,
expediente.Activo
};
return await connection.ExecuteScalarAsync<int>(sql, parameters);
}
public async Task<bool> DeleteAsync(int id)
{
using var connection = await _connectionFactory.CreateConnectionAsync();
var sql = "DELETE FROM expedientes WHERE id = @Id";
var affected = await connection.ExecuteAsync(sql, new { Id = id });
return affected > 0;
}
public async Task<IEnumerable<Expediente?>> GetActivosAsync()
{
using var connection = await _connectionFactory.CreateConnectionAsync();
if (connection.State == System.Data.ConnectionState.Open)
{
var result = await connection.QueryAsync<Expediente?>(@"SELECT * FROM expedientes where activo = True ORDER BY nombre");
return result;
}
else
{
System.Diagnostics.Debug.WriteLine("Estado de la conexion es:" + connection.State);
return null;
}
}
public async Task<IEnumerable<Expediente?>> GetAllAsync()
{
using var connection = await _connectionFactory.CreateConnectionAsync();
if (connection.State == System.Data.ConnectionState.Open) {
var result = await connection.QueryAsync<Expediente>(
@"SELECT *
FROM expedientes
ORDER BY nombre"
);
return result;
}
else {
System.Diagnostics.Debug.WriteLine("Estado de la conexion es:" + connection.State);
return null;
}
}
public async Task<Expediente?> GetByIdAsync(int id)
{
using var connection = await _connectionFactory.CreateConnectionAsync();
Expediente? expediente = await connection.QueryFirstOrDefaultAsync<Expediente>(
"select *from expedientes where id = @Id",
new { Id = id }
);
return expediente;
}
public async Task<bool> UpdateAsync(Expediente expediente)
{
using var connection = await _connectionFactory.CreateConnectionAsync();
var sql = @"
UPDATE expedientes SET
nombre = @Nombre,
apellidos = @Apellidos,
fecha_nacimiento = @FechaNacimiento,
sexo = @Sexo,
nombre_padre = @NombrePadre,
nombre_madre = @NombreMadre,
nombre_responsable = @NombreResponsable,
parentesco_responsable = @ParentescoResponsable,
direccion = @Direccion,
telefono = @Telefono,
observaciones = @Observaciones,
foto_url = @FotoUrl,
fecha_actualizacion = CURRENT_TIMESTAMP,
activo = @Activo
WHERE id = @Id";
var parameters = new
{
expediente.Id,
expediente.Nombre,
expediente.Apellidos,
FechaNacimiento = expediente.FechaNacimiento.Date, // Solo fecha
expediente.Sexo,
expediente.NombrePadre,
expediente.NombreMadre,
expediente.NombreResponsable,
expediente.ParentescoResponsable,
expediente.Direccion,
expediente.Telefono,
expediente.Observaciones,
expediente.FotoUrl,
expediente.Activo
};
var affectedRows = await connection.ExecuteAsync(sql, parameters);
return affectedRows > 0;
}
}
}