PI2024-9 #1

Merged
s464958 merged 8 commits from PI2024-9 into master 2024-06-03 15:17:12 +02:00
6 changed files with 122 additions and 1 deletions
Showing only changes of commit 7b54a5d796 - Show all commits

View File

@ -0,0 +1,44 @@
using FirmTracker_Server.nHibernate.Expenses;
using Microsoft.AspNetCore.Mvc;
namespace FirmTracker_Server.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ExpensesController : ControllerBase
{
private readonly ExpenseCRUD _expenseCrud;
public ExpensesController()
{
_expenseCrud = new ExpenseCRUD();
}
// POST: api/Expenses
[HttpPost]
[ProducesResponseType(201)] // Created
[ProducesResponseType(400)] // Bad Request
public IActionResult CreateExpense([FromBody] Expense expense) {
try
{
_expenseCrud.AddExpense(expense);
return CreatedAtAction("GetExpense", new { id = expense.Id }, expense);
}
catch (System.Exception ex) {
return BadRequest(ex.Message);
}
}
// GET: api/Expenses
[HttpGet("{id}")]
[ProducesResponseType(200)] // Created
[ProducesResponseType(400)] // Bad Request
public IActionResult GetExpense(int id)
{
var expense = _expenseCrud.GetExpense(id);
if (expense == null)
{
return NotFound();
}
return Ok(expense);
}
}
}

View File

@ -2,6 +2,7 @@
using FirmTracker_Server.nHibernate;
using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate.Transactions;
using FirmTracker_Server.nHibernate.Expenses;
using NHibernate;
namespace FirmTracker_Server
@ -37,13 +38,23 @@ namespace FirmTracker_Server
PaymentType = "Karta kredytowa",
};
var expense1 = new Expense
{
Date = DateTime.Now,
TotalPrice = 10.5m,
Description = "testowy rozchód"
};
try
{
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
ExpenseCRUD expenseCrud = new ExpenseCRUD();
crud.AddProduct(product);
crud.AddProduct(product2);
transactionCrud.AddTransaction(transaction1);
expenseCrud.AddExpense(expense1);
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
new TransactionProduct { ProductID = 1, Quantity = 2 },

View File

@ -0,0 +1,10 @@
namespace FirmTracker_Server.nHibernate.Expenses
{
public class Expense
{
public virtual int Id { get; set; }
public virtual DateTime Date { get; set; }
public virtual decimal TotalPrice { get; set; }
public virtual string Description { get; set; }
}
}

View File

@ -0,0 +1,38 @@
using FirmTracker_Server.nHibernate;
namespace FirmTracker_Server.nHibernate.Expenses
{
public class ExpenseCRUD
{
public void AddExpense(Expense expense)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
try
{
session.Save(expense);
transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
}
}
public Expense GetExpense(int expenseId) {
using (var session = SessionFactory.OpenSession())
{
/*var query = session.CreateQuery(@"
SELECT e
FROM Expense e
WHERE e.Id = expenseId
");
query.SetParameter("expenseId", expenseId);
var expense = query.UniqueResult<Expense>();*/
return session.Get<Expense>(expenseId);
}
}
}
}

View File

@ -0,0 +1,17 @@
using FluentNHibernate.Mapping;
namespace FirmTracker_Server.nHibernate.Expenses
{
public class ExpenseMapping : ClassMap<Expense>
{
public ExpenseMapping()
{
Table("Expenses");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Date);
Map(x => x.TotalPrice);
Map(x => x.Description);
}
}
}

View File

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