0% found this document useful (0 votes)
9 views2 pages

Message

The CierreCajaService class provides methods for managing 'CierreCaja' entities, including saving, modifying, deleting, and searching for records. It checks for the existence of a record before deciding to insert or modify it, and it supports listing records based on a specified criterion. The service interacts with a database context created through a factory pattern for asynchronous operations.

Uploaded by

almaylili1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Message

The CierreCajaService class provides methods for managing 'CierreCaja' entities, including saving, modifying, deleting, and searching for records. It checks for the existence of a record before deciding to insert or modify it, and it supports listing records based on a specified criterion. The service interacts with a database context created through a factory pattern for asynchronous operations.

Uploaded by

almaylili1234
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class CierreCajaService(IDbContextFactory<ApplicationDbContext> DbFactory)

{
public async Task<bool> Guardar(CierreCaja cierreCaja)
{
await using var _contexto = await DbFactory.CreateDbContextAsync();

if (!await Existe(cierreCaja.OrdenVentaId))
{
return await Insertar(cierreCaja);
}
else
{
return await Modificar(cierreCaja);
}
}
private async Task<bool> Existe(int CierreCajaId)
{
await using var _contexto = await DbFactory.CreateDbContextAsync();

return await _contexto.CierreCaja


.AnyAsync(c => c.CierreId == CierreCajaId);
}
private async Task<bool> Insertar(CierreCaja cierreCaja)
{
await using var _contexto = await DbFactory.CreateDbContextAsync();

_contexto.CierreCaja.Add(cierreCaja);
return await _contexto.SaveChangesAsync() > 0;
}

private async Task<bool> Modificar(CierreCaja cierreCaja)


{
await using var _contexto = await DbFactory.CreateDbContextAsync();

_contexto.Update(cierreCaja);
return await _contexto.SaveChangesAsync() > 0;

public async Task<bool> Eliminar(CierreCaja cierreCaja)


{
await using var _contexto = await DbFactory.CreateDbContextAsync();

return await _contexto.CierreCaja


.AsNoTracking()
.Where(c => c.CierreId == cierreCaja.CierreId)
.ExecuteDeleteAsync() > 0;
}

public async Task<CierreCaja?> Buscar(int cierreCajaId)


{
await using var _contexto = await DbFactory.CreateDbContextAsync();

return await _contexto.CierreCaja


.AsNoTracking()
.FirstOrDefaultAsync(c => c.CierreId == cierreCajaId);
}
public async Task<List<CierreCaja>> Listar(Expression<Func<CierreCaja, bool>>
criterio)
{
await using var _contexto = await DbFactory.CreateDbContextAsync();

return await _contexto.CierreCaja


.Include(c => c.OrdenVenta)
.AsNoTracking()
.Where(criterio)
.ToListAsync();
}
}

You might also like