Agregar archivos de proyecto.
This commit is contained in:
11
MieSystem/Data/Interfaces/IDatabaseConnectionFactory.cs
Normal file
11
MieSystem/Data/Interfaces/IDatabaseConnectionFactory.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Data;
|
||||
using Npgsql;
|
||||
|
||||
namespace MieSystem.Data.Interfaces
|
||||
{
|
||||
public interface IDatabaseConnectionFactory
|
||||
{
|
||||
Task<IDbConnection> CreateConnectionAsync();
|
||||
IDbConnection CreateConnection();
|
||||
}
|
||||
}
|
||||
13
MieSystem/Data/Interfaces/IExpedienteRepository.cs
Normal file
13
MieSystem/Data/Interfaces/IExpedienteRepository.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using MieSystem.Models;
|
||||
|
||||
namespace MieSystem.Data.Interfaces
|
||||
{
|
||||
public interface IExpedienteRepository
|
||||
{
|
||||
Task<Expediente?> GetByIdAsync(int id);
|
||||
Task<IEnumerable<Expediente?>> GetAllAsync();
|
||||
Task<int> CreateAsync(Expediente expediente);
|
||||
Task<bool> UpdateAsync(Expediente expediente);
|
||||
Task<bool> DeleteAsync(int id);
|
||||
}
|
||||
}
|
||||
31
MieSystem/Data/Mapper.cs
Normal file
31
MieSystem/Data/Mapper.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using MieSystem.Models;
|
||||
|
||||
namespace MieSystem.Data
|
||||
{
|
||||
public static class Mapper
|
||||
{
|
||||
|
||||
public static Expediente MapFromDictionary(IDictionary<string, object> dict)
|
||||
{
|
||||
return new Expediente
|
||||
{
|
||||
Id = Convert.ToInt32(dict["id"]),
|
||||
Nombre = dict["nombre"]?.ToString(),
|
||||
Apellidos = dict["apellidos"]?.ToString(),
|
||||
FechaNacimiento = Convert.ToDateTime(dict["fecha_nacimiento"]),
|
||||
NombrePadre = dict["nombre_padre"]?.ToString(),
|
||||
NombreMadre = dict["nombre_madre"]?.ToString(),
|
||||
NombreResponsable = dict["nombre_responsable"]?.ToString(),
|
||||
ParentescoResponsable = dict["parentesco_responsable"]?.ToString(),
|
||||
Sexo = dict["sexo"]?.ToString(),
|
||||
Direccion = dict["direccion"]?.ToString(),
|
||||
Telefono = dict["telefono"]?.ToString(),
|
||||
Observaciones = dict["observaciones"]?.ToString(),
|
||||
FotoUrl = dict["foto_url"]?.ToString(),
|
||||
FechaCreacion = Convert.ToDateTime(dict["fecha_creacion"]),
|
||||
FechaActualizacion = Convert.ToDateTime(dict["fecha_actualizacion"]),
|
||||
Activo = Convert.ToBoolean(dict["activo"])
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
45
MieSystem/Data/Repositories/DatabaseConnectionFactory.cs
Normal file
45
MieSystem/Data/Repositories/DatabaseConnectionFactory.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using Dapper;
|
||||
using MieSystem.Data.Interfaces;
|
||||
using Npgsql;
|
||||
using System.Data;
|
||||
|
||||
namespace MieSystem.Data.Repositories
|
||||
{
|
||||
public class DatabaseConnectionFactory : IDatabaseConnectionFactory
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly string? _connectionString;
|
||||
|
||||
public DatabaseConnectionFactory(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_connectionString = _configuration.GetConnectionString("PostgreSQL");
|
||||
|
||||
// Mapear tipos de PostgreSQL a .NET
|
||||
Dapper.DefaultTypeMap.MatchNamesWithUnderscores = true;
|
||||
NpgsqlConnection.GlobalTypeMapper.UseJsonNet();
|
||||
|
||||
}
|
||||
|
||||
public IDbConnection CreateConnection()
|
||||
{
|
||||
var connection = new NpgsqlConnection(_connectionString);
|
||||
|
||||
// Configuración adicional
|
||||
connection.Open();
|
||||
connection.ReloadTypes(); // Para tipos custom de PostgreSQL
|
||||
|
||||
return connection;
|
||||
}
|
||||
|
||||
public async Task<IDbConnection> CreateConnectionAsync()
|
||||
{
|
||||
var connection = new NpgsqlConnection(_connectionString);
|
||||
|
||||
await connection.OpenAsync();
|
||||
await connection.ReloadTypesAsync();
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
}
|
||||
155
MieSystem/Data/Repositories/ExpedienteRepository.cs
Normal file
155
MieSystem/Data/Repositories/ExpedienteRepository.cs
Normal file
@@ -0,0 +1,155 @@
|
||||
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();
|
||||
return await connection.InsertAsync(expediente);
|
||||
}*/
|
||||
|
||||
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?>> GetAllAsync()
|
||||
{
|
||||
using var connection = await _connectionFactory.CreateConnectionAsync();
|
||||
|
||||
if (connection.State == System.Data.ConnectionState.Open) {
|
||||
/*var result = await connection.QueryAsync<Expediente>(
|
||||
@"SELECT
|
||||
id,
|
||||
nombre,
|
||||
apellidos,
|
||||
fecha_nacimiento::timestamp as fecha_nacimiento, -- Convertir a timestamp
|
||||
sexo,
|
||||
nombre_padre,
|
||||
nombre_madre,
|
||||
nombre_responsable,
|
||||
parentesco_responsable,
|
||||
direccion,
|
||||
telefono,
|
||||
observaciones,
|
||||
foto_url,
|
||||
fecha_creacion,
|
||||
fecha_actualizacion,
|
||||
activo
|
||||
FROM expedientes
|
||||
ORDER BY nombre"
|
||||
);*/
|
||||
|
||||
|
||||
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();
|
||||
|
||||
// Con Dapper.Contrib
|
||||
return await connection.UpdateAsync(expediente);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user