phronCare/Models/Repositories/PhSQuoteHeaderRepository.cs

74 lines
2.9 KiB
C#
Raw Normal View History

using Microsoft.EntityFrameworkCore;
2025-04-27 02:19:29 -03:00
using Models.Interfaces;
using Models.Helpers;
2025-04-27 02:19:29 -03:00
using Models.Models;
using Domain.Entities;
using Domain.Generics;
2025-04-27 02:19:29 -03:00
2025-05-08 15:46:04 -03:00
namespace Models.Repositories
2025-04-27 02:19:29 -03:00
{
public class PhSQuoteHeaderRepository(PhronCareOperationsHubContext context) : IPhSQuoteHeaderRepository
2025-04-27 02:19:29 -03:00
{
private readonly PhronCareOperationsHubContext _context = context;
//private readonly IPhSFormSeriesRepository _formSeriesRepository = formSeriesRepository;
#region Metodos
2025-04-27 02:19:29 -03:00
public async Task<PagedResult<EQuoteHeader>> GetAllAsync(int page = 1, int pageSize = 50)
{
var query = _context.PhSQuoteHeaders
.Include(q => q.PhSQuoteDetails)
.Include(q => q.PhSQuoteRoles)
.Include(q => q.PhSQuoteAdjustments)
.Include(q => q.PhSQuoteTaxes)
.AsNoTracking();
2025-04-27 02:19:29 -03:00
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
return new PagedResult<EQuoteHeader>
{
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>),
TotalItems = pagedEntities.TotalItems,
Page = pagedEntities.Page,
PageSize = pagedEntities.PageSize
};
}
public async Task<EQuoteHeader?> GetByIdAsync(int id)
{
var entity = await _context.PhSQuoteHeaders
.Include(q => q.PhSQuoteDetails)
.Include(q => q.PhSQuoteRoles)
.Include(q => q.PhSQuoteAdjustments)
.Include(q => q.PhSQuoteTaxes)
2025-04-27 02:19:29 -03:00
.FirstOrDefaultAsync(q => q.Id == id);
return entity != null ? EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>(entity) : null;
}
public async Task<IEnumerable<EQuoteHeader>> GetByCustomerIdAsync(int customerId)
{
var entities = await _context.PhSQuoteHeaders
.Where(q => q.CustomerId == customerId)
.Include(q => q.PhSQuoteDetails)
.Include(q => q.PhSQuoteRoles)
.Include(q => q.PhSQuoteAdjustments)
.Include(q => q.PhSQuoteTaxes)
2025-04-27 02:19:29 -03:00
.ToListAsync();
return entities.Select(EntityMapper.MapEntity<PhSQuoteHeader, EQuoteHeader>);
}
public async Task UpdateAsync(EQuoteHeader quoteHeader)
{
var dbEntity = EntityMapper.MapEntity<EQuoteHeader, PhSQuoteHeader>(quoteHeader);
_context.PhSQuoteHeaders.Update(dbEntity);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await _context.PhSQuoteHeaders.FindAsync(id);
if (entity != null)
{
_context.PhSQuoteHeaders.Remove(entity);
await _context.SaveChangesAsync();
}
}
#endregion
2025-04-27 02:19:29 -03:00
}
}