основной круд готов-
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using FluentResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Tnsu.ApprovalRoute.Application.Common.Errors;
|
||||
|
||||
namespace Tnsu.ApprovalRoute.Api.Common;
|
||||
|
||||
public static class ResultExtensions
|
||||
{
|
||||
public static IActionResult ToActionResult<T>(this Result<T> result)
|
||||
{
|
||||
if (result.IsFailed)
|
||||
return result.Errors[0].ToActionResult();
|
||||
|
||||
var warnings = result.Reasons.OfType<Warning>().Select(w => w.Message).ToList();
|
||||
return new OkObjectResult(new { data = result.Value, warnings });
|
||||
}
|
||||
|
||||
public static IActionResult ToActionResult(this Result result)
|
||||
{
|
||||
if (result.IsFailed)
|
||||
return result.Errors[0].ToActionResult();
|
||||
|
||||
return new NoContentResult();
|
||||
}
|
||||
|
||||
private static IActionResult ToActionResult(this IError error) => error switch
|
||||
{
|
||||
NotFoundError => new NotFoundObjectResult(new { message = error.Message }),
|
||||
ValidationError => new BadRequestObjectResult(new { message = error.Message }),
|
||||
ConflictError => new ConflictObjectResult(new { message = error.Message }),
|
||||
_ => new ObjectResult(new { message = error.Message }) { StatusCode = StatusCodes.Status500InternalServerError }
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user