From c94363b7900a9e8cbc55a9680eecf10550fe2e48 Mon Sep 17 00:00:00 2001 From: Maciej Maciejewski Date: Fri, 26 Apr 2024 21:24:17 +0200 Subject: [PATCH] =?UTF-8?q?zmiany=20do=20test=C3=B3w=20rest=20api=20-=20sw?= =?UTF-8?q?agger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config/dotnet-tools.json | 12 ++++ Controllers/ProductController.cs | 13 ++++ FirmTracker-Server.csproj | 32 ++++++++++ Program.cs | 58 +++++++++++++++--- Properties/Resources.Designer.cs | 63 +++++++++++++++++++ Properties/Resources.resx | 101 +++++++++++++++++++++++++++++++ Properties/launchSettings.json | 64 ++++++++++---------- TestClass.cs | 21 +++++-- appsettings.json | 34 ++++++++--- nHIbernate/Products/Product.cs | 6 +- 10 files changed, 349 insertions(+), 55 deletions(-) create mode 100644 .config/dotnet-tools.json create mode 100644 Properties/Resources.Designer.cs create mode 100644 Properties/Resources.resx diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json new file mode 100644 index 0000000..677ed3f --- /dev/null +++ b/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.4", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/Controllers/ProductController.cs b/Controllers/ProductController.cs index 91056fc..5eda21b 100644 --- a/Controllers/ProductController.cs +++ b/Controllers/ProductController.cs @@ -16,7 +16,12 @@ namespace FirmTracker_Server.Controllers } // POST: api/Products + /// + /// Creates a new product. + /// [HttpPost] + [ProducesResponseType(200)] // Created + [ProducesResponseType(400)] // Bad Request public IActionResult CreateProduct([FromBody] Product product) { try @@ -32,6 +37,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 +49,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 +69,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,6 +86,8 @@ namespace FirmTracker_Server.Controllers // GET: api/Products [HttpGet] + [ProducesResponseType(200)] // Created + [ProducesResponseType(400)] // Bad Request public IActionResult GetAllProducts() { var products = _productCrud.GetAllProducts(); diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj index 727e11d..3616e11 100644 --- a/FirmTracker-Server.csproj +++ b/FirmTracker-Server.csproj @@ -5,8 +5,13 @@ enable enable FirmTracker_Server + 08986e21-848b-485a-a219-03e2dc6041e4 + + + + @@ -16,4 +21,31 @@ + + + True + True + Resources.resx + + + + + + PreserveNewest + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + + + PreserveNewest + + + diff --git a/Program.cs b/Program.cs index 9986c4b..bff4c04 100644 --- a/Program.cs +++ b/Program.cs @@ -4,33 +4,75 @@ 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(); + // Combine the application directory with the relative path to the config file + string configFilePath = Path.Combine(appDirectory, "appsettings.json"); + string connectionString = ""; + // Check if the config file exists + 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.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("Port", 5075); + var port2 = configSwagger.GetValue("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(); + Console.WriteLine("raz dwa trzy"); app.UseAuthorization(); @@ -38,9 +80,9 @@ namespace FirmTracker_Server app.MapControllers(); var configuration = new Configuration(); - + app.Run(); } } -} \ No newline at end of file +} diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs new file mode 100644 index 0000000..9a0ac44 --- /dev/null +++ b/Properties/Resources.Designer.cs @@ -0,0 +1,63 @@ +//------------------------------------------------------------------------------ +// +// 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. +// +//------------------------------------------------------------------------------ + +namespace FirmTracker_Server.Properties { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // 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() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [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; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + } +} diff --git a/Properties/Resources.resx b/Properties/Resources.resx new file mode 100644 index 0000000..4fdb1b6 --- /dev/null +++ b/Properties/Resources.resx @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 1.3 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index ae02a6f..c7b3353 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -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" - } - } } -} +} \ No newline at end of file diff --git a/TestClass.cs b/TestClass.cs index e428bcb..f7302d4 100644 --- a/TestClass.cs +++ b/TestClass.cs @@ -8,21 +8,30 @@ 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 }; try { FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD(); crud.AddProduct(product); + crud.AddProduct(product2); } catch(Exception ex) { diff --git a/appsettings.json b/appsettings.json index 10f68b8..57058bd 100644 --- a/appsettings.json +++ b/appsettings.json @@ -1,9 +1,29 @@ { - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } + "AppSettings": { + "ConnectionString": "Server=localhost;Database=FirmTrackerDB;User Id=sa;Password=Rap45tro2;", }, - "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" + + } + } +} \ No newline at end of file diff --git a/nHIbernate/Products/Product.cs b/nHIbernate/Products/Product.cs index 2faefa4..f0089b4 100644 --- a/nHIbernate/Products/Product.cs +++ b/nHIbernate/Products/Product.cs @@ -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; } } }