diff --git a/Controllers/ExpenseController.cs b/Controllers/ExpenseController.cs index d45493d..f182ea3 100644 --- a/Controllers/ExpenseController.cs +++ b/Controllers/ExpenseController.cs @@ -110,6 +110,10 @@ namespace FirmTracker_Server.Controllers _expenseCrud.DeleteExpense(id); return NoContent(); } + catch (InvalidOperationException ioe) + { + return BadRequest($"{ioe.Message}"); + } catch (System.Exception ex) { return NotFound(ex.Message); diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index d7711b0..d662d96 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -148,6 +148,10 @@ namespace FirmTracker_Server.Controllers _productCrud.DeleteProduct(id); return NoContent(); } + catch (InvalidOperationException ioe) + { + return BadRequest($"{ioe.Message}"); + } catch (System.Exception ex) { return NotFound(ex.Message); diff --git a/Controllers/ReportController.cs b/Controllers/ReportController.cs index fe917bb..dabcd8e 100644 --- a/Controllers/ReportController.cs +++ b/Controllers/ReportController.cs @@ -64,7 +64,7 @@ namespace FirmTracker_Server.Controllers .Where(e => e.Date >= fromDate && e.Date <= toDate) .ToList(); - + decimal totalIncome = 0; foreach (var transaction in transactions) { @@ -126,7 +126,7 @@ namespace FirmTracker_Server.Controllers var options = new JsonSerializerOptions { - ReferenceHandler = ReferenceHandler.Preserve + ReferenceHandler = ReferenceHandler.Preserve }; var json = JsonSerializer.Serialize(report, options); @@ -202,7 +202,7 @@ namespace FirmTracker_Server.Controllers .Where(e => e.Date >= fromDate && e.Date <= toDate) .ToList(); - + decimal totalIncome = 0; foreach (var transaction in transactions) { @@ -241,7 +241,7 @@ namespace FirmTracker_Server.Controllers } } - [HttpDelete] + [HttpDelete("{id}")] [ProducesResponseType(204)] [ProducesResponseType(404)] public IActionResult DeleteReport(int id) diff --git a/Controllers/TransactionController.cs b/Controllers/TransactionController.cs index 3f82653..20b556e 100644 --- a/Controllers/TransactionController.cs +++ b/Controllers/TransactionController.cs @@ -167,6 +167,10 @@ namespace FirmTracker_Server.Controllers _transactionCRUD.DeleteTransaction(id); return NoContent(); } + catch (InvalidOperationException ioe) + { + return BadRequest($"{ioe.Message}"); + } catch (Exception ex) { return NotFound(ex.Message); diff --git a/nHIbernate/Expenses/ExpenseCRUD.cs b/nHIbernate/Expenses/ExpenseCRUD.cs index c19c5e6..27096fa 100644 --- a/nHIbernate/Expenses/ExpenseCRUD.cs +++ b/nHIbernate/Expenses/ExpenseCRUD.cs @@ -17,6 +17,8 @@ using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate.Products; +using FirmTracker_Server.nHibernate.Reports; +using NHibernate.Criterion; namespace FirmTracker_Server.nHibernate.Expenses { @@ -102,6 +104,16 @@ namespace FirmTracker_Server.nHibernate.Expenses var expense = session.Get(expenseId); if (expense != null) { + var criteria = session.CreateCriteria(); + criteria.Add(Restrictions.Eq("Expense.Id", expenseId)); + var reportExpenses = criteria.List(); + + 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); transaction.Commit(); } diff --git a/nHIbernate/Products/ProductCRUD.cs b/nHIbernate/Products/ProductCRUD.cs index bb2bdc3..ac06989 100644 --- a/nHIbernate/Products/ProductCRUD.cs +++ b/nHIbernate/Products/ProductCRUD.cs @@ -22,6 +22,9 @@ using System.Collections.Generic; using Microsoft.AspNetCore.OpenApi; using Microsoft.AspNetCore.Http.HttpResults; using System.Security.Cryptography.X509Certificates; +using FirmTracker_Server.nHibernate.Reports; +using NHibernate.Criterion; +using FirmTracker_Server.nHibernate.Transactions; namespace FirmTracker_Server.nHibernate.Products { @@ -129,6 +132,16 @@ namespace FirmTracker_Server.nHibernate.Products var product = session.Get(productId); if (product != null) { + + var criteria = session.CreateCriteria(); + criteria.Add(Restrictions.Eq("ProductID", productId)); + var transactionProducts = criteria.List(); + + if (transactionProducts.Any()) + { + throw new InvalidOperationException("Nie można usunąć produktu. Produkt jest ujęty w co najmniej jednej transakcji."); + } + session.Delete(product); transaction.Commit(); } diff --git a/nHibernate/Transactions/TransactionCRUD.cs b/nHibernate/Transactions/TransactionCRUD.cs index 57a0221..c16d574 100644 --- a/nHibernate/Transactions/TransactionCRUD.cs +++ b/nHibernate/Transactions/TransactionCRUD.cs @@ -1,5 +1,7 @@ using FirmTracker_Server.nHibernate.Products; +using FirmTracker_Server.nHibernate.Reports; using NHibernate; +using NHibernate.Criterion; using NHibernate.Linq; using System; using System.Collections.Generic; @@ -177,6 +179,15 @@ namespace FirmTracker_Server.nHibernate.Transactions var transaction = session.Get(transactionId); if (transaction != null) { + var criteria = session.CreateCriteria(); + criteria.Add(Restrictions.Eq("Transaction.Id", transactionId)); + var reportTransactions = criteria.List(); + + 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) { var product = session.Get(transactionProduct.ProductID);