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);
return NoContent();
}
catch (InvalidOperationException ioe)
{
return BadRequest($"{ioe.Message}");
}
catch (System.Exception ex)
{
return NotFound(ex.Message);

View File

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

View File

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

View File

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

View File

@ -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<Expense>(expenseId);
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);
transaction.Commit();
}

View File

@ -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<Product>(productId);
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);
transaction.Commit();
}

View File

@ -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<Transaction>(transactionId);
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)
{
var product = session.Get<Product>(transactionProduct.ProductID);