phronCare/Data/Repositories/TicketRepository.cs

132 lines
4.6 KiB
C#

using Data.Interfaces;
using Domain.Entities;
using Data.Entities;
using Data.Models;
using Microsoft.EntityFrameworkCore;
using System.Reflection;
namespace Data.Repositories
{
public class TicketRepository : ITicketRepository
{
#region Declaraciones y Constructor
private readonly OperationsHubContext _dbConnection;
public TicketRepository(OperationsHubContext dbConnection)
{
_dbConnection = dbConnection;
}
#endregion
#region Metodos de clase
public async Task<ETicket> GetByIdAsync(Guid ticketId)
{
try
{
var ticket = await _dbConnection.Tickets
.FirstOrDefaultAsync(t => t.TicketId == ticketId);
if (ticket == null) return new ETicket();
var eTicket = MapEntity<PhOH_Tickets, ETicket>(ticket);
return eTicket;
}
catch (Exception ex)
{
throw new Exception($"{MethodBase.GetCurrentMethod()?.Name} Message: {ex.Message}", ex);
}
}
public async Task<IEnumerable<ETicket>> GetAllAsync()
{
var tickets = await _dbConnection.Tickets.ToListAsync();
return tickets.Select(ticket => MapEntity<PhOH_Tickets, ETicket>(ticket));
}
public async Task InsertTicketAsync(ETicket ticket)
{
try
{
ticket.TicketId = Guid.NewGuid();
var dataTicket = MapEntity<ETicket, PhOH_Tickets>(ticket);
await _dbConnection.Tickets.AddAsync(dataTicket);
await _dbConnection.SaveChangesAsync();
}
catch (DbUpdateException ex)
{
throw new Exception($"{MethodBase.GetCurrentMethod()?.Name} Message: {ex.InnerException?.Message}", ex);
}
catch (Exception ex)
{
throw new Exception($"{MethodBase.GetCurrentMethod()?.Name} Message: {ex.Message}", ex);
}
}
//public IEnumerable<ETickets_GetSummary> GetSummary()
//{
// var summary_Results = _dbConnection.Tickets_GetSummary();
// return summary_Results.Select(item =>
// {
// var eSummary = Activator.CreateInstance<ETickets_GetSummary>();
// foreach (var propertyInfo in typeof(Tickets_GetSummary_Result).GetProperties())
// {
// var value = propertyInfo.GetValue(item);
// typeof(ETickets_GetSummary).GetProperty(propertyInfo.Name)?.SetValue(eSummary, value);
// }
// return eSummary;
// });
//}
//public IEnumerable<ETicket_Dashboard> GetTicketDashboard(string Estado, string Orden)
//{
// var ticketDashboard_Results = _dbConnection.Tickets_Dashboard(Estado, Orden);
// return (ticketDashboard_Results.Select(item =>
// {
// var eTicketDashboard = Activator.CreateInstance<ETicket_Dashboard>();
// foreach (var propertyInfo in typeof(Tickets_Dashboard_Result).GetProperties())
// {
// var value = propertyInfo.GetValue(item);
// typeof(ETicket_Dashboard).GetProperty(propertyInfo.Name)?.SetValue(eTicketDashboard, value);
// }
// return eTicketDashboard;
// }));
//}
#endregion
#region Métodos Auxiliares
private TDestination MapEntity<TSource, TDestination>(TSource source) where TDestination : new()
{
var destination = new TDestination();
foreach (var propertyInfo in typeof(TSource).GetProperties())
{
var value = propertyInfo.GetValue(source);
typeof(TDestination).GetProperty(propertyInfo.Name)?.SetValue(destination, value);
}
return destination;
}
public ETicket GetById(Guid ticketId)
{
throw new NotImplementedException();
}
public IEnumerable<ETicket> GetAll()
{
throw new NotImplementedException();
}
public void InsertTicket(ETicket ticket)
{
throw new NotImplementedException();
}
public IEnumerable<ETickets_GetSummary> GetSummary()
{
throw new NotImplementedException();
}
public IEnumerable<ETicket_Dashboard> GetTicketDashboard(string Estado, string Orden)
{
throw new NotImplementedException();
}
#endregion
}
}