Skip to content

Commit aee9041

Browse files
committed
Refactor library and improve folders architecture
1 parent da83ae5 commit aee9041

21 files changed

+121
-56
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ To get started with Initium, just add the package using NuGet:
2323
dotnet add package Initium
2424
```
2525

26-
## How it Works?
26+
## How does it Work?
2727

2828
Here's how to create a controller :
2929

src/Initium/Attributes/ValidateModelAttribute.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
namespace Initium.Attributes;
88

99
/// <summary>
10-
/// An attribute that performs model validation using a specified FluentValidation validator.
10+
/// An attribute that performs request validation using a specified FluentValidation validator.
1111
/// </summary>
1212
[AttributeUsage(AttributeTargets.Method)]
1313
[SuppressMessage("ReSharper", "UnusedType.Global")]
14-
public class ValidateModelAttribute(Type validatorType) : Attribute, IActionFilter
14+
public class ValidateRequestAttribute(Type validatorType) : Attribute, IActionFilter
1515
{
1616
/// <summary>
17-
/// Called before the action method executes. Validates the model using the specified validator.
17+
/// Called before the action method executes. Validates the request using the specified validator.
1818
/// </summary>
1919
/// <param name="context">The context for the action filter.</param>
2020
public void OnActionExecuting(ActionExecutingContext context)
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Net;
22
using Initium.Exceptions;
3-
using Microsoft.AspNetCore.Mvc;
43
using Initium.Filters;
54
using Initium.Infrastructure;
5+
using Microsoft.AspNetCore.Mvc;
66

7-
namespace Initium;
7+
namespace Initium.Controllers;
88

99
/// <summary>
1010
/// Base API controller providing global filters and routing.
@@ -25,3 +25,14 @@ public abstract class ApiController<TService> : ApiController where TService : B
2525
{
2626
protected TService Service => (TService?)HttpContext.RequestServices.GetService(typeof(TService)) ?? throw new ApiException(HttpStatusCode.InternalServerError, $"Service of type `{typeof(TService).Name}` is not registered in the dependency injection container.");
2727
}
28+
29+
// handle this
30+
// public static ActionResult<T> OrFallbackWithStatusCode<T>(this T? baseEntity, HttpStatusCode statusCode) =>
31+
// baseEntity != null ? new OkObjectResult(baseEntity) : new StatusCodeResult((int)statusCode);
32+
33+
// Add serializable to exceptions
34+
// [Serializable]
35+
36+
// handle new/override
37+
// return ServiceResult.Ok("COOL").As<SignUpResponse>();
38+
// return ServiceResult<SignUpResponse>.Ok("COOL");

src/Initium/Infrastructure/BaseController.cs renamed to src/Initium/Controllers/BaseController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Microsoft.AspNetCore.Mvc;
22

3-
namespace Initium.Infrastructure;
3+
namespace Initium.Controllers;
44

55
public class BaseController : ControllerBase
66
{

src/Initium/Extensions/StringExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ public static class StringExtensions
2424
/// </summary>
2525
/// <param name="value">The string to check.</param>
2626
/// <returns><c>true</c> if the string is empty after trimming; otherwise, <c>false</c>.</returns>
27-
public static bool IsEmpty(this string value) => value.Trim().Equals(string.Empty);
27+
public static bool IsEmpty(this string value) => string.IsNullOrWhiteSpace(value.Trim());
2828
}

src/Initium/Filters/ApiExceptionFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.AspNetCore.Mvc.Filters;
44
using Initium.Exceptions;
55
using Initium.Helpers;
6+
using Initium.Infrastructure.Helpers;
67
using Initium.Response;
78
using Microsoft.Extensions.Logging;
89

src/Initium/Filters/ApiResponseFilter.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
using Initium.Helpers;
55
using Initium.Response;
66
using Initium.Results;
7-
using Microsoft.Extensions.Logging;
87

98
namespace Initium.Filters;
109

1110
/// <summary>
1211
/// Filter responsible for transforming <see cref="ServiceResult"/> objects into a standardized <see cref="ApiResponse"/>.
1312
/// </summary>
14-
internal class ApiResponseFilter(ILogger<ApiResponseFilter> logger) : ActionFilterAttribute
13+
internal class ApiResponseFilter : ActionFilterAttribute
1514
{
1615
/// <summary>
1716
/// Called after the action executed, and transforms <see cref="ServiceResult"/> into a standardized <see cref="ApiResponse"/>.
@@ -21,29 +20,30 @@ public override void OnActionExecuted(ActionExecutedContext context)
2120
{
2221
// Check if the result of the action is an ObjectResult containing a ServiceResult.
2322
if (context.Result is not ObjectResult { Value: ServiceResult serviceResult }) return;
24-
23+
if (string.IsNullOrWhiteSpace(serviceResult.Message)) serviceResult.Message = null;
24+
2525
// Start building the ApiResponse using the Fluent Builder
2626
var apiResponseBuilder = ApiResponseBuilder.CreateFromContext(context.HttpContext);
27-
27+
2828
// Determine the HTTP status code:
29-
// 1. Use ServiceResult status code if set.
30-
// 2. Otherwise, use the default based on the HTTP method.
31-
var statusCode =
32-
serviceResult.StatusCode
29+
// 1. Use ServiceResult status code if set.
30+
// 2. Otherwise, use the default based on the HTTP method.
31+
var statusCode =
32+
serviceResult.StatusCode
3333
?? ApiResponseHelper.GetDefaultStatusCode(context.HttpContext);
3434

35-
// Determine the response message:
36-
// 1. Use ServiceResult message if set.
37-
// 2. Otherwise, get the message from [ApiResponse] attributes.
38-
// 3. If none is found, use the default message for the StatusCode.
39-
var message =
40-
serviceResult.Message
41-
?? ApiResponseHelper.GetApiResponseMessage(context.ActionDescriptor, statusCode)
35+
// Determine the response message:
36+
// 1. Use ServiceResult message if set.
37+
// 2. Otherwise, get the message from [ApiResponse] attributes.
38+
// 3. If none is found, use the default message for the StatusCode.
39+
var message =
40+
serviceResult.Message
41+
?? ApiResponseHelper.GetApiResponseMessage(context.ActionDescriptor, statusCode)
4242
?? ApiResponseHelper.GetDefaultMessageForStatusCode(statusCode);
4343

4444
// Determine the appropriate response based on the status code:
45-
// - For 204 (No Content) and 304 (Not Modified), set a StatusCodeResult without a response body.
46-
// - For other status codes, construct a standardized ApiResponse object including HTTP context details.
45+
// - For 204 (No Content) and 304 (Not Modified), set a StatusCodeResult without a response body.
46+
// - For other status codes, construct a standardized ApiResponse object including HTTP context details.
4747
context.Result = statusCode switch
4848
{
4949
HttpStatusCode.NoContent or HttpStatusCode.NotModified => new StatusCodeResult((int)statusCode),

src/Initium/Filters/ImplicitValidationFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Net;
22
using FluentValidation;
3-
using Initium.Infrastructure;
3+
using Initium.Request;
44
using Initium.Response;
55
using Microsoft.AspNetCore.Mvc.Filters;
66

src/Initium/Filters/LoggingFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Diagnostics;
22
using System.Net;
3-
using Initium.Helpers;
3+
using Initium.Infrastructure.Helpers;
44
using Microsoft.AspNetCore.Mvc.Filters;
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Logging;

src/Initium/Infrastructure/BaseRequestWithValidator.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)