update/delete dla raportu + drobne poprawki

This commit is contained in:
Kamil Ryżek 2024-06-09 23:35:15 +02:00
parent ffbcb447a6
commit 207ee08330
3 changed files with 174 additions and 5 deletions

View File

@ -30,7 +30,7 @@ namespace FirmTracker_Server.Controllers
// GET: api/Expenses
[HttpGet("{id}")]
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
[ProducesResponseType(404)] // Bad Request
public IActionResult GetExpense(int id)
{
var expense = _expenseCrud.GetExpense(id);
@ -43,8 +43,8 @@ namespace FirmTracker_Server.Controllers
//PUT: api/Expenses
[HttpPut("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(200)]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
{
if (id != expense.Id)
@ -63,8 +63,8 @@ namespace FirmTracker_Server.Controllers
}
[HttpDelete("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public IActionResult DeleteExpense(int id)
{
try

View File

@ -8,6 +8,7 @@ using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate;
using NHibernate.Linq;
namespace FirmTracker_Server.Controllers
{
[Route("api/[controller]")]
@ -31,6 +32,10 @@ namespace FirmTracker_Server.Controllers
{
var fromDate = dateRange.FromDate;
var toDate = dateRange.ToDate;
if (fromDate >= toDate)
{
return BadRequest();
}
using (var session = SessionFactory.OpenSession())
{
var transactions = session.Query<nHibernate.Transactions.Transaction>()
@ -125,5 +130,90 @@ namespace FirmTracker_Server.Controllers
return Ok(reports);
}
[HttpPut("{id}")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public IActionResult UpdateReport(int reportId, [FromBody] Report.DateRangeDto dateRange)
{
try
{
var report = _reportCRUD.GetReport(reportId);
if (report == null)
{
return NotFound();
}
using (var session = SessionFactory.OpenSession())
{
var fromDate = dateRange.FromDate;
var toDate = dateRange.ToDate;
if (fromDate >= toDate)
{
return BadRequest();
}
var transactions = session.Query<nHibernate.Transactions.Transaction>()
.Where(t => t.Date >= fromDate && t.Date <= toDate)
.FetchMany(t => t.TransactionProducts)
.ToList();
var expenses = session.Query<Expense>()
.Where(e => e.Date >= fromDate && e.Date <= toDate)
.ToList();
// Calculate total income from transactions
decimal totalIncome = 0;
foreach (var transaction in transactions)
{
foreach (var product in transaction.TransactionProducts)
{
decimal price = _productCRUD.GetProductPrice(product.ProductID);
int type = _productCRUD.GetProductType(product.ProductID);
if (type == 1)
{
totalIncome += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
}
else
{
totalIncome += (price * ((1 - (transaction.Discount / 100))));
}
}
}
var totalExpenses = expenses.Sum(e => e.Value);
var totalBalance = totalIncome - totalExpenses;
report.FromDate = fromDate;
report.ToDate = toDate;
report.TotalIncome = totalIncome;
report.TotalExpenses = totalExpenses;
report.TotalBalance = totalBalance;
_reportCRUD.UpdateReport(report, transactions, expenses);
return NoContent();
}
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpDelete]
[ProducesResponseType(204)]
[ProducesResponseType(404)]
public IActionResult DeleteReport(int reportId)
{
try
{
_reportCRUD.DeleteReport(reportId);
return NoContent();
}
catch (Exception ex)
{
return NotFound(ex.Message);
}
}
}
}

View File

@ -98,5 +98,84 @@ namespace FirmTracker_Server.nHibernate.Reports
return reports;
}
}
public Report UpdateReport(Report report, IList<nHibernate.Transactions.Transaction> transactions, IList<Expense> expenses)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
session.Update(report);
session.CreateQuery("delete from ReportTransaction where Report.Id = :reportId")
.SetParameter("reportId", report.Id)
.ExecuteUpdate();
session.CreateQuery("delete from ReportExpense where Report.Id = :reportId")
.SetParameter("reportId", report.Id)
.ExecuteUpdate();
foreach (var transactionItem in transactions)
{
var reportTransaction = new ReportTransaction
{
Report = report,
Transaction = transactionItem
};
session.Save(reportTransaction);
}
foreach (var expenseItem in expenses)
{
var reportExpense = new ReportExpense
{
Report = report,
Expense = expenseItem
};
session.Save(reportExpense);
}
transaction.Commit();
return report;
}
catch
{
transaction.Rollback();
throw;
}
}
}
public void DeleteReport(int reportId)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
session.CreateQuery("delete from ReportTransaction where Report.Id = :reportId")
.SetParameter("reportId", reportId)
.ExecuteUpdate();
session.CreateQuery("delete from ReportExpense where Report.Id = :reportId")
.SetParameter("reportId", reportId)
.ExecuteUpdate();
var report = session.Get<Report>(reportId);
if (report != null)
{
session.Delete(report);
}
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
}
}