diff --git a/Controllers/ExpenseController.cs b/Controllers/ExpenseController.cs index 77478be..f182ea3 100644 --- a/Controllers/ExpenseController.cs +++ b/Controllers/ExpenseController.cs @@ -16,6 +16,7 @@ */ using FirmTracker_Server.nHibernate.Expenses; +using FirmTracker_Server.nHibernate.Products; using Microsoft.AspNetCore.Mvc; namespace FirmTracker_Server.Controllers { @@ -33,15 +34,25 @@ namespace FirmTracker_Server.Controllers [HttpPost] [ProducesResponseType(201)] // Created [ProducesResponseType(400)] // Bad Request - public IActionResult CreateExpense([FromBody] Expense expense) { - try + public IActionResult CreateExpense([FromBody] Expense expense) { + try { + if (expense.Value <= 0) + { + throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0."); + } + _expenseCrud.AddExpense(expense); return CreatedAtAction("GetExpense", new { id = expense.Id }, expense); } - catch (System.Exception ex) { + catch (InvalidOperationException ioe) + { + return BadRequest(ioe.Message); + } + catch (Exception ex) + { return BadRequest(ex.Message); - } + } } // GET: api/Expenses @@ -64,16 +75,26 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] public IActionResult UpdateExpense(int id, [FromBody] Expense expense) { - if (id != expense.Id) - { - return BadRequest("Expense ID mismatch"); - } try { + 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."); + } + + _expenseCrud.UpdateExpense(expense); return NoContent(); } - catch (System.Exception ex) + catch (InvalidOperationException ioe) + { + return BadRequest(ioe.Message); + } + catch (Exception ex) { return BadRequest(ex.Message); } @@ -89,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 1e0814e..d662d96 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -17,6 +17,8 @@ using FirmTracker_Server.nHibernate.Products; using Microsoft.AspNetCore.Mvc; +using System; + namespace FirmTracker_Server.Controllers { [Route("api/[controller]")] @@ -39,20 +41,33 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] // Bad Request public IActionResult CreateProduct([FromBody] Product product) { - if (product.Type != 0 && product.Type != 1) - { - return BadRequest("Kategoria produktu musi być ustawiona na 0 lub 1."); - } - if (product.Type == 0 && product.Availability != 0) - { - return BadRequest("Dostępność usługi musi być ustawiona na 0."); - } try { + if (product.Type != 0 && product.Type != 1) + { + throw new InvalidOperationException("Kategoria produktu musi być ustawiona na 0 lub 1."); + } + if (product.Type == 0 && product.Availability != 0) + { + throw new InvalidOperationException("Dostępność usługi musi być ustawiona na 0."); + } + if (product.Type == 1 && product.Availability < 0) + { + throw new InvalidOperationException("Dostępność towaru nie może być ujemna."); + } + if (product.Price < 0) + { + throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny."); + } + _productCrud.AddProduct(product); return CreatedAtAction("GetProduct", new { id = product.Id }, product); } - catch (System.Exception ex) + catch (InvalidOperationException ioe) + { + return BadRequest(ioe.Message); + } + catch (Exception ex) { return BadRequest(ex.Message); } @@ -87,23 +102,36 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] // Bad Request public IActionResult UpdateProduct(int id, [FromBody] Product product) { - if (id != product.Id) - return BadRequest("ID produktu nie zgadza się."); - if (product.Type != 0 && product.Type != 1) - { - return BadRequest("Kategoria produktu musi być ustawiona na 0 lub 1."); - } - if (product.Type == 0 && product.Availability != 0) - { - return BadRequest("Dostępność usługi musi być ustawiona na 0."); - } - try { + if (id != product.Id) + throw new InvalidOperationException("ID produktu nie zgadza się."); + if (product.Type != 0 && product.Type != 1) + { + throw new InvalidOperationException("Kategoria produktu musi być ustawiona na 0 lub 1."); + } + if (product.Type == 0 && product.Availability != 0) + { + throw new InvalidOperationException("Dostępność usługi musi być ustawiona na 0."); + } + if (product.Type == 1 && product.Availability < 0) + { + throw new InvalidOperationException("Dostępność towaru nie może być ujemna."); + } + if (product.Price < 0) + { + throw new InvalidOperationException("Produkt nie może posiadać ujemnej ceny."); + } + + _productCrud.UpdateProduct(product); return NoContent(); } - catch (System.Exception ex) + catch (InvalidOperationException ioe) + { + return BadRequest(ioe.Message); + } + catch (Exception ex) { return BadRequest(ex.Message); } @@ -120,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/TestClass.cs b/TestClass.cs index eb3e30d..5e28ecf 100644 --- a/TestClass.cs +++ b/TestClass.cs @@ -54,7 +54,7 @@ namespace FirmTracker_Server var products = new List { CreateProduct("Tarta_truskawka", "produkt", 31.99m, 1, 10), - CreateProduct("Tarta_czekolada", "produkt", 30.99m, 1, 8), + CreateProduct("Tarta_czekolada", "produkt", 30.99m, 1, 10), CreateProduct("Tarta_agrest", "produkt", 32.90m, 1, 8), CreateProduct("Tarta_pistacja", "produkt", 35.99m, 1, 12), CreateProduct("Tarta_karmel", "produkt", 32.00m, 1, 12), 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/Transaction2Mapping.cs b/nHibernate/Transactions/Transaction2Mapping.cs index 6fff813..53ac5b8 100644 --- a/nHibernate/Transactions/Transaction2Mapping.cs +++ b/nHibernate/Transactions/Transaction2Mapping.cs @@ -18,7 +18,7 @@ namespace FirmTracker_Server.nHibernate.Transactions HasMany(x => x.TransactionProducts) .KeyColumn("TransactionId") .Cascade.AllDeleteOrphan() - .Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji + .Inverse(); } } 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); diff --git a/nHibernate/Transactions/TransactionMapping.cs b/nHibernate/Transactions/TransactionMapping.cs index eb39303..552fe76 100644 --- a/nHibernate/Transactions/TransactionMapping.cs +++ b/nHibernate/Transactions/TransactionMapping.cs @@ -18,7 +18,7 @@ namespace FirmTracker_Server.nHibernate.Transactions HasMany(x => x.TransactionProducts) .KeyColumn("TransactionId") .Cascade.AllDeleteOrphan() - .Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji + .Inverse(); } }