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 GetByIdAsync(Guid ticketId) { try { var ticket = await _dbConnection.Tickets .FirstOrDefaultAsync(t => t.TicketId == ticketId); if (ticket == null) return new ETicket(); var eTicket = MapEntity(ticket); return eTicket; } catch (Exception ex) { throw new Exception($"{MethodBase.GetCurrentMethod()?.Name} Message: {ex.Message}", ex); } } public async Task> GetAllAsync() { var tickets = await _dbConnection.Tickets.ToListAsync(); return tickets.Select(ticket => MapEntity(ticket)); } public async Task InsertTicketAsync(ETicket ticket) { try { ticket.TicketId = Guid.NewGuid(); var dataTicket = MapEntity(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 GetSummary() //{ // var summary_Results = _dbConnection.Tickets_GetSummary(); // return summary_Results.Select(item => // { // var eSummary = Activator.CreateInstance(); // 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 GetTicketDashboard(string Estado, string Orden) //{ // var ticketDashboard_Results = _dbConnection.Tickets_Dashboard(Estado, Orden); // return (ticketDashboard_Results.Select(item => // { // var eTicketDashboard = Activator.CreateInstance(); // 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 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 GetAll() { throw new NotImplementedException(); } public void InsertTicket(ETicket ticket) { throw new NotImplementedException(); } public IEnumerable GetSummary() { throw new NotImplementedException(); } public IEnumerable GetTicketDashboard(string Estado, string Orden) { throw new NotImplementedException(); } #endregion } }