Compare commits
9 Commits
b45dedab71
...
c4f62ec62b
Author | SHA1 | Date | |
---|---|---|---|
c4f62ec62b | |||
2c4927aa1d | |||
f09289c101 | |||
7b54a5d796 | |||
8f3f1e5b1e | |||
|
4e023be7ca | ||
|
31f920a048 | ||
|
fc8ac5d51d | ||
|
c94363b790 |
12
.config/dotnet-tools.json
Normal file
12
.config/dotnet-tools.json
Normal file
@ -0,0 +1,12 @@
|
||||
{
|
||||
"version": 1,
|
||||
"isRoot": true,
|
||||
"tools": {
|
||||
"dotnet-ef": {
|
||||
"version": "8.0.4",
|
||||
"commands": [
|
||||
"dotnet-ef"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,5 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using FirmTracker_Server;
|
||||
using System.Collections.Generic;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
namespace FirmTracker_Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
@ -16,7 +14,12 @@ namespace FirmTracker_Server.Controllers
|
||||
}
|
||||
|
||||
// POST: api/Products
|
||||
/// <summary>
|
||||
/// Creates a new product.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(200)] // Created
|
||||
[ProducesResponseType(400)] // Bad Request
|
||||
public IActionResult CreateProduct([FromBody] Product product)
|
||||
{
|
||||
try
|
||||
@ -32,6 +35,8 @@ namespace FirmTracker_Server.Controllers
|
||||
|
||||
// GET: api/Products/5
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(200)] // Created
|
||||
[ProducesResponseType(400)] // Bad Request
|
||||
public IActionResult GetProduct(int id)
|
||||
{
|
||||
var product = _productCrud.GetProduct(id);
|
||||
@ -42,6 +47,8 @@ namespace FirmTracker_Server.Controllers
|
||||
|
||||
// PUT: api/Products/5
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(200)] // Created
|
||||
[ProducesResponseType(400)] // Bad Request
|
||||
public IActionResult UpdateProduct(int id, [FromBody] Product product)
|
||||
{
|
||||
if (id != product.Id)
|
||||
@ -60,6 +67,8 @@ namespace FirmTracker_Server.Controllers
|
||||
|
||||
// DELETE: api/Products/5
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(200)] // Created
|
||||
[ProducesResponseType(400)] // Bad Request
|
||||
public IActionResult DeleteProduct(int id)
|
||||
{
|
||||
try
|
||||
@ -75,10 +84,44 @@ namespace FirmTracker_Server.Controllers
|
||||
|
||||
// GET: api/Products
|
||||
[HttpGet]
|
||||
[ProducesResponseType(200)] // Created
|
||||
[ProducesResponseType(400)] // Bad Request
|
||||
public IActionResult GetAllProducts()
|
||||
{
|
||||
var products = _productCrud.GetAllProducts();
|
||||
return Ok(products);
|
||||
}
|
||||
|
||||
[HttpPost("CalculateTotalPrice")]
|
||||
[ProducesResponseType(200)]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult CalculateTotalPrice([FromBody] ProductOrder[] orders)
|
||||
{
|
||||
decimal totalPrice = 0;
|
||||
decimal discount = 0;
|
||||
foreach (var order in orders)
|
||||
{
|
||||
discount = order.Discount;
|
||||
var product = _productCrud.GetProduct(order.ProductId);
|
||||
if (product == null)
|
||||
{
|
||||
return BadRequest($"Product with ID {order.ProductId} not found.");
|
||||
}
|
||||
totalPrice += product.Price * order.Quantity;
|
||||
}
|
||||
|
||||
// Apply discount
|
||||
decimal discountAmount = totalPrice * (discount / 100);
|
||||
totalPrice -= discountAmount;
|
||||
|
||||
return Ok(new { TotalPrice = totalPrice });
|
||||
}
|
||||
|
||||
public class ProductOrder
|
||||
{
|
||||
public int ProductId { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
public decimal Discount { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
160
Controllers/TransactionController.cs
Normal file
160
Controllers/TransactionController.cs
Normal file
@ -0,0 +1,160 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using FirmTracker_Server.nHibernate.Transactions;
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Text.Json;
|
||||
using System.Transactions;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
|
||||
namespace FirmTracker_Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class TransactionController : ControllerBase
|
||||
{
|
||||
private readonly TransactionCRUD _transactionCRUD;
|
||||
private readonly ProductCRUD _productCRUD;
|
||||
|
||||
public TransactionController()
|
||||
{
|
||||
_transactionCRUD = new TransactionCRUD();
|
||||
_productCRUD = new ProductCRUD();
|
||||
}
|
||||
|
||||
|
||||
// POST: api/Transaction
|
||||
/// <summary>
|
||||
/// Creates a new transaction.
|
||||
/// </summary>
|
||||
[HttpPost]
|
||||
[ProducesResponseType(StatusCodes.Status201Created)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public IActionResult CreateTransaction([FromBody] nHibernate.Transactions.Transaction transaction)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Before adding the transaction, ensure each product is linked properly
|
||||
foreach (var product in transaction.TransactionProducts)
|
||||
{
|
||||
product.TransactionId = transaction.Id; // This might be 0 at this point if transaction isn't saved yet
|
||||
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
||||
int type = _productCRUD.GetProductType(product.ProductID);
|
||||
if (type == 1)
|
||||
{
|
||||
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
|
||||
}
|
||||
else
|
||||
{
|
||||
transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
|
||||
}
|
||||
}
|
||||
|
||||
_transactionCRUD.AddTransaction(transaction);
|
||||
|
||||
// Now that the transaction is saved, update each product with the correct TransactionId
|
||||
foreach (var product in transaction.TransactionProducts)
|
||||
{
|
||||
product.TransactionId = transaction.Id; // Now transaction.Id is a valid ID after saving
|
||||
_transactionCRUD.UpdateTransactionProduct(product);
|
||||
}
|
||||
|
||||
|
||||
// session.Flush(); // Ensure changes are committed if managing session manually
|
||||
|
||||
return CreatedAtAction(nameof(GetTransaction), new { id = transaction.Id }, transaction);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// GET: api/Transaction/5
|
||||
[HttpGet("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public IActionResult GetTransaction(int id)
|
||||
{
|
||||
var transaction = _transactionCRUD.GetTransaction(id);
|
||||
if (transaction == null)
|
||||
return NotFound();
|
||||
return Ok(transaction);
|
||||
}
|
||||
|
||||
// PUT: api/Transaction/5
|
||||
[HttpPut("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
||||
public IActionResult UpdateTransaction(int id, [FromBody] nHibernate.Transactions.Transaction transaction)
|
||||
{
|
||||
if (id != transaction.Id)
|
||||
return BadRequest("Transaction ID mismatch");
|
||||
|
||||
try
|
||||
{
|
||||
// Before adding the transaction, ensure each product is linked properly
|
||||
foreach (var product in transaction.TransactionProducts)
|
||||
{
|
||||
product.TransactionId = transaction.Id; // This might be 0 at this point if transaction isn't saved yet
|
||||
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
||||
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
|
||||
}
|
||||
_transactionCRUD.UpdateTransaction(transaction);
|
||||
|
||||
// Now that the transaction is saved, update each product with the correct TransactionId
|
||||
foreach (var product in transaction.TransactionProducts)
|
||||
{
|
||||
product.TransactionId = transaction.Id; // Now transaction.Id is a valid ID after saving
|
||||
_transactionCRUD.UpdateTransactionProduct(product);
|
||||
}
|
||||
return NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: api/Transaction/5
|
||||
[HttpDelete("{id}")]
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public IActionResult DeleteTransaction(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_transactionCRUD.DeleteTransaction(id);
|
||||
return NoContent();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// GET: api/Transaction
|
||||
[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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -5,15 +5,52 @@
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>FirmTracker_Server</RootNamespace>
|
||||
<UserSecretsId>08986e21-848b-485a-a219-03e2dc6041e4</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Remove="nHIbernate\Transactions\TransactionCRUD2.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Properties\launchSettings.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FluentNHibernate" Version="3.3.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.18" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.12" />
|
||||
<PackageReference Include="NHibernate" Version="5.5.1" />
|
||||
<PackageReference Include="NSwag.Annotations" Version="14.0.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="Properties\Resources.Designer.cs">
|
||||
<DesignTime>True</DesignTime>
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<EmbeddedResource Update="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
</EmbeddedResource>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="Properties\launchSettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
66
Program.cs
66
Program.cs
@ -4,43 +4,91 @@ using NHibernate.Dialect;
|
||||
using NHibernate.Driver;
|
||||
using FirmTracker_Server.Controllers;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using FirmTracker_Server.nHibernate;
|
||||
|
||||
namespace FirmTracker_Server
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
string appDirectory = Directory.GetCurrentDirectory();
|
||||
string configFilePath = Path.Combine(appDirectory, "appsettings.json");
|
||||
string connectionString = "";
|
||||
if (File.Exists(configFilePath))
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddJsonFile(configFilePath)
|
||||
.Build();
|
||||
|
||||
var connectionstringsection = config.GetSection("AppSettings:ConnectionString");
|
||||
|
||||
connectionString = connectionstringsection.Value;
|
||||
|
||||
SessionFactory.Init(connectionString);
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"The configuration file '{configFilePath}' was not found.");
|
||||
}
|
||||
|
||||
// Add services to the container.
|
||||
TestClass test = new TestClass();
|
||||
test.AddTestProduct();
|
||||
builder.Services.AddCors(options =>
|
||||
{
|
||||
options.AddPolicy("AllowSpecificOrigin",
|
||||
policy => policy.WithOrigins("http://localhost:3000")
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyMethod());
|
||||
});
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
var configSwagger = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
||||
var port = configSwagger.GetValue<int>("Port", 5075);
|
||||
var port2 = configSwagger.GetValue<int>("Port", 7039);
|
||||
app.Urls.Add($"http://*:{port}");
|
||||
app.Urls.Add($"https://*:{port2}");
|
||||
|
||||
try
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint($"/swagger/v1/swagger.json", "FirmTracker - TEST");
|
||||
c.RoutePrefix = "swagger";
|
||||
});
|
||||
Console.WriteLine("uruchomiono swaggera");
|
||||
app.UseHttpsRedirection();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Nie uda³o siê uruchomiæ swaggera");
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseCors("AllowSpecificOrigin");
|
||||
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
var configuration = new Configuration();
|
||||
|
||||
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
Properties/Resources.Designer.cs
generated
Normal file
63
Properties/Resources.Designer.cs
generated
Normal file
@ -0,0 +1,63 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// This code was generated by a tool.
|
||||
// Runtime Version:4.0.30319.42000
|
||||
//
|
||||
// Changes to this file may cause incorrect behavior and will be lost if
|
||||
// the code is regenerated.
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace FirmTracker_Server.Properties {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// A strongly-typed resource class, for looking up localized strings, etc.
|
||||
/// </summary>
|
||||
// This class was auto-generated by the StronglyTypedResourceBuilder
|
||||
// class via a tool like ResGen or Visual Studio.
|
||||
// To add or remove a member, edit your .ResX file then rerun ResGen
|
||||
// with the /str option, or rebuild your VS project.
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the cached ResourceManager instance used by this class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("FirmTracker_Server.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overrides the current thread's CurrentUICulture property for all
|
||||
/// resource lookups using this strongly typed resource class.
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
101
Properties/Resources.resx
Normal file
101
Properties/Resources.resx
Normal file
@ -0,0 +1,101 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 1.3
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">1.3</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1">this is my long string</data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
[base64 mime encoded serialized .NET Framework object]
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
[base64 mime encoded string representing a byte array form of the .NET Framework object]
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>1.3</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
@ -1,4 +1,34 @@
|
||||
{
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Production"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5045"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Production"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7039;http://localhost:5045"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Production"
|
||||
}
|
||||
}
|
||||
},
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
@ -7,35 +37,5 @@
|
||||
"applicationUrl": "http://localhost:17940",
|
||||
"sslPort": 44326
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5045",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7039;http://localhost:5045",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
131
TestClass.cs
131
TestClass.cs
@ -1,5 +1,8 @@
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using FirmTracker_Server.Controllers;
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using FirmTracker_Server.nHibernate.Transactions;
|
||||
using FirmTracker_Server.nHibernate.Expenses;
|
||||
using NHibernate;
|
||||
|
||||
namespace FirmTracker_Server
|
||||
@ -8,24 +11,134 @@ namespace FirmTracker_Server
|
||||
{
|
||||
public void AddTestProduct()
|
||||
{
|
||||
SessionFactory.Init("Server=localhost;Database=FirmTrackerDB;User Id=sa;Password=Rap45tro2;");
|
||||
// SessionFactory.Init(ConnectionString);
|
||||
|
||||
var product = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Test Product2",
|
||||
Description = "This is a test product",
|
||||
Price = 11.99m,
|
||||
Type = 0, // Goods
|
||||
Availability = true
|
||||
Name = "Produkt 1",
|
||||
Description = "testowy produkt",
|
||||
Price = 11.50m,
|
||||
Type = 1,
|
||||
Availability = 5
|
||||
};
|
||||
var product2 = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Usluga 1",
|
||||
Description = "testowa usluga",
|
||||
Price = 1120.00m,
|
||||
Type = 0,
|
||||
Availability = 0
|
||||
};
|
||||
var product3 = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Produkt 2",
|
||||
Description = "produkt",
|
||||
Price = 16.50m,
|
||||
Type = 1,
|
||||
Availability = 20
|
||||
};
|
||||
var product4 = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Produkt 3",
|
||||
Description = "produkt",
|
||||
Price = 25.00m,
|
||||
Type = 1,
|
||||
Availability = 10
|
||||
};
|
||||
var product5 = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Usługa 2",
|
||||
Description = "usługa",
|
||||
Price = 700.00m,
|
||||
Type = 0,
|
||||
Availability = 0
|
||||
};
|
||||
var transaction1 = new Transaction
|
||||
{
|
||||
Date = DateTime.Now.AddDays(-2),
|
||||
Description = "testowa transakcja",
|
||||
Discount = 10,
|
||||
EmployeeId = 1,
|
||||
PaymentType = "Karta kredytowa",
|
||||
};
|
||||
var transaction2 = new Transaction
|
||||
{
|
||||
Date = DateTime.Now.AddDays(-3),
|
||||
Description = "testowa transakcja",
|
||||
Discount = 30,
|
||||
EmployeeId = 2,
|
||||
PaymentType = "Gotówka",
|
||||
};
|
||||
var transaction3 = new Transaction
|
||||
{
|
||||
Date = DateTime.Now,
|
||||
Description = "testowa transakcja",
|
||||
Discount = 15,
|
||||
EmployeeId = 1,
|
||||
PaymentType = "BLIK",
|
||||
};
|
||||
|
||||
var expense1 = new Expense
|
||||
{
|
||||
Date = DateTime.Now,
|
||||
Value = 1003.9m,
|
||||
Description = "testowy rozchód"
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
|
||||
crud.AddProduct(product);
|
||||
FirmTracker_Server.nHibernate.Products.ProductCRUD productCrud = new ProductCRUD();
|
||||
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
|
||||
ExpenseCRUD expenseCrud = new ExpenseCRUD();
|
||||
productCrud.AddProduct(product);
|
||||
productCrud.AddProduct(product2);
|
||||
productCrud.AddProduct(product3);
|
||||
productCrud.AddProduct(product4);
|
||||
productCrud.AddProduct(product5);
|
||||
transactionCrud.AddTransaction(transaction1);
|
||||
transactionCrud.AddTransaction(transaction2);
|
||||
transactionCrud.AddTransaction(transaction3);
|
||||
expenseCrud.AddExpense(expense1);
|
||||
|
||||
|
||||
List<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
|
||||
new TransactionProduct { ProductID = 1, Quantity = 2 },
|
||||
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 = 4, Quantity=5},
|
||||
new TransactionProduct { ProductID = 5, Quantity=1}
|
||||
};
|
||||
foreach (var transactionProduct in testTransactionProducts2)
|
||||
{
|
||||
transactionCrud.AddTransactionProductToTransaction(transaction2.Id, transactionProduct);
|
||||
|
||||
}
|
||||
|
||||
List<TransactionProduct> testTransactionProducts3 = new List<TransactionProduct>
|
||||
{
|
||||
new TransactionProduct { ProductID = 3, Quantity=12},
|
||||
new TransactionProduct { ProductID = 2, Quantity=1}
|
||||
};
|
||||
foreach (var transactionProduct in testTransactionProducts3)
|
||||
{
|
||||
transactionCrud.AddTransactionProductToTransaction(transaction3.Id, transactionProduct);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,29 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
"AppSettings": {
|
||||
"ConnectionString": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=master;Integrated Security=True;"
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5045"
|
||||
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7039"
|
||||
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger"
|
||||
|
||||
}
|
||||
}
|
||||
}
|
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,6 @@
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
@ -7,6 +9,6 @@
|
||||
public virtual string Description { get; set; }
|
||||
public virtual decimal Price { get; set; }
|
||||
public virtual int Type { get; set; } // 0 for service, 1 for goods
|
||||
public virtual bool Availability { get; set; }
|
||||
public virtual int Availability { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ using NHibernate;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.OpenApi;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
{
|
||||
@ -25,8 +26,33 @@ namespace FirmTracker_Server.nHibernate.Products
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public decimal GetProductPrice(int productId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var product = session.Query<Product>()
|
||||
.Where(p => p.Id == productId)
|
||||
.Select(p => p.Price)
|
||||
.FirstOrDefault();
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
||||
public int GetProductType(int productId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var product = session.Query<Product>()
|
||||
.Where(p => p.Id == productId)
|
||||
.Select(p => p.Type)
|
||||
.FirstOrDefault();
|
||||
|
||||
return product;
|
||||
}
|
||||
}
|
||||
public Product GetProduct(int productId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
|
@ -25,7 +25,16 @@ namespace FirmTracker_Server.nHibernate
|
||||
.Database(MsSqlConfiguration.MsSql2012
|
||||
.ConnectionString(c => c.Is(connectionString))
|
||||
.ShowSql())
|
||||
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Products.ProductMapping>())
|
||||
.Mappings(m =>
|
||||
{
|
||||
m.FluentMappings
|
||||
.AddFromAssemblyOf<Products.ProductMapping>()
|
||||
.AddFromAssemblyOf<Transactions.TransactionMapping>()
|
||||
.AddFromAssemblyOf<Transactions.TransactionProductMapping>()
|
||||
.AddFromAssemblyOf<Transactions.Transaction2Mapping>()
|
||||
.AddFromAssemblyOf<Transactions.TransactionWithProductsMapping>()
|
||||
.AddFromAssemblyOf<Expenses.ExpenseMapping>();
|
||||
})
|
||||
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
|
||||
.BuildSessionFactory();
|
||||
}
|
||||
|
24
nHibernate/Transactions/Transaction.cs
Normal file
24
nHibernate/Transactions/Transaction.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
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 int EmployeeId { get; set; }
|
||||
public virtual IList<TransactionProduct> TransactionProducts { get; set; } = new List<TransactionProduct>();
|
||||
public virtual string PaymentType { get; set; }
|
||||
public virtual decimal Discount { get; set; }
|
||||
public virtual string Description { get; set; }
|
||||
public virtual decimal TotalPrice { get; set; }//=> TransactionProducts.Sum(tp => ((tp.Quantity * tp.UnitPrice)* ((1 - (Discount / 100)))));// (1 - (Discount / 100)));
|
||||
|
||||
public Transaction()
|
||||
{
|
||||
TransactionProducts = new List<TransactionProduct>();
|
||||
}
|
||||
}
|
||||
}
|
24
nHibernate/Transactions/Transaction2.cs
Normal file
24
nHibernate/Transactions/Transaction2.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class Transaction2
|
||||
{
|
||||
public virtual int Id { get; set; }
|
||||
public virtual DateTime Date { get; set; }
|
||||
public virtual int EmployeeId { get; set; }
|
||||
public virtual IList<TransactionWithProducts> TransactionProducts { get; set; } = new List<TransactionWithProducts>();
|
||||
public virtual string PaymentType { get; set; }
|
||||
public virtual decimal Discount { get; set; }
|
||||
public virtual string Description { get; set; }
|
||||
public virtual decimal TotalPrice { get; set; }//=> TransactionProducts.Sum(tp => ((tp.Quantity * tp.UnitPrice)* ((1 - (Discount / 100)))));// (1 - (Discount / 100)));
|
||||
|
||||
public Transaction2()
|
||||
{
|
||||
TransactionProducts = new List<TransactionWithProducts>();
|
||||
}
|
||||
}
|
||||
}
|
23
nHibernate/Transactions/Transaction2Mapping.cs
Normal file
23
nHibernate/Transactions/Transaction2Mapping.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class Transaction2Mapping : ClassMap<Transaction2>
|
||||
{
|
||||
public Transaction2Mapping()
|
||||
{
|
||||
Table("Transactions");
|
||||
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);
|
||||
|
||||
HasMany(x => x.TransactionProducts)
|
||||
.KeyColumn("TransactionId")
|
||||
.Cascade.AllDeleteOrphan()
|
||||
.Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji
|
||||
}
|
||||
}
|
||||
}
|
197
nHibernate/Transactions/TransactionCRUD.cs
Normal file
197
nHibernate/Transactions/TransactionCRUD.cs
Normal file
@ -0,0 +1,197 @@
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using NHibernate;
|
||||
using NHibernate.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Transactions;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class TransactionCRUD
|
||||
{
|
||||
public void AddTransaction(Transaction transaction)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var sessionTransaction = session.BeginTransaction())
|
||||
{
|
||||
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<Product>(transactionProduct.ProductID);
|
||||
if (product.Type != 0)
|
||||
{
|
||||
product.Availability -= transactionProduct.Quantity;
|
||||
session.Update(product);
|
||||
}
|
||||
}
|
||||
|
||||
sessionTransaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
sessionTransaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//usage of HQL
|
||||
public Transaction2 GetTransaction(int transactionId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var query = session.CreateQuery(@"
|
||||
SELECT t
|
||||
FROM Transaction2 t
|
||||
LEFT JOIN FETCH t.TransactionProducts tp
|
||||
LEFT JOIN FETCH tp.Product
|
||||
WHERE t.Id = :transactionId
|
||||
");
|
||||
query.SetParameter("transactionId", transactionId);
|
||||
|
||||
var transaction = query.UniqueResult<Transaction2>();
|
||||
return transaction;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void UpdateTransaction(Transaction transaction)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var sessionTransaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var transactionProduct in transaction.TransactionProducts)
|
||||
{
|
||||
|
||||
transactionProduct.TransactionId = transaction.Id;
|
||||
session.Update(transactionProduct);
|
||||
}
|
||||
session.Update(transaction);
|
||||
|
||||
// Decrease product quantities based on transaction
|
||||
foreach (var transactionProduct in transaction.TransactionProducts)
|
||||
{
|
||||
|
||||
var product = session.Get<Product>(transactionProduct.ProductID);
|
||||
if (product.Type != 0)
|
||||
{
|
||||
product.Availability -= transactionProduct.Quantity;
|
||||
session.Update(product);
|
||||
}
|
||||
}
|
||||
|
||||
sessionTransaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
sessionTransaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void UpdateTransactionProduct(TransactionProduct transactionProduct)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var t = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
session.Update(transactionProduct);
|
||||
t.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
t.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteTransaction(int transactionId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var t = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var transaction = session.Get<Transaction>(transactionId);
|
||||
if (transaction != null)
|
||||
{
|
||||
session.Delete(transaction);
|
||||
t.Commit();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
t.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
public void AddTransactionProductToTransaction(int transactionId, TransactionProduct transactionProduct)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var transactionToUpdate = session.Get<Transaction>(transactionId);
|
||||
if (transactionToUpdate != null)
|
||||
{
|
||||
transactionProduct.TransactionId= transactionToUpdate.Id;
|
||||
session.Save(transactionProduct);
|
||||
transaction.Commit();
|
||||
|
||||
var product = session.Get<Product>(transactionProduct.ProductID);
|
||||
if (product.Type != 0)
|
||||
{
|
||||
product.Availability -= transactionProduct.Quantity;
|
||||
session.Update(product);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Transaction not found.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public IList<Transaction2> GetAllTransactions()
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
var transactions = session.Query<Transaction2>()
|
||||
.FetchMany(t => t.TransactionProducts)
|
||||
.ThenFetch(tp => tp.Product)
|
||||
.ToList();
|
||||
|
||||
return transactions;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
23
nHibernate/Transactions/TransactionMapping.cs
Normal file
23
nHibernate/Transactions/TransactionMapping.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class TransactionMapping : ClassMap<Transaction>
|
||||
{
|
||||
public TransactionMapping()
|
||||
{
|
||||
Table("Transactions");
|
||||
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);
|
||||
|
||||
HasMany(x => x.TransactionProducts)
|
||||
.KeyColumn("TransactionId")
|
||||
.Cascade.AllDeleteOrphan()
|
||||
.Inverse(); // Ustawienie Inverse() wskazuje, że to `TransactionProduct` jest właścicielem relacji
|
||||
}
|
||||
}
|
||||
}
|
14
nHibernate/Transactions/TransactionProduct.cs
Normal file
14
nHibernate/Transactions/TransactionProduct.cs
Normal file
@ -0,0 +1,14 @@
|
||||
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; }
|
||||
public virtual int TransactionId { get; set; }
|
||||
public virtual int ProductID { get; set; }
|
||||
public virtual int Quantity { get; set; }
|
||||
}
|
||||
}
|
19
nHibernate/Transactions/TransactionProductMapping.cs
Normal file
19
nHibernate/Transactions/TransactionProductMapping.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class TransactionProductMapping : ClassMap<TransactionProduct>
|
||||
{
|
||||
public TransactionProductMapping()
|
||||
{
|
||||
Table("TransactionProducts");
|
||||
Id(x => x.Id).GeneratedBy.Identity();
|
||||
|
||||
Map(x => x.TransactionId).Column("TransactionId").Not.Nullable();
|
||||
Map(x => x.ProductID).Column("ProductId").Not.Nullable();
|
||||
|
||||
Map(x => x.Quantity);
|
||||
//Map(x => x.UnitPrice);
|
||||
}
|
||||
}
|
||||
}
|
16
nHibernate/Transactions/TransactionWithProducts.cs
Normal file
16
nHibernate/Transactions/TransactionWithProducts.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using Newtonsoft.Json;
|
||||
using NSwag.Annotations;
|
||||
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class TransactionWithProducts
|
||||
{
|
||||
public virtual int Id { get; set; }
|
||||
public virtual int TransactionId { get; set; }
|
||||
public virtual Products.Product Product { get; set; }
|
||||
public virtual int Quantity { get; set; }
|
||||
|
||||
}
|
||||
}
|
19
nHibernate/Transactions/TransactionWithProductsMapping.cs
Normal file
19
nHibernate/Transactions/TransactionWithProductsMapping.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Transactions
|
||||
{
|
||||
public class TransactionWithProductsMapping : ClassMap<TransactionWithProducts>
|
||||
{
|
||||
public TransactionWithProductsMapping()
|
||||
{
|
||||
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);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user