Various changes (Controller and test)
- Added RootController - Added new methods in BaseController - Added BaseResponse - Changed Program structure - Added first basic test for Program
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
[Controller]
|
||||
@@ -17,7 +18,41 @@ public abstract class BaseController : ControllerBase
|
||||
_configuration.GetSection("AppSettings").Bind(_appSettings);
|
||||
}
|
||||
|
||||
#nullable enable
|
||||
private BaseResponse CreateResponse(HttpStatusCode status, string message, object? data = null)
|
||||
{
|
||||
return new BaseResponse()
|
||||
{
|
||||
Status = (int)status,
|
||||
Message = message,
|
||||
Data = data
|
||||
};
|
||||
}
|
||||
|
||||
protected new IActionResult Created(string message, object? data = null)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.Created, CreateResponse(HttpStatusCode.Created, message, data));
|
||||
}
|
||||
|
||||
protected IActionResult Success(string message, object? data = null)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.OK, CreateResponse(HttpStatusCode.OK, message, data));
|
||||
}
|
||||
|
||||
protected IActionResult NotFound(string message, object? data = null)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.NotFound, CreateResponse(HttpStatusCode.NotFound, message, data));
|
||||
}
|
||||
|
||||
protected IActionResult BadRequest(string message, object? data = null)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.BadRequest, CreateResponse(HttpStatusCode.BadRequest, message, data));
|
||||
}
|
||||
|
||||
protected IActionResult InternalServerError(string message)
|
||||
{
|
||||
return StatusCode((int)HttpStatusCode.InternalServerError, CreateResponse(HttpStatusCode.InternalServerError, message));
|
||||
}
|
||||
|
||||
#nullable disable
|
||||
}
|
||||
22
MainProject/Controllers/RootController.cs
Normal file
22
MainProject/Controllers/RootController.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
{
|
||||
public class RootController : BaseController
|
||||
{
|
||||
public RootController(
|
||||
IConfiguration configuration
|
||||
) : base(configuration)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("")]
|
||||
public IActionResult GetVersion()
|
||||
{
|
||||
return Success(String.Empty, "Success");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ namespace BasicDotnetTemplate.MainProject.Controllers
|
||||
[Route("version")]
|
||||
public IActionResult GetVersion()
|
||||
{
|
||||
return Ok(new { version = _appSettings.Settings.Version });
|
||||
return Success(String.Empty, _appSettings?.Settings?.Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
10
MainProject/Models/Api/Response/BaseResponse.cs
Normal file
10
MainProject/Models/Api/Response/BaseResponse.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace BasicDotnetTemplate.MainProject.Models.Api.Response;
|
||||
|
||||
public class BaseResponse
|
||||
{
|
||||
public int Status { get; set; }
|
||||
public string Message { get; set; }
|
||||
#nullable enable
|
||||
public object? Data { get; set; }
|
||||
#nullable disable
|
||||
}
|
||||
@@ -1,16 +1,43 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using NLog;
|
||||
using BasicDotnetTemplate.MainProject.Models.Settings;
|
||||
using BasicDotnetTemplate.MainProject.Utils;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BasicDotnetTemplate.MainProject;
|
||||
|
||||
public class ReflectionProgram
|
||||
{
|
||||
public static MethodInfo LaunchConfiguration()
|
||||
{
|
||||
var a = typeof(Program);
|
||||
|
||||
MethodInfo[] methods = a.GetMethods(); //Using BindingFlags.NonPublic does not show any results
|
||||
MethodInfo? initialize = null;
|
||||
|
||||
foreach (MethodInfo m in methods)
|
||||
{
|
||||
if (m.Name == "Initialize")
|
||||
{
|
||||
|
||||
initialize = m;
|
||||
}
|
||||
}
|
||||
return initialize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
internal static class Program
|
||||
{
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
|
||||
internal static WebApplication app;
|
||||
|
||||
public static void Main(string[] args)
|
||||
public static void Initialize(string[] args)
|
||||
{
|
||||
Logger.Info("[Program][Main] Start building");
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
@@ -86,7 +113,7 @@ internal static class Program
|
||||
options.SwaggerDoc("v1", openApiInfo);
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
app = builder.Build();
|
||||
|
||||
// REGISTER MIDDLEWARE HERE
|
||||
app.UseRouting();
|
||||
@@ -110,6 +137,13 @@ internal static class Program
|
||||
}
|
||||
|
||||
Logger.Info("[Program][Main] Launching app");
|
||||
}
|
||||
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
ReflectionProgram.LaunchConfiguration();
|
||||
Initialize(args);
|
||||
|
||||
app.Run();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user