diff --git a/MobileConfiguration/Controllers/MobileConfigurationController.cs b/MobileConfiguration/Controllers/MobileConfigurationController.cs deleted file mode 100644 index 2fa98f7..0000000 --- a/MobileConfiguration/Controllers/MobileConfigurationController.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using MobileConfiguration.Repository; -using Shared.Results.Web; - -namespace MobileConfiguration.Controllers -{ - using DataTransferObjects; - using Models; - - [Route("api/[controller]")] - [ApiController] - public class TransactionMobileConfigurationController : ControllerBase - { - private readonly IConfigurationRepository Repository; - - public TransactionMobileConfigurationController(IConfigurationRepository repository) { - this.Repository = repository; - } - - [HttpPost] - public async Task PostConfiguration([FromBody] Configuration configuration, - CancellationToken cancellationToken) { - - MobileConfiguration configurationModel = Factories.Factory.ToMobileConfiguration(configuration); - - var result = await this.Repository.CreateConfiguration(configurationModel, cancellationToken); - - return result.ToActionResultX(); - } - - [HttpGet] - [Route("{id}")] - public async Task GetConfiguration([FromRoute] String id, CancellationToken cancellationToken) { - MobileConfiguration configurationModel = await this.Repository.GetConfiguration(ConfigurationType.TransactionMobile, id, cancellationToken); - - ConfigurationResponse response = Factories.Factory.ToConfigurationResponse(configurationModel); - - return this.Ok(response); - } - - [HttpPut] - public async Task PutConfiguration(String id, Configuration configuration, CancellationToken cancellationToken) { - MobileConfiguration configurationModel = Factories.Factory.ToMobileConfiguration(configuration); - - await this.Repository.UpdateConfiguration(ConfigurationType.TransactionMobile, id, configurationModel, cancellationToken); - - return this.Ok(); - } - } - - -} diff --git a/MobileConfiguration/Controllers/TransactionMobileLoggingController.cs b/MobileConfiguration/Controllers/TransactionMobileLoggingController.cs deleted file mode 100644 index bb17a51..0000000 --- a/MobileConfiguration/Controllers/TransactionMobileLoggingController.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Microsoft.AspNetCore.Mvc; -using MobileConfiguration.DataTransferObjects; -using MobileConfiguration.Repository; -using Newtonsoft.Json; -using Shared.Logger; - -namespace MobileConfiguration.Controllers; - -[Route("api/[controller]")] -[ApiController] -public class TransactionMobileLoggingController : ControllerBase -{ - [HttpPost] - public async Task PostConfiguration([FromBody] List logMessages, - CancellationToken cancellationToken) { - - Logger.LogInformation(JsonConvert.SerializeObject(logMessages)); - - return this.Ok(); - } -} \ No newline at end of file diff --git a/MobileConfiguration/Handlers/TransactionMobileConfigurationHandler.cs b/MobileConfiguration/Handlers/TransactionMobileConfigurationHandler.cs new file mode 100644 index 0000000..cdbb46c --- /dev/null +++ b/MobileConfiguration/Handlers/TransactionMobileConfigurationHandler.cs @@ -0,0 +1,88 @@ +using MobileConfiguration.DataTransferObjects; +using MobileConfiguration.Factories; +using MobileConfiguration.Models; +using MobileConfiguration.Repository; +using Shared.Results.Web; + +namespace MobileConfiguration.Handlers +{ + public static class TransactionMobileConfigurationHandler + { + public static async Task PostConfiguration(IConfigurationRepository repository, + Configuration configuration, + CancellationToken cancellationToken) + { + Models.MobileConfiguration configurationModel = Factory.ToMobileConfiguration(configuration); + + var result = await repository.CreateConfiguration(configurationModel, cancellationToken); + + // Attempt to map the result to an appropriate HTTP response. + // The shared library previously used an extension to convert to IActionResult from controllers. + // For minimal APIs return 200 on success and 400 on failure. + try + { + // Try to read a `Success` or `IsSuccess` property via dynamic - fallback if not present. + dynamic dyn = result; + bool success = false; + + if (HasProperty(dyn, "Success")) success = (bool)dyn.Success; + else if (HasProperty(dyn, "IsSuccess")) success = (bool)dyn.IsSuccess; + else if (HasProperty(dyn, "Succeeded")) success = (bool)dyn.Succeeded; + + if (success) + { + return Results.Ok(result); + } + + return Results.BadRequest(result); + } + catch + { + // If we cannot inspect the result type, return OK with the result object. + return Results.Ok(result); + } + } + + public static async Task GetConfiguration(IConfigurationRepository repository, + string id, + CancellationToken cancellationToken) + { + Models.MobileConfiguration configurationModel = await repository.GetConfiguration(ConfigurationType.TransactionMobile, id, cancellationToken); + + ConfigurationResponse response = Factory.ToConfigurationResponse(configurationModel); + + return Results.Ok(response); + } + + public static async Task PutConfiguration(IConfigurationRepository repository, + string id, + Configuration configuration, + CancellationToken cancellationToken) + { + Models.MobileConfiguration configurationModel = Factory.ToMobileConfiguration(configuration); + + await repository.UpdateConfiguration(ConfigurationType.TransactionMobile, id, configurationModel, cancellationToken); + + return Results.Ok(); + } + + private static bool HasProperty(dynamic obj, string name) + { + try + { + var dictionary = obj as System.Collections.IDictionary; + if (dictionary != null) + { + return dictionary.Contains(name); + } + + // fallback to reflection + return obj.GetType().GetProperty(name) != null; + } + catch + { + return false; + } + } + } +} diff --git a/MobileConfiguration/Handlers/TransactionMobileLoggingHandler.cs b/MobileConfiguration/Handlers/TransactionMobileLoggingHandler.cs new file mode 100644 index 0000000..963975c --- /dev/null +++ b/MobileConfiguration/Handlers/TransactionMobileLoggingHandler.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Mvc; +using MobileConfiguration.DataTransferObjects; +using Newtonsoft.Json; +using Shared.Logger; + +namespace MobileConfiguration.Handlers +{ + public static class TransactionMobileLoggingHandler + { + public static Task PostLogging(List logMessages, CancellationToken cancellationToken) + { + Logger.LogInformation(JsonConvert.SerializeObject(logMessages)); + return Task.FromResult(Results.Ok() as IResult); + } + } +} diff --git a/MobileConfiguration/MobileConfiguration.csproj b/MobileConfiguration/MobileConfiguration.csproj index 886a093..531b2ac 100644 --- a/MobileConfiguration/MobileConfiguration.csproj +++ b/MobileConfiguration/MobileConfiguration.csproj @@ -91,4 +91,8 @@ + + + + diff --git a/MobileConfiguration/Models/Models.cs b/MobileConfiguration/Models/Models.cs index f5bbe52..e130bf8 100644 --- a/MobileConfiguration/Models/Models.cs +++ b/MobileConfiguration/Models/Models.cs @@ -1,6 +1,4 @@ -using MobileConfiguration.Controllers; - -namespace MobileConfiguration.Models +namespace MobileConfiguration.Models { public class MobileConfiguration { diff --git a/MobileConfiguration/Program.cs b/MobileConfiguration/Program.cs index 6cdd99a..af57c19 100644 --- a/MobileConfiguration/Program.cs +++ b/MobileConfiguration/Program.cs @@ -65,7 +65,8 @@ // Add services to the container. builder.Services.AddHttpContextAccessor(); -builder.Services.AddControllers(); +builder.Services.AddAuthorization(); +// Use minimal APIs and handler pattern instead of MVC controllers // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); @@ -108,7 +109,12 @@ app.AddExceptionHandler(); -app.MapControllers(); +// Minimal API endpoints (handler pattern) +app.MapPost("/api/TransactionMobileConfiguration", MobileConfiguration.Handlers.TransactionMobileConfigurationHandler.PostConfiguration); +app.MapGet("/api/TransactionMobileConfiguration/{id}", MobileConfiguration.Handlers.TransactionMobileConfigurationHandler.GetConfiguration); +app.MapPut("/api/TransactionMobileConfiguration/{id}", MobileConfiguration.Handlers.TransactionMobileConfigurationHandler.PutConfiguration); + +app.MapPost("/api/TransactionMobileLogging", MobileConfiguration.Handlers.TransactionMobileLoggingHandler.PostLogging); InitializeDatabase(app).Wait(CancellationToken.None); diff --git a/MobileConfiguration/appsettings.json b/MobileConfiguration/appsettings.json index 6075e36..6abeec4 100644 --- a/MobileConfiguration/appsettings.json +++ b/MobileConfiguration/appsettings.json @@ -4,6 +4,6 @@ "InMemoryDatabase": false }, "ConnectionStrings": { - "ConfigurationDatabase": "server=127.0.0.1;user id=sa;password=sp1ttal;database=ConfigurationHost-00000000-0000-0000-0000-000000000001;Encrypt=false" + "ConfigurationDatabase": "server=192.168.1.163;user id=sa;password=Sc0tland;database=ConfigurationHost;Encrypt=false" } }