Compare commits

...

5 Commits

14 changed files with 317 additions and 2 deletions

View File

@ -0,0 +1,75 @@
using FirmTracker_Server.nHibernate.Reports;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json.Serialization;
using System.Text.Json;
using NuGet.Protocol;
namespace FirmTracker_Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ReportController : ControllerBase
{
private readonly ReportCRUD _reportCRUD;
public ReportController()
{
_reportCRUD = new ReportCRUD();
}
[HttpPost]
[ProducesResponseType(201)] //Created
[ProducesResponseType(400)] //Bad request
public IActionResult CreateReport([FromBody] Report.DateRangeDto dateRange)
{
try
{
if (dateRange == null || dateRange.FromDate >= dateRange.ToDate)
{
return BadRequest("Invalid date range.");
}
var report = _reportCRUD.AddReport(dateRange.FromDate, dateRange.ToDate);
return CreatedAtAction(nameof(GetReport), new { id = report.Id }, report); // to change?
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public IActionResult GetReport(int id)
{
var report = _reportCRUD.GetReport(id);
if (report == null)
return NotFound();
var options = new JsonSerializerOptions
{
ReferenceHandler = ReferenceHandler.Preserve // Obsługa cykli obiektów
};
var json = JsonSerializer.Serialize(report, options);
return Ok(json);
}
[HttpGet]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public IActionResult GetAllReports()
{
var reports = _reportCRUD.GetAllReports();
if (reports == null)
return NotFound();
return Ok(reports);
}
}
}

View File

@ -1,4 +1,6 @@
namespace FirmTracker_Server.nHibernate.Expenses using FirmTracker_Server.nHibernate.Reports;
namespace FirmTracker_Server.nHibernate.Expenses
{ {
public class Expense public class Expense
{ {

View File

@ -0,0 +1,29 @@
using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Transactions;
using Newtonsoft.Json;
namespace FirmTracker_Server.nHibernate.Reports
{
public class Report
{
public virtual int Id { get; set; }
public virtual DateTime FromDate { get; set; }
public virtual DateTime ToDate { get; set; }
public virtual decimal TotalIncome { get; set; }
public virtual decimal TotalExpenses { get; set; }
public virtual decimal TotalBalance { get; set; }
public virtual IList<ReportTransaction> ReportTransactions { get; set; } = new List<ReportTransaction>();
public virtual IList<ReportExpense> ReportExpenses { get; set; } = new List<ReportExpense>();
public class DateRangeDto
{
public DateTime FromDate { get; set; }
public DateTime ToDate { get; set; }
}
}
}

View File

@ -0,0 +1,27 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportMapping : ClassMap<Report>
{
public ReportMapping()
{
Table("Reports");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.FromDate);
Map(x => x.ToDate);
Map(x => x.TotalIncome);
Map(x => x.TotalExpenses);
Map(x => x.TotalBalance);
HasMany(x => x.ReportTransactions)
.Cascade.All()
.Inverse();
HasMany(x => x.ReportExpenses)
.Cascade.All()
.Inverse();
}
}
}

View File

@ -33,7 +33,10 @@ namespace FirmTracker_Server.nHibernate
.AddFromAssemblyOf<Transactions.TransactionProductMapping>() .AddFromAssemblyOf<Transactions.TransactionProductMapping>()
.AddFromAssemblyOf<Transactions.Transaction2Mapping>() .AddFromAssemblyOf<Transactions.Transaction2Mapping>()
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>() .AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>()
.AddFromAssemblyOf<Expenses.ExpenseMapping>(); .AddFromAssemblyOf<Expenses.ExpenseMapping>()
.AddFromAssemblyOf<Reports.ReportMapping>()
.AddFromAssemblyOf<Reports.ReportTransactionMapping>()
.AddFromAssemblyOf<Reports.ReportExpenseMapping>();
}) })
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
.BuildSessionFactory(); .BuildSessionFactory();

View File

@ -0,0 +1,93 @@
using FirmTracker_Server.nHibernate.Expenses;
using FirmTracker_Server.nHibernate.Transactions;
using NHibernate;
using NHibernate.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportCRUD
{
public Report AddReport(DateTime fromDate, DateTime toDate)
{
using (var session = SessionFactory.OpenSession())
using (var sessionTransaction = session.BeginTransaction())
{
try
{
var transactions = session.Query<nHibernate.Transactions.Transaction>()
.Where(t => t.Date >= fromDate && t.Date <= toDate)
.ToList();
var expenses = session.Query<Expense>()
.Where(e => e.Date >= fromDate && e.Date <= toDate)
.ToList();
var totalIncome = transactions.Sum(t => t.TotalPrice);
var totalExpenses = expenses.Sum(e => e.Value);
var totalBalance = totalIncome - totalExpenses;
var report = new Report
{
FromDate = fromDate,
ToDate = toDate,
TotalIncome = totalIncome,
TotalExpenses = totalExpenses,
TotalBalance = totalBalance
};
foreach (var transactionItem in transactions)
{
var reportTransaction = new ReportTransaction
{
ReportId = report.Id,
TransactionId = transactionItem.Id
};
report.ReportTransactions.Add(reportTransaction);
}
foreach (var expenseItem in expenses)
{
var reportExpense = new ReportExpense
{
ReportId = report.Id,
ExpenseId = expenseItem.Id
};
report.ReportExpenses.Add(reportExpense);
}
session.Save(report);
sessionTransaction.Commit();
return report;
}
catch
{
sessionTransaction.Rollback();
throw;
}
}
}
public Report GetReport(int reportId)
{
using (var session = SessionFactory.OpenSession())
{
return session.Get<Report>(reportId);
}
}
public IList<Report> GetAllReports()
{
using (var session = SessionFactory.OpenSession())
{
var reports = session.Query<Report>()
.ToList();
return reports;
}
}
}
}

View File

@ -0,0 +1,26 @@
using FirmTracker_Server.nHibernate.Expenses;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportExpense
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int ExpenseId { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
var other = (ReportExpense)obj;
return ReportId == other.ReportId && ExpenseId == other.ExpenseId;
}
public override int GetHashCode()
{
return HashCode.Combine(ReportId, ExpenseId);
}
}
}

View File

@ -0,0 +1,15 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportExpenseMapping : ClassMap<ReportExpense>
{
public ReportExpenseMapping()
{
Table("ReportExpenses");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.ExpenseId, "ExpenseId");
}
}
}

View File

@ -0,0 +1,26 @@
using FirmTracker_Server.nHibernate.Transactions;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportTransaction
{
public virtual int Id { get; set; }
public virtual int ReportId { get; set; }
public virtual int TransactionId { get; set; }
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
return false;
var other = (ReportTransaction)obj;
return ReportId == other.ReportId && TransactionId == other.TransactionId;
}
public override int GetHashCode()
{
return HashCode.Combine(ReportId, TransactionId);
}
}
}

View File

@ -0,0 +1,15 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Reports
{
public class ReportTransactionMapping : ClassMap<ReportTransaction>
{
public ReportTransactionMapping()
{
Table("ReportTransactions");
Id(x => x.Id);
References(x => x.ReportId, "ReportId");
References(x => x.TransactionId, "TransactionId");
}
}
}

View File

@ -1,4 +1,5 @@
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate.Reports;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

@ -1,4 +1,5 @@
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate.Reports;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;

View File

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

View File

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