92 lines
2.5 KiB
C#
92 lines
2.5 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 RimborsiController : ControllerBase
|
|
{
|
|
private readonly ZentralDbContext _context;
|
|
|
|
public RimborsiController(ZentralDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<Rimborso>>> GetRimborsi([FromQuery] int? dipendenteId, [FromQuery] string? stato)
|
|
{
|
|
var query = _context.Rimborsi.Include(r => r.Dipendente).AsQueryable();
|
|
|
|
if (dipendenteId.HasValue)
|
|
query = query.Where(r => r.DipendenteId == dipendenteId.Value);
|
|
|
|
if (!string.IsNullOrEmpty(stato))
|
|
query = query.Where(r => r.Stato == stato);
|
|
|
|
return await query.OrderByDescending(r => r.DataSpesa).ToListAsync();
|
|
}
|
|
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<Rimborso>> GetRimborso(int id)
|
|
{
|
|
var rimborso = await _context.Rimborsi
|
|
.Include(r => r.Dipendente)
|
|
.FirstOrDefaultAsync(r => r.Id == id);
|
|
|
|
if (rimborso == null)
|
|
return NotFound();
|
|
|
|
return rimborso;
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<ActionResult<Rimborso>> CreateRimborso(Rimborso rimborso)
|
|
{
|
|
rimborso.CreatedAt = DateTime.UtcNow;
|
|
_context.Rimborsi.Add(rimborso);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction(nameof(GetRimborso), new { id = rimborso.Id }, rimborso);
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
public async Task<IActionResult> UpdateRimborso(int id, Rimborso rimborso)
|
|
{
|
|
if (id != rimborso.Id)
|
|
return BadRequest();
|
|
|
|
rimborso.UpdatedAt = DateTime.UtcNow;
|
|
_context.Entry(rimborso).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!await _context.Rimborsi.AnyAsync(r => r.Id == id))
|
|
return NotFound();
|
|
throw;
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
public async Task<IActionResult> DeleteRimborso(int id)
|
|
{
|
|
var rimborso = await _context.Rimborsi.FindAsync(id);
|
|
if (rimborso == null)
|
|
return NotFound();
|
|
|
|
_context.Rimborsi.Remove(rimborso);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return NoContent();
|
|
}
|
|
}
|