Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Expression method #201

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions src/Ardalis.Result.AspNetCore/ActionResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
/// <param name="result">The Result to convert to an ActionResult</param>
/// <returns></returns>
public static ActionResult<T> ToActionResult<T>(this Result<T> result, ControllerBase controller)
{
return controller.ToActionResult((IResult)result);
}
=> controller.ToActionResult((IResult)result);

/// <summary>
/// Convert a <see cref="Result"/> to a <see cref="ActionResult"/>
Expand All @@ -28,9 +26,7 @@
/// <param name="result">The Result to convert to an ActionResult</param>
/// <returns></returns>
public static ActionResult ToActionResult(this Result result, ControllerBase controller)
{
return controller.ToActionResult((IResult)result);
}
=> controller.ToActionResult((IResult)result);

/// <summary>
/// Convert a <see cref="Result{T}"/> to a <see cref="ActionResult"/>
Expand All @@ -39,30 +35,24 @@
/// <param name="controller">The controller this is called from</param>
/// <param name="result">The Result to convert to an ActionResult</param>
/// <returns></returns>
public static ActionResult<T> ToActionResult<T>(this ControllerBase controller,
Result<T> result)
{
return controller.ToActionResult((IResult)result);
}
public static ActionResult<T> ToActionResult<T>(this ControllerBase controller, Result<T> result)
=> controller.ToActionResult((IResult)result);

/// <summary>
/// Convert a <see cref="Result"/> to a <see cref="ActionResult"/>
/// </summary>
/// <param name="controller">The controller this is called from</param>
/// <param name="result">The Result to convert to an ActionResult</param>
/// <returns></returns>
public static ActionResult ToActionResult(this ControllerBase controller,
Result result)
{
return controller.ToActionResult((IResult)result);
}
public static ActionResult ToActionResult(this ControllerBase controller, Result result)
=> controller.ToActionResult((IResult)result);

internal static ActionResult ToActionResult(this ControllerBase controller, IResult result)
{
var actionProps = controller.ControllerContext.ActionDescriptor.Properties;

var resultStatusMap = actionProps.ContainsKey(ResultConvention.RESULT_STATUS_MAP_PROP)
?(actionProps[ResultConvention.RESULT_STATUS_MAP_PROP] as ResultStatusMap)
var resultStatusMap = actionProps.ContainsKey(ResultConvention.RESULT_STATUS_MAP_PROP)
? (actionProps[ResultConvention.RESULT_STATUS_MAP_PROP] as ResultStatusMap)
: new ResultStatusMap().AddDefaultMap();

var resultStatusOptions = resultStatusMap[result.Status];
Expand All @@ -76,7 +66,7 @@
: controller.StatusCode(statusCode, result.GetValue());
case ResultStatus.Created:
if(string.IsNullOrEmpty(result.Location))
return controller.Created((string?)null, result.GetValue());

Check warning on line 69 in src/Ardalis.Result.AspNetCore/ActionResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in src/Ardalis.Result.AspNetCore/ActionResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 69 in src/Ardalis.Result.AspNetCore/ActionResultExtensions.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

var httpRequest = controller.HttpContext.Request;
var locationUri = new UriBuilder(httpRequest.Scheme,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@

namespace Ardalis.Result.AspNetCore.Exceptions
{
internal class UnexpectedFailureResultsException : Exception
internal class UnexpectedFailureResultsException(IEnumerable<ResultStatus> statuses) : Exception
{
public UnexpectedFailureResultsException(IEnumerable<ResultStatus> statuses)
{
UnexpectedStatuses = statuses;
}

public IEnumerable<ResultStatus> UnexpectedStatuses { get; }
public IEnumerable<ResultStatus> UnexpectedStatuses { get; } = statuses;

public override string ToString()
{
return $"ActionModel has [{nameof(ExpectedFailuresAttribute)}] with result statuses which are not configured in ResultConvention.";
}
=> $"ActionModel has [{nameof(ExpectedFailuresAttribute)}] with result statuses which are not configured in ResultConvention.";
}
}
9 changes: 2 additions & 7 deletions src/Ardalis.Result.AspNetCore/ExpectedFailuresAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
namespace Ardalis.Result.AspNetCore
{
[AttributeUsage(AttributeTargets.Method)]
public class ExpectedFailuresAttribute : Attribute
public class ExpectedFailuresAttribute(params ResultStatus[] resultStatuses) : Attribute
{
public ExpectedFailuresAttribute(params ResultStatus[] resultStatuses)
{
ResultStatuses = resultStatuses;
}

public IEnumerable<ResultStatus> ResultStatuses { get; }
public IEnumerable<ResultStatus> ResultStatuses { get; } = resultStatuses;
}
}
10 changes: 2 additions & 8 deletions src/Ardalis.Result.AspNetCore/MinimalApiResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ public static partial class ResultExtensions
/// <typeparam name="T">The value being returned</typeparam>
/// <param name="result">The Ardalis.Result to convert to an Microsoft.AspNetCore.Http.IResult</param>
/// <returns></returns>
public static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult<T>(this Result<T> result)
{
return ToMinimalApiResult((IResult)result);
}
public static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult<T>(this Result<T> result) => ToMinimalApiResult((IResult)result);

/// <summary>
/// Convert a <see cref="Result"/> to an instance of <see cref="Microsoft.AspNetCore.Http.IResult"/>
/// </summary>
/// <param name="result">The Ardalis.Result to convert to an Microsoft.AspNetCore.Http.IResult</param>
/// <returns></returns>
public static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult(this Result result)
{
return ToMinimalApiResult((IResult)result);
}
public static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult(this Result result) => ToMinimalApiResult((IResult)result);

internal static Microsoft.AspNetCore.Http.IResult ToMinimalApiResult(this IResult result) =>
result.Status switch
Expand Down
5 changes: 1 addition & 4 deletions src/Ardalis.Result.AspNetCore/ResultConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ internal class ResultConvention : IActionModelConvention

private readonly ResultStatusMap _map;

internal ResultConvention(ResultStatusMap map)
{
_map = map;
}
internal ResultConvention(ResultStatusMap map) => _map = map;

public void Apply(ActionModel action)
{
Expand Down
12 changes: 4 additions & 8 deletions src/Ardalis.Result.AspNetCore/ResultStatusMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ internal ResultStatusMap()
/// Adds default mapping for all known <see cref="ResultStatus"/>es to <see cref="HttpStatusCode"/>s
/// </summary>
public ResultStatusMap AddDefaultMap()
{
return For(ResultStatus.Ok, HttpStatusCode.OK)

=> For(ResultStatus.Ok, HttpStatusCode.OK)
.For(ResultStatus.Created, HttpStatusCode.Created)
.For(ResultStatus.Error, (HttpStatusCode)422, resultStatusOptions => resultStatusOptions
.With(UnprocessableEntity))
Expand All @@ -44,7 +44,6 @@ public ResultStatusMap AddDefaultMap()
resultStatusOptions
.With(UnavailableEntity))
.For(ResultStatus.NoContent, HttpStatusCode.NoContent);
}

/// <summary>
/// Maps <paramref name="status"/> to <paramref name="defaultStatusCode"/>.
Expand Down Expand Up @@ -88,10 +87,7 @@ public ResultStatusMap Remove(ResultStatus status)
/// </summary>
/// <param name="status"></param>
/// <returns></returns>
public bool ContainsKey(ResultStatus status)
{
return _map.ContainsKey(status);
}
public bool ContainsKey(ResultStatus status) => _map.ContainsKey(status);

internal ResultStatusOptions this[ResultStatus status]
{
Expand Down Expand Up @@ -138,7 +134,7 @@ private static ProblemDetails NotFoundEntity(ControllerBase controller, IResult
Detail = result.Errors.Any() ? details.ToString() : null
};
}

private static ProblemDetails ConflictEntity(ControllerBase controller, IResult result)
{
var details = new StringBuilder("Next error(s) occurred:");
Expand Down
27 changes: 9 additions & 18 deletions src/Ardalis.Result/PagedInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,16 @@

namespace Ardalis.Result
{
public class PagedInfo
public class PagedInfo(long pageNumber, long pageSize, long totalPages, long totalRecords)
{

public PagedInfo(long pageNumber, long pageSize, long totalPages, long totalRecords)
{
PageNumber = pageNumber;
PageSize = pageSize;
TotalPages = totalPages;
TotalRecords = totalRecords;
}

[JsonInclude]
public long PageNumber { get; private set; }
[JsonInclude]
public long PageSize { get; private set; }
[JsonInclude]
public long TotalPages { get; private set; }
[JsonInclude]
public long TotalRecords { get; private set; }
[JsonInclude]
public long PageNumber { get; private set; } = pageNumber;
[JsonInclude]
public long PageSize { get; private set; } = pageSize;
[JsonInclude]
public long TotalPages { get; private set; } = totalPages;
[JsonInclude]
public long TotalRecords { get; private set; } = totalRecords;

public PagedInfo SetPageNumber(long pageNumber)
{
Expand Down
11 changes: 3 additions & 8 deletions src/Ardalis.Result/PagedResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@

namespace Ardalis.Result
{
public class PagedResult<T> : Result<T>
public class PagedResult<T>(PagedInfo pagedInfo, T value) : Result<T>(value)
{
public PagedResult(PagedInfo pagedInfo, T value) : base(value)
{
PagedInfo = pagedInfo;
}

[JsonInclude]
public PagedInfo PagedInfo { get; init; }
[JsonInclude]
public PagedInfo PagedInfo { get; init; } = pagedInfo;
}
}
Loading
Loading