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 Microsoft.AspNetCore.Mvc;
|
2024-05-05 22:08:27 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate.Transactions;
|
2024-05-06 21:25:10 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Text.Json.Serialization;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Transactions;
|
2024-05-16 00:42:06 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate.Products;
|
2024-06-07 22:25:44 +02:00
|
|
|
|
using FirmTracker_Server.nHibernate;
|
2024-06-11 23:54:59 +02:00
|
|
|
|
using Microsoft.AspNetCore.Http.HttpResults;
|
2024-05-05 22:08:27 +02:00
|
|
|
|
|
|
|
|
|
namespace FirmTracker_Server.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class TransactionController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly TransactionCRUD _transactionCRUD;
|
2024-05-16 00:42:06 +02:00
|
|
|
|
private readonly ProductCRUD _productCRUD;
|
2024-05-05 22:08:27 +02:00
|
|
|
|
|
|
|
|
|
public TransactionController()
|
|
|
|
|
{
|
|
|
|
|
_transactionCRUD = new TransactionCRUD();
|
2024-05-16 00:42:06 +02:00
|
|
|
|
_productCRUD = new ProductCRUD();
|
2024-05-05 22:08:27 +02:00
|
|
|
|
}
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
|
|
|
|
|
// POST: api/Transaction
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new transaction.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2024-05-06 21:25:10 +02:00
|
|
|
|
public IActionResult CreateTransaction([FromBody] nHibernate.Transactions.Transaction transaction)
|
2024-05-05 22:08:27 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-11 20:12:24 +02:00
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
2024-06-13 19:10:25 +02:00
|
|
|
|
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
|
|
|
|
if (productByName == null)
|
|
|
|
|
{
|
2024-06-14 13:46:20 +02:00
|
|
|
|
throw new InvalidOperationException($"Produkt o nazwie {product.ProductName} nie istnieje.");
|
2024-06-13 19:10:25 +02:00
|
|
|
|
}
|
|
|
|
|
product.ProductID = productByName.Id;
|
|
|
|
|
product.TransactionId = transaction.Id;
|
|
|
|
|
|
|
|
|
|
decimal price = productByName.Price;// _productCRUD.GetProductPrice(product.ProductID);
|
|
|
|
|
int type = productByName.Type;//_productCRUD.GetProductType(product.ProductID);
|
2024-05-16 00:42:06 +02:00
|
|
|
|
if (type == 1)
|
|
|
|
|
{
|
2024-06-13 16:36:44 +02:00
|
|
|
|
var prod = _productCRUD.GetProduct(product.ProductID);
|
2024-06-07 22:25:44 +02:00
|
|
|
|
|
2024-06-13 16:36:44 +02:00
|
|
|
|
if (product.Quantity > prod.Availability)
|
2024-06-07 22:25:44 +02:00
|
|
|
|
{
|
2024-06-14 13:46:20 +02:00
|
|
|
|
throw new InvalidOperationException($"Nie można dodać {prod.Name} do transakcji. Dostępność: {prod.Availability}, Zażądano: {product.Quantity}");
|
2024-06-11 23:54:59 +02:00
|
|
|
|
//return BadRequest($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}");
|
2024-06-07 22:25:44 +02:00
|
|
|
|
}
|
2024-05-16 00:42:06 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
_transactionCRUD.AddTransaction(transaction);
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-06-11 20:12:24 +02:00
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
2024-06-11 20:12:24 +02:00
|
|
|
|
product.TransactionId = transaction.Id;
|
2024-05-16 00:42:06 +02:00
|
|
|
|
_transactionCRUD.UpdateTransactionProduct(product);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-11 20:12:24 +02:00
|
|
|
|
// session.Flush();
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
return CreatedAtAction(nameof(GetTransaction), new { id = transaction.Id }, transaction);
|
|
|
|
|
}
|
2024-06-11 23:54:59 +02:00
|
|
|
|
catch (InvalidOperationException ioe)
|
|
|
|
|
{
|
2024-06-13 16:36:44 +02:00
|
|
|
|
return BadRequest(ioe.Message);
|
2024-06-11 23:54:59 +02:00
|
|
|
|
}
|
2024-05-05 22:08:27 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
// GET: api/Transaction/5
|
|
|
|
|
[HttpGet("{id}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult GetTransaction(int id)
|
|
|
|
|
{
|
|
|
|
|
var transaction = _transactionCRUD.GetTransaction(id);
|
|
|
|
|
if (transaction == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
return Ok(transaction);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/Transaction/5
|
|
|
|
|
[HttpPut("{id}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2024-05-06 21:25:10 +02:00
|
|
|
|
public IActionResult UpdateTransaction(int id, [FromBody] nHibernate.Transactions.Transaction transaction)
|
2024-05-05 22:08:27 +02:00
|
|
|
|
{
|
|
|
|
|
if (id != transaction.Id)
|
2024-06-14 13:46:20 +02:00
|
|
|
|
return BadRequest("ID transakcji nie zgadza się.");
|
2024-05-05 22:08:27 +02:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-16 00:42:06 +02:00
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
2024-06-13 19:10:25 +02:00
|
|
|
|
var productByName = _productCRUD.GetProductByName(product.ProductName);
|
|
|
|
|
if (productByName == null)
|
|
|
|
|
{
|
2024-06-14 13:46:20 +02:00
|
|
|
|
throw new InvalidOperationException($"Produkt o nazwie {product.ProductName} nie istnieje.");
|
2024-06-13 19:10:25 +02:00
|
|
|
|
}
|
|
|
|
|
product.ProductID = productByName.Id;
|
|
|
|
|
product.TransactionId = transaction.Id;
|
|
|
|
|
|
|
|
|
|
decimal price = productByName.Price;// _productCRUD.GetProductPrice(product.ProductID);
|
|
|
|
|
int type = productByName.Type;//_productCRUD.GetProductType(product.ProductID);
|
2024-06-12 14:03:49 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-06-13 16:36:44 +02:00
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
_transactionCRUD.UpdateTransaction(transaction);
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
2024-06-11 20:12:24 +02:00
|
|
|
|
|
2024-05-16 00:42:06 +02:00
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
2024-06-11 20:12:24 +02:00
|
|
|
|
product.TransactionId = transaction.Id;
|
2024-05-16 00:42:06 +02:00
|
|
|
|
_transactionCRUD.UpdateTransactionProduct(product);
|
|
|
|
|
}
|
2024-05-05 22:08:27 +02:00
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/Transaction/5
|
|
|
|
|
[HttpDelete("{id}")]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
|
|
|
public IActionResult DeleteTransaction(int id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_transactionCRUD.DeleteTransaction(id);
|
|
|
|
|
return NoContent();
|
|
|
|
|
}
|
2024-06-17 22:17:59 +02:00
|
|
|
|
catch (InvalidOperationException ioe)
|
|
|
|
|
{
|
|
|
|
|
return BadRequest($"{ioe.Message}");
|
|
|
|
|
}
|
2024-05-05 22:08:27 +02:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return NotFound(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/Transaction
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
2024-05-06 21:25:10 +02:00
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2024-05-05 22:08:27 +02:00
|
|
|
|
public IActionResult GetAllTransactions()
|
|
|
|
|
{
|
|
|
|
|
var transactions = _transactionCRUD.GetAllTransactions();
|
2024-05-06 21:25:10 +02:00
|
|
|
|
if (transactions == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2024-06-11 20:12:24 +02:00
|
|
|
|
|
2024-05-06 21:25:10 +02:00
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
|
{
|
2024-06-11 20:12:24 +02:00
|
|
|
|
ReferenceHandler = ReferenceHandler.Preserve
|
2024-05-06 21:25:10 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// var json = JsonSerializer.Serialize(transactions, options);
|
|
|
|
|
|
2024-06-11 20:12:24 +02:00
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
return Ok(transactions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-06 21:25:10 +02:00
|
|
|
|
}
|