From 43f2e40b05edbafbec843afba8c88f3a01ad2848 Mon Sep 17 00:00:00 2001 From: Francesco Del Re Date: Mon, 28 Oct 2024 19:26:35 +0100 Subject: [PATCH] Add API Test project; Add DI support --- .../Controllers/ConnectorController.cs | 113 ++++++++++++++++++ src/SharpConnector.Api/Program.cs | 32 +++++ .../Properties/launchSettings.json | 41 +++++++ .../SharpConnector.Api.csproj | 18 +++ .../SharpConnector.Api.http | 6 + .../appsettings.Development.json | 8 ++ src/SharpConnector.Api/appsettings.json | 14 +++ .../SharpConnectorServiceExtensions.cs | 31 +++++ src/SharpConnector/SharpConnector.csproj | 2 +- src/SharpConnector/SharpConnector.sln | 14 ++- src/SharpConnector/SharpConnectorClient.cs | 5 + 11 files changed, 279 insertions(+), 5 deletions(-) create mode 100644 src/SharpConnector.Api/Controllers/ConnectorController.cs create mode 100644 src/SharpConnector.Api/Program.cs create mode 100644 src/SharpConnector.Api/Properties/launchSettings.json create mode 100644 src/SharpConnector.Api/SharpConnector.Api.csproj create mode 100644 src/SharpConnector.Api/SharpConnector.Api.http create mode 100644 src/SharpConnector.Api/appsettings.Development.json create mode 100644 src/SharpConnector.Api/appsettings.json create mode 100644 src/SharpConnector/Middleware/SharpConnectorServiceExtensions.cs diff --git a/src/SharpConnector.Api/Controllers/ConnectorController.cs b/src/SharpConnector.Api/Controllers/ConnectorController.cs new file mode 100644 index 0000000..ede9d74 --- /dev/null +++ b/src/SharpConnector.Api/Controllers/ConnectorController.cs @@ -0,0 +1,113 @@ +// (c) 2020 Francesco Del Re +// This code is licensed under MIT license (see LICENSE.txt for details) +using Microsoft.AspNetCore.Mvc; +using SharpConnector.Interfaces; + +namespace SharpConnector.Api.Controllers +{ + [ApiController] + [Route("[controller]")] + public class ConnectorController : ControllerBase + { + private readonly ISharpConnectorClient _connectorClient; + + private readonly ILogger _logger; + + public ConnectorController(ILogger logger, ISharpConnectorClient connectorClient) + { + _logger = logger; + _connectorClient = connectorClient; + } + + [HttpGet("{key}", Name = "Get")] + public ActionResult Get(string key) + { + var result = _connectorClient.Get(key); + return result != null ? Ok(result) : NotFound(); + } + + [HttpGet("async/{key}")] + public async Task> GetAsync(string key) + { + var result = await _connectorClient.GetAsync(key); + return result != null ? Ok(result) : NotFound(); + } + + [HttpGet("all")] + public ActionResult> GetAll() + { + var results = _connectorClient.GetAll(); + return Ok(results); + } + + [HttpPost("insert")] + public ActionResult Insert(string key, [FromBody] string value) + { + var success = _connectorClient.Insert(key, value); + return success ? Ok(success) : BadRequest("Insert failed."); + } + + [HttpPost("insert/with-expiration")] + public ActionResult Insert(string key, [FromBody] string value, TimeSpan expiration) + { + var success = _connectorClient.Insert(key, value, expiration); + return success ? Ok(success) : BadRequest("Insert with expiration failed."); + } + + [HttpPost("insert-async")] + public async Task> InsertAsync(string key, [FromBody] string value) + { + var success = await _connectorClient.InsertAsync(key, value); + return success ? Ok(success) : BadRequest("Async insert failed."); + } + + [HttpPost("insert-async/with-expiration")] + public async Task> InsertAsync(string key, [FromBody] string value, TimeSpan expiration) + { + var success = await _connectorClient.InsertAsync(key, value, expiration); + return success ? Ok(success) : BadRequest("Async insert with expiration failed."); + } + + [HttpPost("insert-many")] + public ActionResult InsertMany([FromBody] Dictionary values) + { + var success = _connectorClient.InsertMany(values); + return success ? Ok(success) : BadRequest("Insert many failed."); + } + + [HttpPost("insert-many/with-expiration")] + public ActionResult InsertMany([FromBody] Dictionary values, TimeSpan expiration) + { + var success = _connectorClient.InsertMany(values, expiration); + return success ? Ok(success) : BadRequest("Insert many with expiration failed."); + } + + [HttpDelete("{key}")] + public ActionResult Delete(string key) + { + var success = _connectorClient.Delete(key); + return success ? Ok(success) : NotFound("Delete failed."); + } + + [HttpDelete("async/{key}")] + public async Task> DeleteAsync(string key) + { + var success = await _connectorClient.DeleteAsync(key); + return success ? Ok(success) : NotFound("Async delete failed."); + } + + [HttpPut("{key}")] + public ActionResult Update(string key, [FromBody] string value) + { + var success = _connectorClient.Update(key, value); + return success ? Ok(success) : NotFound("Update failed."); + } + + [HttpPut("async/{key}")] + public async Task> UpdateAsync(string key, [FromBody] string value) + { + var success = await _connectorClient.UpdateAsync(key, value); + return success ? Ok(success) : NotFound("Async update failed."); + } + } +} diff --git a/src/SharpConnector.Api/Program.cs b/src/SharpConnector.Api/Program.cs new file mode 100644 index 0000000..2c00ead --- /dev/null +++ b/src/SharpConnector.Api/Program.cs @@ -0,0 +1,32 @@ +// (c) 2020 Francesco Del Re +// This code is licensed under MIT license (see LICENSE.txt for details) +using SharpConnector.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +// Register the SharpConnector services with string payload type. +builder.Services.AddSharpConnectorServices(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/src/SharpConnector.Api/Properties/launchSettings.json b/src/SharpConnector.Api/Properties/launchSettings.json new file mode 100644 index 0000000..8c0c20d --- /dev/null +++ b/src/SharpConnector.Api/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:44453", + "sslPort": 44308 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5124", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7158;http://localhost:5124", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/src/SharpConnector.Api/SharpConnector.Api.csproj b/src/SharpConnector.Api/SharpConnector.Api.csproj new file mode 100644 index 0000000..2734a0b --- /dev/null +++ b/src/SharpConnector.Api/SharpConnector.Api.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + enable + enable + false + + + + + + + + + + + diff --git a/src/SharpConnector.Api/SharpConnector.Api.http b/src/SharpConnector.Api/SharpConnector.Api.http new file mode 100644 index 0000000..add1736 --- /dev/null +++ b/src/SharpConnector.Api/SharpConnector.Api.http @@ -0,0 +1,6 @@ +@SharpConnector.Api_HostAddress = http://localhost:5124 + +GET {{SharpConnector.Api_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/src/SharpConnector.Api/appsettings.Development.json b/src/SharpConnector.Api/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/src/SharpConnector.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/src/SharpConnector.Api/appsettings.json b/src/SharpConnector.Api/appsettings.json new file mode 100644 index 0000000..0924ea4 --- /dev/null +++ b/src/SharpConnector.Api/appsettings.json @@ -0,0 +1,14 @@ +{ + "ConnectorConfig": { + "Instance": "Redis", + "DatabaseNumber": 0, + "ConnectionString": "redis_connectionstring_here" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/SharpConnector/Middleware/SharpConnectorServiceExtensions.cs b/src/SharpConnector/Middleware/SharpConnectorServiceExtensions.cs new file mode 100644 index 0000000..dc2b5a1 --- /dev/null +++ b/src/SharpConnector/Middleware/SharpConnectorServiceExtensions.cs @@ -0,0 +1,31 @@ +// (c) 2020 Francesco Del Re +// This code is licensed under MIT license (see LICENSE.txt for details) +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using SharpConnector.Interfaces; + +namespace SharpConnector.Middleware +{ + public static class SharpConnectorServiceExtensions + { + /// + /// Registers the SharpConnector services in the dependency injection container. + /// This allows for a generic SharpConnectorClient to be used with any specified type T. + /// + /// The type parameter that specifies the type the client will handle. + /// The service collection to which the SharpConnector services will be added. + /// The updated service collection with the SharpConnector services registered. + public static IServiceCollection AddSharpConnectorServices(this IServiceCollection services) + { + services.AddControllers(); + + services.AddSingleton>(sp => + { + var configuration = sp.GetRequiredService(); + return new SharpConnectorClient(configuration); + }); + + return services; + } + } +} diff --git a/src/SharpConnector/SharpConnector.csproj b/src/SharpConnector/SharpConnector.csproj index f617e7e..82328cd 100755 --- a/src/SharpConnector/SharpConnector.csproj +++ b/src/SharpConnector/SharpConnector.csproj @@ -17,7 +17,7 @@ - + diff --git a/src/SharpConnector/SharpConnector.sln b/src/SharpConnector/SharpConnector.sln index f744048..4e16e1b 100755 --- a/src/SharpConnector/SharpConnector.sln +++ b/src/SharpConnector/SharpConnector.sln @@ -1,11 +1,13 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.30621.155 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpConnector", "SharpConnector.csproj", "{04B2A952-D494-45B5-8819-EB0C38975CCD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpConnector", "SharpConnector.csproj", "{04B2A952-D494-45B5-8819-EB0C38975CCD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpConnector.Test", "..\SharpConnector.Test\SharpConnector.Test.csproj", "{F5953500-0FA2-4520-8E52-AB7E67293F09}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharpConnector.Test", "..\SharpConnector.Test\SharpConnector.Test.csproj", "{F5953500-0FA2-4520-8E52-AB7E67293F09}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpConnector.Api", "..\SharpConnector.Api\SharpConnector.Api.csproj", "{72A48B94-89D2-4F16-AE8C-E32B763EFA99}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {F5953500-0FA2-4520-8E52-AB7E67293F09}.Debug|Any CPU.Build.0 = Debug|Any CPU {F5953500-0FA2-4520-8E52-AB7E67293F09}.Release|Any CPU.ActiveCfg = Release|Any CPU {F5953500-0FA2-4520-8E52-AB7E67293F09}.Release|Any CPU.Build.0 = Release|Any CPU + {72A48B94-89D2-4F16-AE8C-E32B763EFA99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {72A48B94-89D2-4F16-AE8C-E32B763EFA99}.Debug|Any CPU.Build.0 = Debug|Any CPU + {72A48B94-89D2-4F16-AE8C-E32B763EFA99}.Release|Any CPU.ActiveCfg = Release|Any CPU + {72A48B94-89D2-4F16-AE8C-E32B763EFA99}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/SharpConnector/SharpConnectorClient.cs b/src/SharpConnector/SharpConnectorClient.cs index 5f404cb..827cff7 100755 --- a/src/SharpConnector/SharpConnectorClient.cs +++ b/src/SharpConnector/SharpConnectorClient.cs @@ -13,6 +13,11 @@ public sealed class SharpConnectorClient : ISharpConnectorClient { private IOperations _operations; + public SharpConnectorClient(IConfiguration configuration) + { + InitOperations(configuration); + } + /// /// Create e new SharpConnectorClient instance. ///