PI2024-9 #1
44
Controllers/ExpenseController.cs
Normal file
44
Controllers/ExpenseController.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
11
TestClass.cs
11
TestClass.cs
@ -2,6 +2,7 @@
|
|||||||
using FirmTracker_Server.nHibernate;
|
using FirmTracker_Server.nHibernate;
|
||||||
using FirmTracker_Server.nHibernate.Products;
|
using FirmTracker_Server.nHibernate.Products;
|
||||||
using FirmTracker_Server.nHibernate.Transactions;
|
using FirmTracker_Server.nHibernate.Transactions;
|
||||||
|
using FirmTracker_Server.nHibernate.Expenses;
|
||||||
using NHibernate;
|
using NHibernate;
|
||||||
|
|
||||||
namespace FirmTracker_Server
|
namespace FirmTracker_Server
|
||||||
@ -37,13 +38,23 @@ namespace FirmTracker_Server
|
|||||||
PaymentType = "Karta kredytowa",
|
PaymentType = "Karta kredytowa",
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var expense1 = new Expense
|
||||||
|
{
|
||||||
|
Date = DateTime.Now,
|
||||||
|
TotalPrice = 10.5m,
|
||||||
|
Description = "testowy rozchód"
|
||||||
|
};
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
|
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
|
||||||
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
|
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
|
||||||
|
ExpenseCRUD expenseCrud = new ExpenseCRUD();
|
||||||
crud.AddProduct(product);
|
crud.AddProduct(product);
|
||||||
crud.AddProduct(product2);
|
crud.AddProduct(product2);
|
||||||
transactionCrud.AddTransaction(transaction1);
|
transactionCrud.AddTransaction(transaction1);
|
||||||
|
expenseCrud.AddExpense(expense1);
|
||||||
|
|
||||||
|
|
||||||
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
||||||
new TransactionProduct { ProductID = 1, Quantity = 2 },
|
new TransactionProduct { ProductID = 1, Quantity = 2 },
|
||||||
|
10
nHIbernate/Expenses/Expense.cs
Normal file
10
nHIbernate/Expenses/Expense.cs
Normal 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; }
|
||||||
|
}
|
||||||
|
}
|
38
nHIbernate/Expenses/ExpenseCRUD.cs
Normal file
38
nHIbernate/Expenses/ExpenseCRUD.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
17
nHIbernate/Expenses/ExpenseMapping.cs
Normal file
17
nHIbernate/Expenses/ExpenseMapping.cs
Normal 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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -32,7 +32,8 @@ namespace FirmTracker_Server.nHibernate
|
|||||||
.AddFromAssemblyOf<Transactions.TransactionMapping>()
|
.AddFromAssemblyOf<Transactions.TransactionMapping>()
|
||||||
.AddFromAssemblyOf<Transactions.TransactionProductMapping>()
|
.AddFromAssemblyOf<Transactions.TransactionProductMapping>()
|
||||||
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
|
.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
|
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
|
||||||
.BuildSessionFactory();
|
.BuildSessionFactory();
|
||||||
|
Loading…
Reference in New Issue
Block a user