95 lines
2.6 KiB
C#
95 lines
2.6 KiB
C#
using Zentral.Domain.Entities.HR;
|
|
using Zentral.Infrastructure.Data;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Zentral.API.Apps.HR.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/hr/[controller]")]
|
|
public class PagamentiController : ControllerBase
|
|
{
|
|
private readonly ZentralDbContext _context;
|
|
|
|
public PagamentiController(ZentralDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Pagamento>>> GetPagamenti([FromQuery] int? dipendenteId, [FromQuery] DateTime? from, [FromQuery] DateTime? to)
|
|
{
|
|
var query = _context.Pagamenti.Include(p => p.Dipendente).AsQueryable();
|
|
|
|
if (dipendenteId.HasValue)
|
|
query = query.Where(p => p.DipendenteId == dipendenteId.Value);
|
|
|
|
if (from.HasValue)
|
|
query = query.Where(p => p.DataPagamento >= from.Value);
|
|
|
|
if (to.HasValue)
|
|
query = query.Where(p => p.DataPagamento <= to.Value);
|
|
|
|
return await query.OrderByDescending(p => p.DataPagamento).ToListAsync();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Pagamento>> GetPagamento(int id)
|
|
{
|
|
var pagamento = await _context.Pagamenti
|
|
.Include(p => p.Dipendente)
|
|
.FirstOrDefaultAsync(p => p.Id == id);
|
|
|
|
if (pagamento == null)
|
|
return NotFound();
|
|
|
|
return pagamento;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<Pagamento>> CreatePagamento(Pagamento pagamento)
|
|
{
|
|
pagamento.CreatedAt = DateTime.UtcNow;
|
|
_context.Pagamenti.Add(pagamento);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction(nameof(GetPagamento), new { id = pagamento.Id }, pagamento);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> UpdatePagamento(int id, Pagamento pagamento)
|
|
{
|
|
if (id != pagamento.Id)
|
|
return BadRequest();
|
|
|
|
pagamento.UpdatedAt = DateTime.UtcNow;
|
|
_context.Entry(pagamento).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!await _context.Pagamenti.AnyAsync(p => p.Id == id))
|
|
return NotFound();
|
|
throw;
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeletePagamento(int id)
|
|
{
|
|
var pagamento = await _context.Pagamenti.FindAsync(id);
|
|
if (pagamento == null)
|
|
return NotFound();
|
|
|
|
_context.Pagamenti.Remove(pagamento);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
}
|