Compare commits

..

3 Commits

Author SHA1 Message Date
f09289c101 poprawki do cruda expenses 2024-05-16 21:25:07 +02:00
7b54a5d796 added expense object with crud, mapping and controller 2024-05-16 18:02:44 +02:00
8f3f1e5b1e connection string 2024-05-16 12:19:35 +02:00
8 changed files with 285 additions and 7 deletions

View 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);
}
}
}
}

View File

@ -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>

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
@ -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)

View File

@ -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": {

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 Value { get; set; }
public virtual string Description { get; set; }
}
}

View 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();
}
}
}
}

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.Value);
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();