2024-05-05 22:08:27 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
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-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-05-16 00:42:06 +02:00
|
|
|
|
// Before adding the transaction, ensure each product is linked properly
|
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
|
|
|
|
product.TransactionId = transaction.Id; // This might be 0 at this point if transaction isn't saved yet
|
|
|
|
|
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
|
|
|
|
int type = _productCRUD.GetProductType(product.ProductID);
|
|
|
|
|
if (type == 1)
|
|
|
|
|
{
|
|
|
|
|
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
_transactionCRUD.AddTransaction(transaction);
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
|
|
|
|
// Now that the transaction is saved, update each product with the correct TransactionId
|
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
|
|
|
|
product.TransactionId = transaction.Id; // Now transaction.Id is a valid ID after saving
|
|
|
|
|
_transactionCRUD.UpdateTransactionProduct(product);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// session.Flush(); // Ensure changes are committed if managing session manually
|
|
|
|
|
|
2024-05-05 22:08:27 +02:00
|
|
|
|
return CreatedAtAction(nameof(GetTransaction), new { id = transaction.Id }, transaction);
|
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
return BadRequest("Transaction ID mismatch");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-05-16 00:42:06 +02:00
|
|
|
|
// Before adding the transaction, ensure each product is linked properly
|
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
|
|
|
|
product.TransactionId = transaction.Id; // This might be 0 at this point if transaction isn't saved yet
|
|
|
|
|
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
|
|
|
|
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
|
|
|
|
|
}
|
2024-05-05 22:08:27 +02:00
|
|
|
|
_transactionCRUD.UpdateTransaction(transaction);
|
2024-05-16 00:42:06 +02:00
|
|
|
|
|
|
|
|
|
// Now that the transaction is saved, update each product with the correct TransactionId
|
|
|
|
|
foreach (var product in transaction.TransactionProducts)
|
|
|
|
|
{
|
|
|
|
|
product.TransactionId = transaction.Id; // Now transaction.Id is a valid ID after saving
|
|
|
|
|
_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();
|
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// Ustawienie opcji serializatora JSON
|
|
|
|
|
var options = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
ReferenceHandler = ReferenceHandler.Preserve // Obsługa cykli obiektów
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// var json = JsonSerializer.Serialize(transactions, options);
|
|
|
|
|
|
|
|
|
|
// Zwrócenie odpowiedzi z JSON
|
2024-05-05 22:08:27 +02:00
|
|
|
|
return Ok(transactions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-06 21:25:10 +02:00
|
|
|
|
}
|