diff --git a/Controllers/TransactionController.cs b/Controllers/TransactionController.cs
index 29dc5e2..febb74e 100644
--- a/Controllers/TransactionController.cs
+++ b/Controllers/TransactionController.cs
@@ -1,8 +1,9 @@
using Microsoft.AspNetCore.Mvc;
using FirmTracker_Server.nHibernate.Transactions;
-using FirmTracker_Server;
-using System.Collections.Generic;
-
+using System;
+using System.Text.Json.Serialization;
+using System.Text.Json;
+using System.Transactions;
namespace FirmTracker_Server.Controllers
{
@@ -24,7 +25,7 @@ namespace FirmTracker_Server.Controllers
[HttpPost]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
- public IActionResult CreateTransaction([FromBody] Transaction transaction)
+ public IActionResult CreateTransaction([FromBody] nHibernate.Transactions.Transaction transaction)
{
try
{
@@ -53,7 +54,7 @@ namespace FirmTracker_Server.Controllers
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
- public IActionResult UpdateTransaction(int id, [FromBody] Transaction transaction)
+ public IActionResult UpdateTransaction(int id, [FromBody] nHibernate.Transactions.Transaction transaction)
{
if (id != transaction.Id)
return BadRequest("Transaction ID mismatch");
@@ -90,11 +91,24 @@ namespace FirmTracker_Server.Controllers
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
+ [ProducesResponseType(StatusCodes.Status404NotFound)]
public IActionResult GetAllTransactions()
{
var transactions = _transactionCRUD.GetAllTransactions();
+ if (transactions == null)
+ return NotFound();
+
+ // Ustawienie opcji serializatora JSON
+ var options = new JsonSerializerOptions
+ {
+ ReferenceHandler = ReferenceHandler.Preserve // Obsługa cykli obiektów
+ };
+
+ // var json = JsonSerializer.Serialize(transactions, options);
+
+ // Zwrócenie odpowiedzi z JSON
return Ok(transactions);
}
}
-}
\ No newline at end of file
+}
diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj
index 3616e11..e6f7bb1 100644
--- a/FirmTracker-Server.csproj
+++ b/FirmTracker-Server.csproj
@@ -17,6 +17,7 @@
+
diff --git a/TestClass.cs b/TestClass.cs
index e16dc79..892a4ba 100644
--- a/TestClass.cs
+++ b/TestClass.cs
@@ -1,6 +1,7 @@
using FirmTracker_Server.Controllers;
using FirmTracker_Server.nHibernate;
using FirmTracker_Server.nHibernate.Products;
+using FirmTracker_Server.nHibernate.Transactions;
using NHibernate;
namespace FirmTracker_Server
@@ -27,14 +28,13 @@ namespace FirmTracker_Server
Type = 0,
Availability = 0
};
- var transaction1 = new nHibernate.Transactions.Transaction
+ var transaction1 = new Transaction
{
Date = DateTime.Now,
Description = "testowa transakcja",
Discount = 10,
EmployeeId = 1,
PaymentType = "Karta kredytowa",
- Products = new List { product, product2 }
};
try
@@ -44,9 +44,22 @@ namespace FirmTracker_Server
crud.AddProduct(product);
crud.AddProduct(product2);
transactionCrud.AddTransaction(transaction1);
+
+ List testTransactionProducts = new List {
+ new TransactionProduct { Product = product, Quantity = 2, UnitPrice = product.Price },
+ new TransactionProduct { Product = product2, Quantity = 1, UnitPrice = product2.Price }
+ };
+ foreach (var transactionProduct in testTransactionProducts)
+ {
+ transactionCrud.AddTransactionProductToTransaction(transaction1.Id, transactionProduct);
+
+ }
+
+
}
catch(Exception ex)
{
+ Console.WriteLine(ex.ToString());
throw;
}
}
diff --git a/nHIbernate/SessionFactory.cs b/nHIbernate/SessionFactory.cs
index e08bb24..afed35c 100644
--- a/nHIbernate/SessionFactory.cs
+++ b/nHIbernate/SessionFactory.cs
@@ -29,7 +29,8 @@ namespace FirmTracker_Server.nHibernate
{
m.FluentMappings
.AddFromAssemblyOf()
- .AddFromAssemblyOf();
+ .AddFromAssemblyOf()
+ .AddFromAssemblyOf();
})
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
.BuildSessionFactory();
diff --git a/nHibernate/Transactions/Transaction.cs b/nHibernate/Transactions/Transaction.cs
index 448f118..995c34d 100644
--- a/nHibernate/Transactions/Transaction.cs
+++ b/nHibernate/Transactions/Transaction.cs
@@ -1,21 +1,24 @@
using FirmTracker_Server.nHibernate.Products;
-using System.Text.Json.Serialization;
+using System;
+using System.Collections.Generic;
+using System.Linq;
namespace FirmTracker_Server.nHibernate.Transactions
{
public class Transaction
{
public virtual int Id { get; set; }
- public virtual DateTime Date { get; set; }
+ public virtual DateTime Date { get; set; }
public virtual int EmployeeId { get; set; }
- public virtual IList Products { get; set; } = new List();
+ public virtual IList TransactionProducts { get; set; } = new List();
public virtual string PaymentType { get; set; }
- public virtual int Discount { get; set; }
+ public virtual decimal Discount { get; set; }
public virtual string Description { get; set; }
+ public virtual decimal TotalPrice => TransactionProducts.Sum(tp => ((tp.Quantity * tp.UnitPrice)* ((1 - (Discount / 100)))));// (1 - (Discount / 100)));
public Transaction()
{
- Products = new List();
+ TransactionProducts = new List();
}
}
}
diff --git a/nHibernate/Transactions/TransactionCRUD.cs b/nHibernate/Transactions/TransactionCRUD.cs
index 8fb8e21..4cd65ba 100644
--- a/nHibernate/Transactions/TransactionCRUD.cs
+++ b/nHibernate/Transactions/TransactionCRUD.cs
@@ -1,11 +1,9 @@
-using FirmTracker_Server.nHibernate;
-using FirmTracker_Server.nHibernate.Products;
+using FirmTracker_Server.nHibernate.Products;
using NHibernate;
-using System.Collections.Generic;
-using Microsoft.AspNetCore.OpenApi;
-using Microsoft.AspNetCore.Http.HttpResults;
-using FirmTracker_Server.nHibernate.Transactions;
using NHibernate.Linq;
+using System;
+using System.Collections.Generic;
+using System.Linq;
namespace FirmTracker_Server.nHibernate.Transactions
{
@@ -18,7 +16,24 @@ namespace FirmTracker_Server.nHibernate.Transactions
{
try
{
+ foreach (var transactionProduct in transaction.TransactionProducts)
+ {
+ transactionProduct.TransactionId = transaction.Id;
+ session.Save(transactionProduct);
+ }
session.Save(transaction);
+
+ // Decrease product quantities based on transaction
+ foreach (var transactionProduct in transaction.TransactionProducts)
+ {
+ var product = session.Get(transactionProduct.Product.Id);
+ if (product.Type != 0)
+ {
+ product.Availability -= transactionProduct.Quantity;
+ session.Update(product);
+ }
+ }
+
sessionTransaction.Commit();
}
catch
@@ -29,17 +44,25 @@ namespace FirmTracker_Server.nHibernate.Transactions
}
}
+ //usage of HQL
public Transaction GetTransaction(int transactionId)
{
using (var session = SessionFactory.OpenSession())
{
- var transaction = session.Query()
- .Fetch(t => t.Products)
- .FirstOrDefault(t => t.Id == transactionId);
+ var query = session.CreateQuery(@"
+ SELECT t
+ FROM Transaction t
+ LEFT JOIN FETCH t.TransactionProducts tp
+ LEFT JOIN FETCH tp.Product
+ WHERE t.Id = :transactionId
+ ");
+ query.SetParameter("transactionId", transactionId);
+ var transaction = query.UniqueResult();
return transaction;
}
}
+
public void UpdateTransaction(Transaction transaction)
{
using (var session = SessionFactory.OpenSession())
@@ -65,7 +88,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
{
try
{
- var transaction = session.Get(transactionId);
+ var transaction = session.Get(transactionId);
if (transaction != null)
{
session.Delete(transaction);
@@ -79,17 +102,46 @@ namespace FirmTracker_Server.nHibernate.Transactions
}
}
}
+ public void AddTransactionProductToTransaction(int transactionId, TransactionProduct transactionProduct)
+ {
+ using (var session = SessionFactory.OpenSession())
+ using (var transaction = session.BeginTransaction())
+ {
+ try
+ {
+ var transactionToUpdate = session.Get(transactionId);
+ if (transactionToUpdate != null)
+ {
+ transactionProduct.TransactionId = transactionToUpdate.Id;
+ session.Save(transactionProduct);
+ transaction.Commit();
+ }
+ else
+ {
+ throw new Exception("Transaction not found.");
+ }
+ }
+ catch (Exception ex)
+ {
+ transaction.Rollback();
+ throw ex;
+ }
+ }
+ }
+
public IList GetAllTransactions()
{
using (var session = SessionFactory.OpenSession())
{
var transactions = session.Query()
- .FetchMany(t => t.Products)
+ .FetchMany(t => t.TransactionProducts)
+ .ThenFetch(tp => tp.Product)
.ToList();
+
return transactions;
}
}
- }
+ }
}
diff --git a/nHibernate/Transactions/TransactionMapping.cs b/nHibernate/Transactions/TransactionMapping.cs
index 1c26c10..3a24b5b 100644
--- a/nHibernate/Transactions/TransactionMapping.cs
+++ b/nHibernate/Transactions/TransactionMapping.cs
@@ -2,23 +2,22 @@
namespace FirmTracker_Server.nHibernate.Transactions
{
- public class TransactionMapping:ClassMap
+ public class TransactionMapping : ClassMap
{
public TransactionMapping()
{
Table("Transactions");
- Id(x => x.Id).GeneratedBy.Identity();
+ Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Date);
Map(x => x.EmployeeId);
Map(x => x.PaymentType);
Map(x => x.Discount);
Map(x => x.Description);
- HasManyToMany(x => x.Products)
- .Table("TransactionProducts")
- .ParentKeyColumn("TransactionId")
- .ChildKeyColumn("ProductId")
- .Cascade.All();
+ HasMany(x => x.TransactionProducts)
+ .KeyColumn("TransactionId")
+ .Cascade.AllDeleteOrphan()
+ .Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji
}
}
}
diff --git a/nHibernate/Transactions/TransactionProduct.cs b/nHibernate/Transactions/TransactionProduct.cs
new file mode 100644
index 0000000..1eca3c7
--- /dev/null
+++ b/nHibernate/Transactions/TransactionProduct.cs
@@ -0,0 +1,17 @@
+using FirmTracker_Server.nHibernate.Products;
+using Newtonsoft.Json;
+using NSwag.Annotations;
+
+namespace FirmTracker_Server.nHibernate.Transactions
+{
+ public class TransactionProduct
+ {
+ public virtual int Id { get; set; }
+ [JsonIgnore]
+ [SwaggerIgnore]
+ public virtual int TransactionId { get; set; }
+ public virtual Products.Product Product { get; set; }
+ public virtual int Quantity { get; set; }
+ public virtual decimal UnitPrice { get; set; }
+ }
+}
diff --git a/nHibernate/Transactions/TransactionProductMapping.cs b/nHibernate/Transactions/TransactionProductMapping.cs
new file mode 100644
index 0000000..4f0ff0d
--- /dev/null
+++ b/nHibernate/Transactions/TransactionProductMapping.cs
@@ -0,0 +1,19 @@
+using FluentNHibernate.Mapping;
+
+namespace FirmTracker_Server.nHibernate.Transactions
+{
+ public class TransactionProductMapping : ClassMap
+ {
+ public TransactionProductMapping()
+ {
+ Table("TransactionProducts");
+ Id(x => x.Id).GeneratedBy.Identity();
+
+ Map(x => x.TransactionId).Column("TransactionId").Not.Nullable();
+ References(x => x.Product).Column("ProductId").Not.Nullable();
+
+ Map(x => x.Quantity);
+ Map(x => x.UnitPrice);
+ }
+ }
+}