From cd6b90696b411c6023306d08c8f35eed585ca972 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Sun, 16 Jun 2024 23:02:04 +0200 Subject: [PATCH 1/3] =?UTF-8?q?komunikaty=20b=C5=82=C4=99d=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ExpenseController.cs | 15 ++++++++-- Controllers/ProductController.cs | 28 +++++++++++++++---- TestClass.cs | 2 +- .../Transactions/Transaction2Mapping.cs | 2 +- nHibernate/Transactions/TransactionMapping.cs | 2 +- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/Controllers/ExpenseController.cs b/Controllers/ExpenseController.cs index 77478be..2480c83 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,8 +34,12 @@ 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) { + if (expense.Value<=0) + { + throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0."); + } + try { _expenseCrud.AddExpense(expense); return CreatedAtAction("GetExpense", new { id = expense.Id }, expense); @@ -66,7 +71,11 @@ namespace FirmTracker_Server.Controllers { if (id != expense.Id) { - return BadRequest("Expense ID mismatch"); + return BadRequest("Nieprawidłowe ID wydatku"); + } + if (expense.Value <= 0) + { + throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0."); } try { diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 1e0814e..6dd35cb 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]")] @@ -41,11 +43,18 @@ namespace FirmTracker_Server.Controllers { if (product.Type != 0 && product.Type != 1) { - return BadRequest("Kategoria produktu musi być ustawiona na 0 lub 1."); + throw new InvalidOperationException("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."); + 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."); } try { @@ -88,16 +97,23 @@ namespace FirmTracker_Server.Controllers public IActionResult UpdateProduct(int id, [FromBody] Product product) { if (id != product.Id) - return BadRequest("ID produktu nie zgadza się."); + throw new InvalidOperationException("ID produktu nie zgadza się."); if (product.Type != 0 && product.Type != 1) { - return BadRequest("Kategoria produktu musi być ustawiona na 0 lub 1."); + throw new InvalidOperationException("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."); + 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."); } - try { _productCrud.UpdateProduct(product); 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/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/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(); } } -- 2.20.1 From eec83a95fc4e3dd9a12d9709cc4340092191f91d Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Mon, 17 Jun 2024 17:22:34 +0200 Subject: [PATCH 2/3] =?UTF-8?q?poprawa=20komunikat=C3=B3w=20po=20testach?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/ExpenseController.cs | 42 ++++++++++------ Controllers/ProductController.cs | 82 ++++++++++++++++++-------------- 2 files changed, 74 insertions(+), 50 deletions(-) diff --git a/Controllers/ExpenseController.cs b/Controllers/ExpenseController.cs index 2480c83..d45493d 100644 --- a/Controllers/ExpenseController.cs +++ b/Controllers/ExpenseController.cs @@ -35,18 +35,24 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(201)] // Created [ProducesResponseType(400)] // Bad Request public IActionResult CreateExpense([FromBody] Expense expense) { - if (expense.Value<=0) - { - throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0."); - } 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 @@ -69,20 +75,26 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] public IActionResult UpdateExpense(int id, [FromBody] Expense expense) { - 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."); - } 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); } diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 6dd35cb..d7711b0 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -41,27 +41,33 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] // Bad Request public IActionResult CreateProduct([FromBody] Product product) { - 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."); - } 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); } @@ -96,30 +102,36 @@ namespace FirmTracker_Server.Controllers [ProducesResponseType(400)] // Bad Request public IActionResult UpdateProduct(int id, [FromBody] Product product) { - 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."); - } 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); } -- 2.20.1 From f3cb57ba6ff043de07ebf1d2be34afa73e28b5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Ry=C5=BCek?= Date: Mon, 17 Jun 2024 22:17:59 +0200 Subject: [PATCH 3/3] poprawki delete --- Controllers/ExpenseController.cs | 4 ++++ Controllers/ProductController.cs | 4 ++++ Controllers/ReportController.cs | 8 ++++---- Controllers/TransactionController.cs | 4 ++++ nHIbernate/Expenses/ExpenseCRUD.cs | 12 ++++++++++++ nHIbernate/Products/ProductCRUD.cs | 13 +++++++++++++ nHibernate/Transactions/TransactionCRUD.cs | 11 +++++++++++ 7 files changed, 52 insertions(+), 4 deletions(-) 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); -- 2.20.1