forked from EM40a/Reserva-Ya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHotelContext.cs
145 lines (133 loc) · 4.73 KB
/
HotelContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Entidades.Excepciones;
using Entidades.Modelos;
using static Entidades.Modelos.Reserva;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.Data.SqlClient;
using Microsoft.Identity.Client;
using System.Reflection;
namespace Entidades.BaseDeDatos
{
/// <summary>
/// Clase que representa el gestor de la base de datos
/// </summary>
public class HotelContext : DbContext, IComandosDb
{
public DbSet<Reserva> Reservas { get; set; }
public DbSet<Huesped> Huespedes { get; set; }
#region Metodos
/// <summary>
/// Agrega un registro a la base de datos
/// </summary>
/// <typeparam name="T">Sera un tipo de registro (Reserva o Huesped)</typeparam>
public void AgregarRegistro<T>(T registro) where T : class, new()
{
try
{
Add(registro);
SaveChanges();
}
catch(DbUpdateException)
{
throw new BaseDeDatosException("No se puede agregar explicitamente un campo auto-generado");
}
catch (SqlException)
{
throw new BaseDeDatosException("No se pudo agregar el registro");
}
}
public void AgregarRegistro<T>(List<T> registros) where T : class, new()
{
foreach (T registro in registros)
{
AgregarRegistro(registro);
}
}
/// <summary>
/// Elimina un registro de la base de datos
/// </summary>
/// <typeparam name="T">Sera un tipo de registro (Reserva o Huesped)</typeparam>
/// <param name="id">El parametro por el que se elimina el registro</param>
/// <exception cref="ArgumentNullException"></exception>
public T? EliminarRegistro<T>(int id) where T : class, new()
{
try
{
T? registro = SeleccionarRegistro<T>(id);
Remove(registro);
SaveChanges();
return registro;
}
catch(ArgumentNullException)
{
throw new BaseDeDatosException($"No se encotro el/la {typeof(T).Name}");
}
catch(Exception)
{
throw new BaseDeDatosException($"No se pudo eliminar el/la {typeof(T).Name}");
}
}
public bool ActualizarRegistro<T>(int id, int columnIndex, object nuevoValor) where T : class, new()
{
try
{
T registro = SeleccionarRegistro<T>(id);
PropertyInfo propiedad = typeof(T).GetProperties()[columnIndex];
propiedad.SetValue(registro, Convert.ChangeType(nuevoValor, propiedad.PropertyType));
Update(registro);
SaveChanges();
return true;
}
catch (FormatException)
{
throw new BaseDeDatosException("Formato incorrecto");
}
catch (Exception)
{
throw new BaseDeDatosException("Error al actualizar el registro");
}
}
/// <summary>
/// Busca un registro por su Id en la base de datos, si lo encuentra lo retorna
/// </summary>
/// <typeparam name="T">El registro a buscar</typeparam>
/// <exception cref="BaseDeDatosException"></exception>
public T SeleccionarRegistro<T>(int id) where T : class, new()
{
try
{
return Find<T>(id);
}
catch(ArgumentNullException)
{
throw new BaseDeDatosException("El registro no existe en la base de datos");
}
catch (Exception)
{
throw new BaseDeDatosException("Error al encontrar el registro");
}
}
public List<T> SeleccionarTodos<T>() where T : class, new()
{
throw new NotImplementedException();
}
#endregion
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// Conexion a la Base de Datos
optionsBuilder.UseSqlServer(
"Server=.;" +
"Database=Hotel;" + // Cambiar por el nombre de la base de datos
"Trusted_Connection=True;" +
"TrustServerCertificate=Yes"
);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Huesped>()
.Property(e => e.FechaDeNacimiento)
.HasColumnType("date");
base.OnModelCreating(modelBuilder);
}
}
}