-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from Cratis:paging-sorting-fixes
Paging and sorting fixes
- Loading branch information
Showing
19 changed files
with
334 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
Source/DotNET/Applications/Commands/CommandMethodExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using Cratis.Reflection; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Cratis.Applications.Commands; | ||
|
||
/// <summary> | ||
/// Extension methods for methods representing commands. | ||
/// </summary> | ||
public static class CommandMethodExtensions | ||
{ | ||
/// <summary> | ||
/// Check if a method is a command. | ||
/// </summary> | ||
/// <param name="methodInfo">The <see cref="MethodInfo"/> to check.</param> | ||
/// <returns>True if it is a command, false if not.</returns> | ||
public static bool IsCommand(this MethodInfo methodInfo) => | ||
methodInfo.HasAttribute<HttpPostAttribute>() && | ||
!methodInfo.HasAttribute<AspNetResultAttribute>() && | ||
(!methodInfo.DeclaringType?.HasAttribute<AspNetResultAttribute>() ?? false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Source/DotNET/Applications/Queries/QueryMethodExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Reflection; | ||
using Cratis.Reflection; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Cratis.Applications.Queries; | ||
|
||
/// <summary> | ||
/// Extension methods for methods representing commands. | ||
/// </summary> | ||
public static class QueryMethodExtensions | ||
{ | ||
/// <summary> | ||
/// Check if a method is a command. | ||
/// </summary> | ||
/// <param name="methodInfo">The <see cref="MethodInfo"/> to check.</param> | ||
/// <returns>True if it is a command, false if not.</returns> | ||
public static bool IsQuery(this MethodInfo methodInfo) => | ||
methodInfo.HasAttribute<HttpGetAttribute>() && | ||
methodInfo.ReturnType != typeof(void) && | ||
methodInfo.ReturnType != typeof(Task) && | ||
methodInfo.ReturnType != typeof(ValueTask) && | ||
!methodInfo.HasAttribute<AspNetResultAttribute>() && | ||
(!methodInfo.DeclaringType?.HasAttribute<AspNetResultAttribute>() ?? false); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright (c) Cratis. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Cratis.Applications.Commands; | ||
using Cratis.Concepts; | ||
using Cratis.Reflection; | ||
using Microsoft.OpenApi.Models; | ||
using Swashbuckle.AspNetCore.SwaggerGen; | ||
|
||
namespace Cratis.Applications.Swagger; | ||
|
||
/// <summary> | ||
/// Represents an implementation of <see cref="IOperationFilter"/> that adds the command result to the operation for command methods. | ||
/// </summary> | ||
/// <param name="schemaGenerator">The <see cref="ISchemaGenerator"/> to use.</param> | ||
public class CommandResultOperationFilter(ISchemaGenerator schemaGenerator) : IOperationFilter | ||
{ | ||
/// <inheritdoc/> | ||
public void Apply(OpenApiOperation operation, OperationFilterContext context) | ||
{ | ||
if (!context.MethodInfo.IsCommand()) return; | ||
|
||
var returnType = context.MethodInfo.GetActualReturnType(); | ||
|
||
Type? commandResultType = null; | ||
|
||
if (returnType == typeof(Task) || returnType == typeof(ValueTask) || returnType == typeof(void)) | ||
{ | ||
returnType = typeof(object); | ||
commandResultType = typeof(CommandResult); | ||
} | ||
else if (returnType.IsConcept()) | ||
{ | ||
returnType = returnType.GetConceptValueType(); | ||
} | ||
|
||
commandResultType ??= typeof(CommandResult<>).MakeGenericType(returnType); | ||
|
||
var schema = schemaGenerator.GenerateSchema(commandResultType, context.SchemaRepository); | ||
var response = operation.Responses.First().Value; | ||
if (response.Content.ContainsKey("application/json")) | ||
{ | ||
operation.Responses.First().Value.Content["application/json"].Schema = schema; | ||
} | ||
else | ||
{ | ||
response.Content.Add(new("application/json", new() { Schema = schema })); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.