poprawki do cruda expenses

This commit is contained in:
kamil 2024-05-16 21:25:07 +02:00
parent 7b54a5d796
commit f09289c101
6 changed files with 165 additions and 8 deletions

View File

@ -40,5 +40,59 @@ namespace FirmTracker_Server.Controllers
} }
return Ok(expense); 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> <UserSecretsId>08986e21-848b-485a-a219-03e2dc6041e4</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="nHIbernate\Transactions\TransactionCRUD2.cs" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Properties\launchSettings.json" /> <Content Include="Properties\launchSettings.json" />
</ItemGroup> </ItemGroup>

View File

@ -29,6 +29,14 @@ namespace FirmTracker_Server
Type = 0, Type = 0,
Availability = 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 var transaction1 = new Transaction
{ {
Date = DateTime.Now, Date = DateTime.Now,
@ -37,28 +45,39 @@ namespace FirmTracker_Server
EmployeeId = 1, EmployeeId = 1,
PaymentType = "Karta kredytowa", PaymentType = "Karta kredytowa",
}; };
var transaction2 = new Transaction
{
Date = DateTime.Now,
Description = "testowa transakcja",
Discount = 30,
EmployeeId = 2,
PaymentType = "Gotówka",
};
var expense1 = new Expense var expense1 = new Expense
{ {
Date = DateTime.Now, Date = DateTime.Now,
TotalPrice = 10.5m, Value = 1003.9m,
Description = "testowy rozchód" Description = "testowy rozchód"
}; };
try 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(); FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
ExpenseCRUD expenseCrud = new ExpenseCRUD(); ExpenseCRUD expenseCrud = new ExpenseCRUD();
crud.AddProduct(product); productCrud.AddProduct(product);
crud.AddProduct(product2); productCrud.AddProduct(product2);
productCrud.AddProduct(product3);
transactionCrud.AddTransaction(transaction1); transactionCrud.AddTransaction(transaction1);
transactionCrud.AddTransaction(transaction2);
expenseCrud.AddExpense(expense1); 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 },
new TransactionProduct { ProductID = 2, Quantity = 1 } new TransactionProduct { ProductID = 2, Quantity = 1 },
new TransactionProduct { ProductID = 3, Quantity = 10 }
}; };
foreach (var transactionProduct in testTransactionProducts) foreach (var transactionProduct in testTransactionProducts)
{ {
@ -66,6 +85,17 @@ namespace FirmTracker_Server
} }
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) catch(Exception ex)

View File

@ -4,7 +4,7 @@
{ {
public virtual int Id { get; set; } public virtual int Id { get; set; }
public virtual DateTime Date { get; set; } public virtual DateTime Date { get; set; }
public virtual decimal TotalPrice { get; set; } public virtual decimal Value { get; set; }
public virtual string Description { get; set; } public virtual string Description { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using FirmTracker_Server.nHibernate.Products;
namespace FirmTracker_Server.nHibernate.Expenses namespace FirmTracker_Server.nHibernate.Expenses
{ {
@ -34,5 +35,73 @@ namespace FirmTracker_Server.nHibernate.Expenses
return session.Get<Expense>(expenseId); 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

@ -9,7 +9,7 @@ namespace FirmTracker_Server.nHibernate.Expenses
Table("Expenses"); Table("Expenses");
Id(x => x.Id).GeneratedBy.Identity(); Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Date); Map(x => x.Date);
Map(x => x.TotalPrice); Map(x => x.Value);
Map(x => x.Description); Map(x => x.Description);
} }