diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs new file mode 100644 index 0000000..91056fc --- /dev/null +++ b/Controllers/ProductController.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj new file mode 100644 index 0000000..727e11d --- /dev/null +++ b/FirmTracker-Server.csproj @@ -0,0 +1,19 @@ + + + + net7.0 + enable + enable + FirmTracker_Server + + + + + + + + + + + + diff --git a/FirmTracker-Server.sln b/FirmTracker-Server.sln new file mode 100644 index 0000000..53db7ba --- /dev/null +++ b/FirmTracker-Server.sln @@ -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 diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..9986c4b --- /dev/null +++ b/Program.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..ae02a6f --- /dev/null +++ b/Properties/launchSettings.json @@ -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" + } + } + } +} diff --git a/TestClass.cs b/TestClass.cs new file mode 100644 index 0000000..e428bcb --- /dev/null +++ b/TestClass.cs @@ -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; + } + } + } +} diff --git a/appsettings.Development.json b/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/nHIbernate/Products/Product.cs b/nHIbernate/Products/Product.cs new file mode 100644 index 0000000..2faefa4 --- /dev/null +++ b/nHIbernate/Products/Product.cs @@ -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; } + } +} diff --git a/nHIbernate/Products/ProductCRUD.cs b/nHIbernate/Products/ProductCRUD.cs new file mode 100644 index 0000000..5ec3628 --- /dev/null +++ b/nHIbernate/Products/ProductCRUD.cs @@ -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(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(productId); + if (product != null) + { + session.Delete(product); + transaction.Commit(); + } + } + catch + { + transaction.Rollback(); + throw; + } + } + } + + public IList GetAllProducts() + { + using (var session = SessionFactory.OpenSession()) + { + return session.Query().ToList(); + } + } + } + +} diff --git a/nHIbernate/Products/ProductMapping.cs b/nHIbernate/Products/ProductMapping.cs new file mode 100644 index 0000000..4ebdd14 --- /dev/null +++ b/nHIbernate/Products/ProductMapping.cs @@ -0,0 +1,17 @@ +using FluentNHibernate.Mapping; +namespace FirmTracker_Server.nHibernate.Products +{ + public class ProductMapping : ClassMap + { + 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); + } + } +} diff --git a/nHIbernate/SessionFactory.cs b/nHIbernate/SessionFactory.cs new file mode 100644 index 0000000..9ab3368 --- /dev/null +++ b/nHIbernate/SessionFactory.cs @@ -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()) + .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true)) //SchemaUpdate . Execute dla only update + .BuildSessionFactory(); + } + } +}