Added backend functionality
This commit is contained in:
83
WorkTime.Defaults/DataResult.cs
Normal file
83
WorkTime.Defaults/DataResult.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WorkTime.Defaults;
|
||||
|
||||
public class DataResult<TResult, TError>
|
||||
where TResult : notnull
|
||||
where TError : notnull {
|
||||
public TResult? Content { get; }
|
||||
public TError? Error { get; }
|
||||
|
||||
public bool IsSuccessful => Error is null && Content is not null;
|
||||
|
||||
public IResult HttpResult => ToHttpResult(this);
|
||||
|
||||
public DataResult(TResult result) {
|
||||
Content = result;
|
||||
}
|
||||
|
||||
public DataResult(TError error) {
|
||||
Error = error;
|
||||
}
|
||||
|
||||
public static IResult ToHttpResult(DataResult<TResult, TError> dataResult) {
|
||||
if (dataResult.Content is bool content)
|
||||
return content ? Results.Ok() : Results.BadRequest();
|
||||
|
||||
if (dataResult.IsSuccessful)
|
||||
return Results.Ok(dataResult.Content);
|
||||
|
||||
if (dataResult.Error is ValidationProblemDetails validationProblem)
|
||||
return Results.ValidationProblem(
|
||||
validationProblem.Errors,
|
||||
validationProblem.Detail,
|
||||
validationProblem.Instance,
|
||||
validationProblem.Status,
|
||||
validationProblem.Title,
|
||||
validationProblem.Type);
|
||||
|
||||
if (dataResult.Error is ProblemDetails problem)
|
||||
return Results.Problem(problem);
|
||||
|
||||
if (typeof(TError).IsAssignableTo(typeof(IResult)))
|
||||
return dataResult.Error as IResult ?? Results.Problem();
|
||||
|
||||
return Results.Problem();
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult, TError>(TResult result) {
|
||||
return new DataResult<TResult, TError>(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult, TError>(TError error) {
|
||||
return new DataResult<TResult, TError>(error);
|
||||
}
|
||||
}
|
||||
|
||||
public class DataResult<TResult> : DataResult<TResult, Exception> where TResult : notnull {
|
||||
public DataResult(TResult result) : base(result) { }
|
||||
public DataResult(Exception error) : base(error) { }
|
||||
|
||||
public static implicit operator DataResult<TResult>(TResult result) {
|
||||
return new DataResult<TResult>(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult<TResult>(Exception error) {
|
||||
return new DataResult<TResult>(error);
|
||||
}
|
||||
}
|
||||
|
||||
public class DataResult : DataResult<bool> {
|
||||
public DataResult(bool result) : base(result) { }
|
||||
public DataResult(Exception error) : base(error) { }
|
||||
|
||||
public static implicit operator DataResult(bool result) {
|
||||
return new DataResult(result);
|
||||
}
|
||||
|
||||
public static implicit operator DataResult(Exception error) {
|
||||
return new DataResult(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user