Add project files.
This commit is contained in:
parent
0e6dd91292
commit
b45dedab71
84
Controllers/ProductController.cs
Normal file
84
Controllers/ProductController.cs
Normal file
@ -0,0 +1,84 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using FirmTracker_Server;
|
||||
using System.Collections.Generic;
|
||||
namespace FirmTracker_Server.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ProductsController : ControllerBase
|
||||
{
|
||||
private readonly ProductCRUD _productCrud;
|
||||
|
||||
public ProductsController()
|
||||
{
|
||||
_productCrud = new ProductCRUD();
|
||||
}
|
||||
|
||||
// POST: api/Products
|
||||
[HttpPost]
|
||||
public IActionResult CreateProduct([FromBody] Product product)
|
||||
{
|
||||
try
|
||||
{
|
||||
_productCrud.AddProduct(product);
|
||||
return CreatedAtAction("GetProduct", new { id = product.Id }, product);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// GET: api/Products/5
|
||||
[HttpGet("{id}")]
|
||||
public IActionResult GetProduct(int id)
|
||||
{
|
||||
var product = _productCrud.GetProduct(id);
|
||||
if (product == null)
|
||||
return NotFound();
|
||||
return Ok(product);
|
||||
}
|
||||
|
||||
// PUT: api/Products/5
|
||||
[HttpPut("{id}")]
|
||||
public IActionResult UpdateProduct(int id, [FromBody] Product product)
|
||||
{
|
||||
if (id != product.Id)
|
||||
return BadRequest("Product ID mismatch");
|
||||
|
||||
try
|
||||
{
|
||||
_productCrud.UpdateProduct(product);
|
||||
return NoContent();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// DELETE: api/Products/5
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult DeleteProduct(int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
_productCrud.DeleteProduct(id);
|
||||
return NoContent();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
return NotFound(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
// GET: api/Products
|
||||
[HttpGet]
|
||||
public IActionResult GetAllProducts()
|
||||
{
|
||||
var products = _productCrud.GetAllProducts();
|
||||
return Ok(products);
|
||||
}
|
||||
}
|
||||
}
|
19
FirmTracker-Server.csproj
Normal file
19
FirmTracker-Server.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>FirmTracker_Server</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<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="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
25
FirmTracker-Server.sln
Normal file
25
FirmTracker-Server.sln
Normal file
@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.7.34003.232
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FirmTracker-Server", "FirmTracker-Server.csproj", "{F0930A50-F0FF-46EE-A9A8-98BAF95E10FA}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{F0930A50-F0FF-46EE-A9A8-98BAF95E10FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{F0930A50-F0FF-46EE-A9A8-98BAF95E10FA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{F0930A50-F0FF-46EE-A9A8-98BAF95E10FA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{F0930A50-F0FF-46EE-A9A8-98BAF95E10FA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {830059B4-894A-43FF-A716-D92E556AF612}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
46
Program.cs
Normal file
46
Program.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using NHibernate;
|
||||
using NHibernate.Cfg;
|
||||
using NHibernate.Dialect;
|
||||
using NHibernate.Driver;
|
||||
using FirmTracker_Server.Controllers;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
|
||||
namespace FirmTracker_Server
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
TestClass test = new TestClass();
|
||||
test.AddTestProduct();
|
||||
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();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
var configuration = new Configuration();
|
||||
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
41
Properties/launchSettings.json
Normal file
41
Properties/launchSettings.json
Normal file
@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
33
TestClass.cs
Normal file
33
TestClass.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using NHibernate;
|
||||
|
||||
namespace FirmTracker_Server
|
||||
{
|
||||
public class TestClass
|
||||
{
|
||||
public void AddTestProduct()
|
||||
{
|
||||
SessionFactory.Init("Server=localhost;Database=FirmTrackerDB;User Id=sa;Password=Rap45tro2;");
|
||||
|
||||
var product = new nHibernate.Products.Product
|
||||
{
|
||||
Name = "Test Product2",
|
||||
Description = "This is a test product",
|
||||
Price = 11.99m,
|
||||
Type = 0, // Goods
|
||||
Availability = true
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
|
||||
crud.AddProduct(product);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
8
appsettings.Development.json
Normal file
8
appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
9
appsettings.json
Normal file
9
appsettings.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
12
nHIbernate/Products/Product.cs
Normal file
12
nHIbernate/Products/Product.cs
Normal file
@ -0,0 +1,12 @@
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
public virtual int Id { get; set; }
|
||||
public virtual string Name { get; set; }
|
||||
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; }
|
||||
}
|
||||
}
|
87
nHIbernate/Products/ProductCRUD.cs
Normal file
87
nHIbernate/Products/ProductCRUD.cs
Normal file
@ -0,0 +1,87 @@
|
||||
using FirmTracker_Server.nHibernate;
|
||||
using FirmTracker_Server.nHibernate.Products;
|
||||
using NHibernate;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.OpenApi;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
{
|
||||
public class ProductCRUD
|
||||
{
|
||||
public void AddProduct(Product product)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
session.Save(product);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Product GetProduct(int productId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
return session.Get<Product>(productId);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateProduct(Product product)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
session.Update(product);
|
||||
transaction.Commit();
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DeleteProduct(int productId)
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
using (var transaction = session.BeginTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
var product = session.Get<Product>(productId);
|
||||
if (product != null)
|
||||
{
|
||||
session.Delete(product);
|
||||
transaction.Commit();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IList<Product> GetAllProducts()
|
||||
{
|
||||
using (var session = SessionFactory.OpenSession())
|
||||
{
|
||||
return session.Query<Product>().ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
17
nHIbernate/Products/ProductMapping.cs
Normal file
17
nHIbernate/Products/ProductMapping.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using FluentNHibernate.Mapping;
|
||||
namespace FirmTracker_Server.nHibernate.Products
|
||||
{
|
||||
public class ProductMapping : ClassMap<Product>
|
||||
{
|
||||
public ProductMapping()
|
||||
{
|
||||
Table("Products");
|
||||
Id(x => x.Id).GeneratedBy.Identity();
|
||||
Map(x => x.Name);
|
||||
Map(x => x.Description);
|
||||
Map(x => x.Price);
|
||||
Map(x => x.Type);
|
||||
Map(x => x.Availability);
|
||||
}
|
||||
}
|
||||
}
|
33
nHIbernate/SessionFactory.cs
Normal file
33
nHIbernate/SessionFactory.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using FluentNHibernate.Cfg;
|
||||
using FluentNHibernate.Cfg.Db;
|
||||
using NHibernate;
|
||||
using NHibernate.Tool.hbm2ddl;
|
||||
|
||||
namespace FirmTracker_Server.nHibernate
|
||||
{
|
||||
public static class SessionFactory
|
||||
{
|
||||
private static ISessionFactory _factory;
|
||||
|
||||
public static NHibernate.ISession OpenSession()
|
||||
{
|
||||
return _factory.OpenSession();
|
||||
}
|
||||
|
||||
public static void Init(string connectionString)
|
||||
{
|
||||
_factory = BuildSessionFactory(connectionString);
|
||||
}
|
||||
|
||||
private static ISessionFactory BuildSessionFactory(string connectionString)
|
||||
{
|
||||
return Fluently.Configure()
|
||||
.Database(MsSqlConfiguration.MsSql2012
|
||||
.ConnectionString(c => c.Is(connectionString))
|
||||
.ShowSql())
|
||||
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Products.ProductMapping>())
|
||||
.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update
|
||||
.BuildSessionFactory();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user