2024-06-11 20:12:24 +02:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of FirmTracker - Server.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* FirmTracker - Server is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with FirmTracker - Server. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using FirmTracker_Server.nHibernate.Expenses;
|
2024-06-16 23:02:04 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate.Products;
|
2024-05-16 18:02:44 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
namespace FirmTracker_Server.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class ExpensesController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly ExpenseCRUD _expenseCrud;
|
|
|
|
|
|
|
|
|
|
public ExpensesController()
|
|
|
|
|
{
|
|
|
|
|
_expenseCrud = new ExpenseCRUD();
|
|
|
|
|
}
|
|
|
|
|
// POST: api/Expenses
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(201)] // Created
|
|
|
|
|
[ProducesResponseType(400)] // Bad Request
|
2024-06-16 23:02:04 +02:00
|
|
|
|
public IActionResult CreateExpense([FromBody] Expense expense) {
|
|
|
|
|
try
|
2024-05-16 18:02:44 +02:00
|
|
|
|
{
|
2024-06-17 17:22:34 +02:00
|
|
|
|
if (expense.Value <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0.");
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 18:02:44 +02:00
|
|
|
|
_expenseCrud.AddExpense(expense);
|
|
|
|
|
return CreatedAtAction("GetExpense", new { id = expense.Id }, expense);
|
|
|
|
|
}
|
2024-06-17 17:22:34 +02:00
|
|
|
|
catch (InvalidOperationException ioe)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ioe.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-05-16 18:02:44 +02:00
|
|
|
|
return BadRequest(ex.Message);
|
2024-06-17 17:22:34 +02:00
|
|
|
|
}
|
2024-05-16 18:02:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Expenses
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(200)] // Created
|
2024-06-09 23:35:15 +02:00
|
|
|
|
[ProducesResponseType(404)] // Bad Request
|
2024-05-16 18:02:44 +02:00
|
|
|
|
public IActionResult GetExpense(int id)
|
|
|
|
|
{
|
|
|
|
|
var expense = _expenseCrud.GetExpense(id);
|
|
|
|
|
if (expense == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
|
|
|
|
return Ok(expense);
|
|
|
|
|
}
|
2024-05-16 21:25:07 +02:00
|
|
|
|
|
|
|
|
|
//PUT: api/Expenses
|
|
|
|
|
[HttpPut("{id}")]
|
2024-06-09 23:35:15 +02:00
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
[ProducesResponseType(400)]
|
2024-05-16 21:25:07 +02:00
|
|
|
|
public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-17 17:22:34 +02:00
|
|
|
|
if (id != expense.Id)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest("Nieprawidłowe ID wydatku");
|
|
|
|
|
}
|
|
|
|
|
if (expense.Value <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-05-16 21:25:07 +02:00
|
|
|
|
_expenseCrud.UpdateExpense(expense);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2024-06-17 17:22:34 +02:00
|
|
|
|
catch (InvalidOperationException ioe)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ioe.Message);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
2024-05-16 21:25:07 +02:00
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("{id}")]
|
2024-06-09 23:35:15 +02:00
|
|
|
|
[ProducesResponseType(204)]
|
|
|
|
|
[ProducesResponseType(404)]
|
2024-05-16 21:25:07 +02:00
|
|
|
|
public IActionResult DeleteExpense(int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_expenseCrud.DeleteExpense(id);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2024-06-17 22:17:59 +02:00
|
|
|
|
catch (InvalidOperationException ioe)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest($"{ioe.Message}");
|
|
|
|
|
}
|
2024-05-16 21:25:07 +02:00
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(200)]
|
|
|
|
|
[ProducesResponseType(400)]
|
|
|
|
|
public IActionResult GetAllExpenses()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var expenses = _expenseCrud.GetAllExpenses();
|
|
|
|
|
return Ok(expenses);
|
|
|
|
|
}
|
|
|
|
|
catch (System.Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-16 18:02:44 +02:00
|
|
|
|
}
|
2024-05-16 21:25:07 +02:00
|
|
|
|
|
2024-05-16 18:02:44 +02:00
|
|
|
|
}
|