poprawki delete

This commit is contained in:
Kamil Ryżek 2024-06-17 22:17:59 +02:00
parent eec83a95fc
commit f3cb57ba6f
7 changed files with 52 additions and 4 deletions

View File

@ -110,6 +110,10 @@ namespace FirmTracker_Server.Controllers
_expenseCrud.DeleteExpense(id); _expenseCrud.DeleteExpense(id);
return NoContent(); return NoContent();
} }
catch (InvalidOperationException ioe)
{
return BadRequest($"{ioe.Message}");
}
catch (System.Exception ex) catch (System.Exception ex)
{ {
return NotFound(ex.Message); return NotFound(ex.Message);

View File

@ -148,6 +148,10 @@ namespace FirmTracker_Server.Controllers
_productCrud.DeleteProduct(id); _productCrud.DeleteProduct(id);
return NoContent(); return NoContent();
} }
catch (InvalidOperationException ioe)
{
return BadRequest($"{ioe.Message}");
}
catch (System.Exception ex) catch (System.Exception ex)
{ {
return NotFound(ex.Message); return NotFound(ex.Message);

View File

@ -241,7 +241,7 @@ namespace FirmTracker_Server.Controllers
} }
} }
[HttpDelete] [HttpDelete("{id}")]
[ProducesResponseType(204)] [ProducesResponseType(204)]
[ProducesResponseType(404)] [ProducesResponseType(404)]
public IActionResult DeleteReport(int id) public IActionResult DeleteReport(int id)

View File

@ -167,6 +167,10 @@ namespace FirmTracker_Server.Controllers
_transactionCRUD.DeleteTransaction(id); _transactionCRUD.DeleteTransaction(id);
return NoContent(); return NoContent();
} }
catch (InvalidOperationException ioe)
{
return BadRequest($"{ioe.Message}");
}
catch (Exception ex) catch (Exception ex)
{ {
return NotFound(ex.Message); return NotFound(ex.Message);

View File

@ -17,6 +17,8 @@
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate.Reports;
using NHibernate.Criterion;
namespace FirmTracker_Server.nHibernate.Expenses namespace FirmTracker_Server.nHibernate.Expenses
{ {
@ -102,6 +104,16 @@ namespace FirmTracker_Server.nHibernate.Expenses
var expense = session.Get<Expense>(expenseId); var expense = session.Get<Expense>(expenseId);
if (expense != null) if (expense != null)
{ {
var criteria = session.CreateCriteria<ReportExpense>();
criteria.Add(Restrictions.Eq("Expense.Id", expenseId));
var reportExpenses = criteria.List<ReportExpense>();
if (reportExpenses.Any())
{
throw new InvalidOperationException("Nie można usunąć wydatku. Wydatek jest ujęty w co najmniej jednym z raportów.");
}
session.Delete(expense); session.Delete(expense);
transaction.Commit(); transaction.Commit();
} }

View File

@ -22,6 +22,9 @@ using System.Collections.Generic;
using Microsoft.AspNetCore.OpenApi; using Microsoft.AspNetCore.OpenApi;
using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Http.HttpResults;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using FirmTracker_Server.nHibernate.Reports;
using NHibernate.Criterion;
using FirmTracker_Server.nHibernate.Transactions;
namespace FirmTracker_Server.nHibernate.Products namespace FirmTracker_Server.nHibernate.Products
{ {
@ -129,6 +132,16 @@ namespace FirmTracker_Server.nHibernate.Products
var product = session.Get<Product>(productId); var product = session.Get<Product>(productId);
if (product != null) if (product != null)
{ {
var criteria = session.CreateCriteria<TransactionProduct>();
criteria.Add(Restrictions.Eq("ProductID", productId));
var transactionProducts = criteria.List<TransactionProduct>();
if (transactionProducts.Any())
{
throw new InvalidOperationException("Nie można usunąć produktu. Produkt jest ujęty w co najmniej jednej transakcji.");
}
session.Delete(product); session.Delete(product);
transaction.Commit(); transaction.Commit();
} }

View File

@ -1,5 +1,7 @@
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate.Reports;
using NHibernate; using NHibernate;
using NHibernate.Criterion;
using NHibernate.Linq; using NHibernate.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -177,6 +179,15 @@ namespace FirmTracker_Server.nHibernate.Transactions
var transaction = session.Get<Transaction>(transactionId); var transaction = session.Get<Transaction>(transactionId);
if (transaction != null) if (transaction != null)
{ {
var criteria = session.CreateCriteria<ReportTransaction>();
criteria.Add(Restrictions.Eq("Transaction.Id", transactionId));
var reportTransactions = criteria.List<ReportTransaction>();
if (reportTransactions.Any())
{
throw new InvalidOperationException("Nie można usunąć transakcji. Transakcja jest ujęta w co najmniej jednym z raportów.");
}
foreach (var transactionProduct in transaction.TransactionProducts) foreach (var transactionProduct in transaction.TransactionProducts)
{ {
var product = session.Get<Product>(transactionProduct.ProductID); var product = session.Get<Product>(transactionProduct.ProductID);