Compare commits
3 Commits
4e023be7ca
...
f09289c101
Author | SHA1 | Date | |
---|---|---|---|
f09289c101 | |||
7b54a5d796 | |||
8f3f1e5b1e |
98
Controllers/ExpenseController.cs
Normal file
98
Controllers/ExpenseController.cs
Normal file
@ -0,0 +1,98 @@
|
||||
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);
|
||||
}
|
||||
|
||||
//PUT: api/Expenses
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(200)]
|
||||
public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
|
||||
{
|
||||
if (id != expense.Id)
|
||||
{
|
||||
return BadRequest("Expense ID mismatch");
|
||||
}
|
||||
try
|
||||
{
|
||||
_expenseCrud.UpdateExpense(expense);
|
||||
return NoContent();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult DeleteExpense(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_expenseCrud.DeleteExpense(id);
|
||||
return NoContent();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult GetAllExpenses()
|
||||
{
|
||||
try
|
||||
{
|
||||
var expenses = _expenseCrud.GetAllExpenses();
|
||||
return Ok(expenses);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -8,6 +8,10 @@
|
||||
<UserSecretsId>08986e21-848b-485a-a219-03e2dc6041e4</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="nHIbernate\Transactions\TransactionCRUD2.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Properties\launchSettings.json" />
|
||||
</ItemGroup>
|
||||
|
51
TestClass.cs
51
TestClass.cs
@ -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
|
||||
@ -28,6 +29,14 @@ namespace FirmTracker_Server
|
||||
Type = 0,
|
||||
Availability = 0
|
||||
};
|
||||
var product3 = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Produkt 2",
|
||||
Description = "produkt",
|
||||
Price = 16.50m,
|
||||
Type = 1,
|
||||
Availability = 20
|
||||
};
|
||||
var transaction1 = new Transaction
|
||||
{
|
||||
Date = DateTime.Now,
|
||||
@ -36,25 +45,57 @@ namespace FirmTracker_Server
|
||||
EmployeeId = 1,
|
||||
PaymentType = "Karta kredytowa",
|
||||
};
|
||||
var transaction2 = new Transaction
|
||||
{
|
||||
Date = DateTime.Now,
|
||||
Description = "testowa transakcja",
|
||||
Discount = 30,
|
||||
EmployeeId = 2,
|
||||
PaymentType = "Gotówka",
|
||||
};
|
||||
|
||||
var expense1 = new Expense
|
||||
{
|
||||
Date = DateTime.Now,
|
||||
Value = 1003.9m,
|
||||
Description = "testowy rozchód"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
|
||||
FirmTracker_Server.nHibernate.Products.ProductCRUD productCrud = new ProductCRUD();
|
||||
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
|
||||
crud.AddProduct(product);
|
||||
crud.AddProduct(product2);
|
||||
ExpenseCRUD expenseCrud = new ExpenseCRUD();
|
||||
productCrud.AddProduct(product);
|
||||
productCrud.AddProduct(product2);
|
||||
productCrud.AddProduct(product3);
|
||||
transactionCrud.AddTransaction(transaction1);
|
||||
transactionCrud.AddTransaction(transaction2);
|
||||
expenseCrud.AddExpense(expense1);
|
||||
|
||||
|
||||
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
||||
new TransactionProduct { ProductID = 1, Quantity = 2 },
|
||||
new TransactionProduct { ProductID = 2, Quantity = 1 }
|
||||
new TransactionProduct { ProductID = 2, Quantity = 1 },
|
||||
new TransactionProduct { ProductID = 3, Quantity = 10 }
|
||||
};
|
||||
foreach (var transactionProduct in testTransactionProducts)
|
||||
{
|
||||
transactionCrud.AddTransactionProductToTransaction(transaction1.Id, transactionProduct);
|
||||
|
||||
}
|
||||
|
||||
|
||||
List<TransactionProduct> testTransactionProducts2 = new List<TransactionProduct>
|
||||
{
|
||||
new TransactionProduct { ProductID = 3, Quantity=4},
|
||||
new TransactionProduct { ProductID = 1, Quantity=6}
|
||||
};
|
||||
foreach (var transactionProduct in testTransactionProducts2)
|
||||
{
|
||||
transactionCrud.AddTransactionProductToTransaction(transaction2.Id, transactionProduct);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch(Exception ex)
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"AppSettings": {
|
||||
"ConnectionString": "Server=localhost;Database=FirmTrackerDB;User Id=sa;Password=Rap45tro2;TrustServerCertificate=True;"
|
||||
"ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;"
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
|
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 Value { get; set; }
|
||||
public virtual string Description { get; set; }
|
||||
}
|
||||
}
|
107
nHIbernate/Expenses/ExpenseCRUD.cs
Normal file
107
nHIbernate/Expenses/ExpenseCRUD.cs
Normal file
@ -0,0 +1,107 @@
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
public DateTime GetExpenseDate(int expenseId)
|
||||
{
|
||||
using(var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var expense = session.Query<Expense>()
|
||||
.Where(e => e.Id == expenseId)
|
||||
.Select(e => e.Date)
|
||||
.FirstOrDefault();
|
||||
return expense;
|
||||
}
|
||||
}
|
||||
public decimal GetExpenseValue(int expenseId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var expense = session.Query<Expense>()
|
||||
.Where(e => e.Id == expenseId)
|
||||
.Select(e => e.Value)
|
||||
.FirstOrDefault();
|
||||
return expense;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateExpense(Expense expense)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
session.Update(expense);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteExpense(int expenseId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
try
|
||||
{
|
||||
var expense = session.Get<Expense>(expenseId);
|
||||
if (expense != null)
|
||||
{
|
||||
session.Delete(expense);
|
||||
transaction.Commit();
|
||||
}
|
||||
|
||||
}
|
||||
catch {
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public IList<Expense> GetAllExpenses()
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
return session.Query<Expense>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
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.Value);
|
||||
Map(x => x.Description);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user