Merge pull request 'PI2024-23' (#4) from PI2024-23 into master

Reviewed-on: #4
This commit is contained in:
s464958 2024-10-21 17:28:50 +02:00
commit 7cbe1129ef
10 changed files with 134 additions and 37 deletions

View File

@ -16,6 +16,7 @@
*/ */
using FirmTracker_Server.nHibernate.Expenses; using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Products;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace FirmTracker_Server.Controllers namespace FirmTracker_Server.Controllers
{ {
@ -36,10 +37,20 @@ namespace FirmTracker_Server.Controllers
public IActionResult CreateExpense([FromBody] Expense expense) { public IActionResult CreateExpense([FromBody] Expense expense) {
try try
{ {
if (expense.Value <= 0)
{
throw new InvalidOperationException("Wydatek nie może posiadać kwoty mniejszej lub równej 0.");
}
_expenseCrud.AddExpense(expense); _expenseCrud.AddExpense(expense);
return CreatedAtAction("GetExpense", new { id = expense.Id }, 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); return BadRequest(ex.Message);
} }
} }
@ -64,16 +75,26 @@ namespace FirmTracker_Server.Controllers
[ProducesResponseType(400)] [ProducesResponseType(400)]
public IActionResult UpdateExpense(int id, [FromBody] Expense expense) public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
{ {
if (id != expense.Id)
{
return BadRequest("Expense ID mismatch");
}
try 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); _expenseCrud.UpdateExpense(expense);
return NoContent(); return NoContent();
} }
catch (System.Exception ex) catch (InvalidOperationException ioe)
{
return BadRequest(ioe.Message);
}
catch (Exception ex)
{ {
return BadRequest(ex.Message); return BadRequest(ex.Message);
} }
@ -89,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

@ -17,6 +17,8 @@
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System;
namespace FirmTracker_Server.Controllers namespace FirmTracker_Server.Controllers
{ {
[Route("api/[controller]")] [Route("api/[controller]")]
@ -38,21 +40,34 @@ namespace FirmTracker_Server.Controllers
[ProducesResponseType(200)] // Created [ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request [ProducesResponseType(400)] // Bad Request
public IActionResult CreateProduct([FromBody] Product product) public IActionResult CreateProduct([FromBody] Product product)
{
try
{ {
if (product.Type != 0 && product.Type != 1) 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) 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.");
} }
try 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); _productCrud.AddProduct(product);
return CreatedAtAction("GetProduct", new { id = product.Id }, 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); return BadRequest(ex.Message);
} }
@ -86,24 +101,37 @@ namespace FirmTracker_Server.Controllers
[ProducesResponseType(200)] // Created [ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request [ProducesResponseType(400)] // Bad Request
public IActionResult UpdateProduct(int id, [FromBody] Product product) public IActionResult UpdateProduct(int id, [FromBody] Product product)
{
try
{ {
if (id != product.Id) 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) 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) 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); _productCrud.UpdateProduct(product);
return NoContent(); return NoContent();
} }
catch (System.Exception ex) catch (InvalidOperationException ioe)
{
return BadRequest(ioe.Message);
}
catch (Exception ex)
{ {
return BadRequest(ex.Message); return BadRequest(ex.Message);
} }
@ -120,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

@ -54,7 +54,7 @@ namespace FirmTracker_Server
var products = new List<Product> var products = new List<Product>
{ {
CreateProduct("Tarta_truskawka", "produkt", 31.99m, 1, 10), 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_agrest", "produkt", 32.90m, 1, 8),
CreateProduct("Tarta_pistacja", "produkt", 35.99m, 1, 12), CreateProduct("Tarta_pistacja", "produkt", 35.99m, 1, 12),
CreateProduct("Tarta_karmel", "produkt", 32.00m, 1, 12), CreateProduct("Tarta_karmel", "produkt", 32.00m, 1, 12),

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

@ -18,7 +18,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
HasMany(x => x.TransactionProducts) HasMany(x => x.TransactionProducts)
.KeyColumn("TransactionId") .KeyColumn("TransactionId")
.Cascade.AllDeleteOrphan() .Cascade.AllDeleteOrphan()
.Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji .Inverse();
} }
} }

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);

View File

@ -18,7 +18,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
HasMany(x => x.TransactionProducts) HasMany(x => x.TransactionProducts)
.KeyColumn("TransactionId") .KeyColumn("TransactionId")
.Cascade.AllDeleteOrphan() .Cascade.AllDeleteOrphan()
.Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji .Inverse();
} }
} }