Skip to content

Commit

Permalink
Bringing back AspNetResult attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
einari committed Jun 9, 2023
1 parent 5eef618 commit 743d39d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
17 changes: 17 additions & 0 deletions Source/DotNET/CQRS/AspNetResultAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.AspNetCore.Mvc.Filters;

namespace Aksio.Applications;

/// <summary>
/// Represents an attribute that indicates that the result of actions in a controller should be returned as an ASP.NET result.
/// </summary>
/// <remarks>
/// Can be used for an entire controller or individual actions.
/// </remarks>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class AspNetResultAttribute : Attribute, IFilterMetadata
{
}
20 changes: 20 additions & 0 deletions Source/DotNET/CQRS/AspNetResultExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Aksio.Applications;

namespace Microsoft.AspNetCore.Mvc.Filters;

/// <summary>
/// Extensions for <see cref="ActionExecutingContext"/>.
/// </summary>
public static class AspNetResultExtensions
{
/// <summary>
/// Check if an action has the with <see cref="AspNetResultAttribute"/> as filter.
/// </summary>
/// <param name="context"><see cref="ActionExecutingContext"/> to check.</param>
/// <returns>True if it has, false if not.</returns>
public static bool IsAspNetResult(this ActionExecutingContext context)
=> context.Filters.Any(_ => _ is AspNetResultAttribute);
}
9 changes: 6 additions & 3 deletions Source/DotNET/CQRS/Commands/CommandActionFilter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Aksio Insurtech. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Net;
using Aksio.Applications.Validation;
using Aksio.Commands;
using Aksio.Execution;
Expand Down Expand Up @@ -38,6 +39,8 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
{
result = await next();

if (context.IsAspNetResult()) return;

if (result.Exception is not null)
{
var exception = result.Exception;
Expand Down Expand Up @@ -70,15 +73,15 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE

if (!commandResult.IsAuthorized)
{
context.HttpContext.Response.StatusCode = 401; // Forbidden: https://www.rfc-editor.org/rfc/rfc9110.html#name-401-unauthorized
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden; // Forbidden: https://www.rfc-editor.org/rfc/rfc9110.html#name-401-unauthorized
}
else if (!commandResult.IsValid)
{
context.HttpContext.Response.StatusCode = 409; // Conflict: https://www.rfc-editor.org/rfc/rfc9110.html#name-409-conflict
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict; // Conflict: https://www.rfc-editor.org/rfc/rfc9110.html#name-409-conflict
}
else if (commandResult.HasExceptions)
{
context.HttpContext.Response.StatusCode = 500; // Internal Server error: https://www.rfc-editor.org/rfc/rfc9110.html#name-500-internal-server-error
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; // Internal Server error: https://www.rfc-editor.org/rfc/rfc9110.html#name-500-internal-server-error
}

var actualResult = new ObjectResult(commandResult);
Expand Down
2 changes: 2 additions & 0 deletions Source/DotNET/CQRS/Queries/QueryActionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
{
result = await next();

if (context.IsAspNetResult()) return;

if (result.Exception is not null)
{
var exception = result.Exception;
Expand Down

0 comments on commit 743d39d

Please sign in to comment.