zmiany do testów rest api - swagger

This commit is contained in:
Maciej Maciejewski 2024-04-26 21:24:17 +02:00
parent b45dedab71
commit c94363b790
10 changed files with 349 additions and 55 deletions

12
.config/dotnet-tools.json Normal file
View File

@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.4",
"commands": [
"dotnet-ef"
]
}
}
}

View File

@ -16,7 +16,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 +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();

View File

@ -5,8 +5,13 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>FirmTracker_Server</RootNamespace>
<UserSecretsId>08986e21-848b-485a-a219-03e2dc6041e4</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Content Include="Properties\launchSettings.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="FluentNHibernate" Version="3.3.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.18" />
@ -16,4 +21,31 @@
<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>

View File

@ -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<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();
Console.WriteLine("raz dwa trzy");
app.UseAuthorization();
@ -38,9 +80,9 @@ namespace FirmTracker_Server
app.MapControllers();
var configuration = new Configuration();
app.Run();
}
}
}
}

63
Properties/Resources.Designer.cs generated Normal file
View 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
View 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>

View File

@ -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"
}
}
}
}
}

View File

@ -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)
{

View File

@ -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"
}
}
}

View File

@ -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; }
}
}