(feature): init
This commit is contained in:
commit
c93020745a
63
.gitattributes
vendored
Normal file
63
.gitattributes
vendored
Normal file
@ -0,0 +1,63 @@
|
||||
###############################################################################
|
||||
# Set default behavior to automatically normalize line endings.
|
||||
###############################################################################
|
||||
* text=auto
|
||||
|
||||
###############################################################################
|
||||
# Set default behavior for command prompt diff.
|
||||
#
|
||||
# This is need for earlier builds of msysgit that does not have it on by
|
||||
# default for csharp files.
|
||||
# Note: This is only used by command line
|
||||
###############################################################################
|
||||
#*.cs diff=csharp
|
||||
|
||||
###############################################################################
|
||||
# Set the merge driver for project and solution files
|
||||
#
|
||||
# Merging from the command prompt will add diff markers to the files if there
|
||||
# are conflicts (Merging from VS is not affected by the settings below, in VS
|
||||
# the diff markers are never inserted). Diff markers may cause the following
|
||||
# file extensions to fail to load in VS. An alternative would be to treat
|
||||
# these files as binary and thus will always conflict and require user
|
||||
# intervention with every merge. To do so, just uncomment the entries below
|
||||
###############################################################################
|
||||
#*.sln merge=binary
|
||||
#*.csproj merge=binary
|
||||
#*.vbproj merge=binary
|
||||
#*.vcxproj merge=binary
|
||||
#*.vcproj merge=binary
|
||||
#*.dbproj merge=binary
|
||||
#*.fsproj merge=binary
|
||||
#*.lsproj merge=binary
|
||||
#*.wixproj merge=binary
|
||||
#*.modelproj merge=binary
|
||||
#*.sqlproj merge=binary
|
||||
#*.wwaproj merge=binary
|
||||
|
||||
###############################################################################
|
||||
# behavior for image files
|
||||
#
|
||||
# image files are treated as binary by default.
|
||||
###############################################################################
|
||||
#*.jpg binary
|
||||
#*.png binary
|
||||
#*.gif binary
|
||||
|
||||
###############################################################################
|
||||
# diff behavior for common document formats
|
||||
#
|
||||
# Convert binary document formats to text before diffing them. This feature
|
||||
# is only available from the command line. Turn it on by uncommenting the
|
||||
# entries below.
|
||||
###############################################################################
|
||||
#*.doc diff=astextplain
|
||||
#*.DOC diff=astextplain
|
||||
#*.docx diff=astextplain
|
||||
#*.DOCX diff=astextplain
|
||||
#*.dot diff=astextplain
|
||||
#*.DOT diff=astextplain
|
||||
#*.pdf diff=astextplain
|
||||
#*.PDF diff=astextplain
|
||||
#*.rtf diff=astextplain
|
||||
#*.RTF diff=astextplain
|
||||
266
.gitignore
vendored
Normal file
266
.gitignore
vendored
Normal file
@ -0,0 +1,266 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
|
||||
# User-specific files
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
|
||||
# Visual Studio 2015 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUNIT
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# DNX
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_i.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*.log
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# JustCode is a .NET coding add-in
|
||||
.JustCode
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# TODO: Comment the next line if you want to checkin your web deploy settings
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
#*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/packages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/packages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/packages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignoreable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
node_modules/
|
||||
orleans.codegen.cs
|
||||
.directory
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# RIA/Silverlight projects
|
||||
Generated_Code/
|
||||
|
||||
# Backup & report files from converting an old project file
|
||||
# to a newer Visual Studio version. Backup files are not needed,
|
||||
# because we have git ;-)
|
||||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
UpgradeLog*.htm
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# JetBrains Rider
|
||||
.idea/
|
||||
*.sln.iml
|
||||
|
||||
# CodeRush
|
||||
.cr/
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
|
||||
**/*docker-compose/LetsEncryptServer/acme
|
||||
**/*docker-compose/LetsEncryptServer/cache
|
||||
13
src/MaksIT.Results/MaksIT.Results.csproj
Normal file
13
src/MaksIT.Results/MaksIT.Results.csproj
Normal file
@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
250
src/MaksIT.Results/Result.ClientError.cs
Normal file
250
src/MaksIT.Results/Result.ClientError.cs
Normal file
@ -0,0 +1,250 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server could not understand the request due to invalid syntax.
|
||||
/// Corresponds to HTTP status code 400 Bad Request.
|
||||
/// </summary>
|
||||
public static Result BadRequest(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the client must authenticate itself to get the requested response.
|
||||
/// Corresponds to HTTP status code 401 Unauthorized.
|
||||
/// </summary>
|
||||
public static Result Unauthorized(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the client does not have access rights to the content.
|
||||
/// Corresponds to HTTP status code 403 Forbidden.
|
||||
/// </summary>
|
||||
public static Result Forbidden(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.Forbidden);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server can not find the requested resource.
|
||||
/// Corresponds to HTTP status code 404 Not Found.
|
||||
/// </summary>
|
||||
public static Result NotFound(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request could not be completed due to a conflict with the current state of the resource.
|
||||
/// Corresponds to HTTP status code 409 Conflict.
|
||||
/// </summary>
|
||||
public static Result Conflict(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.Conflict);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource is no longer available and will not be available again.
|
||||
/// Corresponds to HTTP status code 410 Gone.
|
||||
/// </summary>
|
||||
public static Result Gone(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)410); // 410 Gone
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request failed because it depended on another request and that request failed.
|
||||
/// Corresponds to HTTP status code 424 Failed Dependency.
|
||||
/// </summary>
|
||||
public static Result FailedDependency(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)424); // 424 Failed Dependency
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server requires the request to be conditional.
|
||||
/// Corresponds to HTTP status code 428 Precondition Required.
|
||||
/// </summary>
|
||||
public static Result PreconditionRequired(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)428); // 428 Precondition Required
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the user has sent too many requests in a given amount of time.
|
||||
/// Corresponds to HTTP status code 429 Too Many Requests.
|
||||
/// </summary>
|
||||
public static Result TooManyRequests(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)429); // 429 Too Many Requests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is unwilling to process the request because its header fields are too large.
|
||||
/// Corresponds to HTTP status code 431 Request Header Fields Too Large.
|
||||
/// </summary>
|
||||
public static Result RequestHeaderFieldsTooLarge(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)431); // 431 Request Header Fields Too Large
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request entity because it is too large.
|
||||
/// Corresponds to HTTP status code 413 Payload Too Large.
|
||||
/// </summary>
|
||||
public static Result PayloadTooLarge(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)413); // 413 Payload Too Large
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because the URI is too long.
|
||||
/// Corresponds to HTTP status code 414 URI Too Long.
|
||||
/// </summary>
|
||||
public static Result UriTooLong(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)414); // 414 URI Too Long
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because the media type is unsupported.
|
||||
/// Corresponds to HTTP status code 415 Unsupported Media Type.
|
||||
/// </summary>
|
||||
public static Result UnsupportedMediaType(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.UnsupportedMediaType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because it expects the request to have a defined Content-Length header.
|
||||
/// Corresponds to HTTP status code 411 Length Required.
|
||||
/// </summary>
|
||||
public static Result LengthRequired(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)411); // 411 Length Required
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request due to an illegal request entity.
|
||||
/// Corresponds to HTTP status code 422 Unprocessable Entity.
|
||||
/// </summary>
|
||||
public static Result UnprocessableEntity(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), (HttpStatusCode)422); // 422 Unprocessable Entity
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server could not understand the request due to invalid syntax.
|
||||
/// Corresponds to HTTP status code 400 Bad Request.
|
||||
/// </summary>
|
||||
public static Result<T> BadRequest(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.BadRequest);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the client must authenticate itself to get the requested response.
|
||||
/// Corresponds to HTTP status code 401 Unauthorized.
|
||||
/// </summary>
|
||||
public static Result<T> Unauthorized(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.Unauthorized);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the client does not have access rights to the content.
|
||||
/// Corresponds to HTTP status code 403 Forbidden.
|
||||
/// </summary>
|
||||
public static Result<T> Forbidden(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.Forbidden);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server can not find the requested resource.
|
||||
/// Corresponds to HTTP status code 404 Not Found.
|
||||
/// </summary>
|
||||
public static Result<T> NotFound(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request could not be completed due to a conflict with the current state of the resource.
|
||||
/// Corresponds to HTTP status code 409 Conflict.
|
||||
/// </summary>
|
||||
public static Result<T> Conflict(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.Conflict);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource is no longer available and will not be available again.
|
||||
/// Corresponds to HTTP status code 410 Gone.
|
||||
/// </summary>
|
||||
public static Result<T> Gone(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)410); // 410 Gone
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request failed because it depended on another request and that request failed.
|
||||
/// Corresponds to HTTP status code 424 Failed Dependency.
|
||||
/// </summary>
|
||||
public static Result<T> FailedDependency(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)424); // 424 Failed Dependency
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server requires the request to be conditional.
|
||||
/// Corresponds to HTTP status code 428 Precondition Required.
|
||||
/// </summary>
|
||||
public static Result<T> PreconditionRequired(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)428); // 428 Precondition Required
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the user has sent too many requests in a given amount of time.
|
||||
/// Corresponds to HTTP status code 429 Too Many Requests.
|
||||
/// </summary>
|
||||
public static Result<T> TooManyRequests(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)429); // 429 Too Many Requests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is unwilling to process the request because its header fields are too large.
|
||||
/// Corresponds to HTTP status code 431 Request Header Fields Too Large.
|
||||
/// </summary>
|
||||
public static Result<T> RequestHeaderFieldsTooLarge(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)431); // 431 Request Header Fields Too Large
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request entity because it is too large.
|
||||
/// Corresponds to HTTP status code 413 Payload Too Large.
|
||||
/// </summary>
|
||||
public static Result<T> PayloadTooLarge(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)413); // 413 Payload Too Large
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because the URI is too long.
|
||||
/// Corresponds to HTTP status code 414 URI Too Long.
|
||||
/// </summary>
|
||||
public static Result<T> UriTooLong(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)414); // 414 URI Too Long
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because the media type is unsupported.
|
||||
/// Corresponds to HTTP status code 415 Unsupported Media Type.
|
||||
/// </summary>
|
||||
public static Result<T> UnsupportedMediaType(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.UnsupportedMediaType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request because it expects the request to have a defined Content-Length header.
|
||||
/// Corresponds to HTTP status code 411 Length Required.
|
||||
/// </summary>
|
||||
public static Result<T> LengthRequired(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)411); // 411 Length Required
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server cannot process the request due to an illegal request entity.
|
||||
/// Corresponds to HTTP status code 422 Unprocessable Entity.
|
||||
/// </summary>
|
||||
public static Result<T> UnprocessableEntity(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), (HttpStatusCode)422); // 422 Unprocessable Entity
|
||||
}
|
||||
}
|
||||
}
|
||||
75
src/MaksIT.Results/Result.Information.cs
Normal file
75
src/MaksIT.Results/Result.Information.cs
Normal file
@ -0,0 +1,75 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the initial part of a request has been received and the client should continue with the request.
|
||||
/// Corresponds to HTTP status code 100 Continue.
|
||||
/// </summary>
|
||||
public static Result Continue(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.Continue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is switching to a different protocol as requested by the client.
|
||||
/// Corresponds to HTTP status code 101 Switching Protocols.
|
||||
/// </summary>
|
||||
public static Result SwitchingProtocols(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.SwitchingProtocols);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server has received and is processing the request, but no response is available yet.
|
||||
/// Corresponds to HTTP status code 102 Processing.
|
||||
/// </summary>
|
||||
public static Result Processing(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.Processing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is sending information about early hints that may be used by the client to begin preloading resources while the server prepares a final response.
|
||||
/// Corresponds to HTTP status code 103 Early Hints.
|
||||
/// </summary>
|
||||
public static Result EarlyHints(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), (HttpStatusCode)103); // Early Hints is not defined in HttpStatusCode enum, 103 is the official code
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public partial class Result<T> : Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the initial part of a request has been received and the client should continue with the request.
|
||||
/// Corresponds to HTTP status code 100 Continue.
|
||||
/// </summary>
|
||||
public static Result<T> Continue(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.Continue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is switching to a different protocol as requested by the client.
|
||||
/// Corresponds to HTTP status code 101 Switching Protocols.
|
||||
/// </summary>
|
||||
public static Result<T> SwitchingProtocols(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.SwitchingProtocols);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server has received and is processing the request, but no response is available yet.
|
||||
/// Corresponds to HTTP status code 102 Processing.
|
||||
/// </summary>
|
||||
public static Result<T> Processing(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.Processing);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the server is sending information about early hints that may be used by the client to begin preloading resources while the server prepares a final response.
|
||||
/// Corresponds to HTTP status code 103 Early Hints.
|
||||
/// </summary>
|
||||
public static Result<T> EarlyHints(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), (HttpStatusCode)103); // Early Hints is not defined in HttpStatusCode enum, 103 is the official code
|
||||
}
|
||||
}
|
||||
}
|
||||
138
src/MaksIT.Results/Result.Redirection.cs
Normal file
138
src/MaksIT.Results/Result.Redirection.cs
Normal file
@ -0,0 +1,138 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request has multiple options, and the user or user-agent should select one of them.
|
||||
/// Corresponds to HTTP status code 300 Multiple Choices.
|
||||
/// </summary>
|
||||
public static Result MultipleChoices(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.MultipleChoices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has been permanently moved to a new URI.
|
||||
/// Corresponds to HTTP status code 301 Moved Permanently.
|
||||
/// </summary>
|
||||
public static Result MovedPermanently(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.MovedPermanently);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource resides temporarily under a different URI.
|
||||
/// Corresponds to HTTP status code 302 Found.
|
||||
/// </summary>
|
||||
public static Result Found(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.Found);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the response to the request can be found under another URI using the GET method.
|
||||
/// Corresponds to HTTP status code 303 See Other.
|
||||
/// </summary>
|
||||
public static Result SeeOther(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.SeeOther);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has not been modified since the last request.
|
||||
/// Corresponds to HTTP status code 304 Not Modified.
|
||||
/// </summary>
|
||||
public static Result NotModified(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.NotModified);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource must be accessed through the proxy given by the location field.
|
||||
/// Corresponds to HTTP status code 305 Use Proxy.
|
||||
/// </summary>
|
||||
public static Result UseProxy(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.UseProxy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource resides temporarily under a different URI, but future requests should still use the original URI.
|
||||
/// Corresponds to HTTP status code 307 Temporary Redirect.
|
||||
/// </summary>
|
||||
public static Result TemporaryRedirect(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.TemporaryRedirect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has been permanently moved to a new URI, and future references should use the new URI.
|
||||
/// Corresponds to HTTP status code 308 Permanent Redirect.
|
||||
/// </summary>
|
||||
public static Result PermanentRedirect(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.PermanentRedirect);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the request has multiple options, and the user or user-agent should select one of them.
|
||||
/// Corresponds to HTTP status code 300 Multiple Choices.
|
||||
/// </summary>
|
||||
public static Result<T> MultipleChoices(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.MultipleChoices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has been permanently moved to a new URI.
|
||||
/// Corresponds to HTTP status code 301 Moved Permanently.
|
||||
/// </summary>
|
||||
public static Result<T> MovedPermanently(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.MovedPermanently);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource resides temporarily under a different URI.
|
||||
/// Corresponds to HTTP status code 302 Found.
|
||||
/// </summary>
|
||||
public static Result<T> Found(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.Found);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the response to the request can be found under another URI using the GET method.
|
||||
/// Corresponds to HTTP status code 303 See Other.
|
||||
/// </summary>
|
||||
public static Result<T> SeeOther(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.SeeOther);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has not been modified since the last request.
|
||||
/// Corresponds to HTTP status code 304 Not Modified.
|
||||
/// </summary>
|
||||
public static Result<T> NotModified(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.NotModified);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource must be accessed through the proxy given by the location field.
|
||||
/// Corresponds to HTTP status code 305 Use Proxy.
|
||||
/// </summary>
|
||||
public static Result<T> UseProxy(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.UseProxy);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource resides temporarily under a different URI, but future requests should still use the original URI.
|
||||
/// Corresponds to HTTP status code 307 Temporary Redirect.
|
||||
/// </summary>
|
||||
public static Result<T> TemporaryRedirect(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.TemporaryRedirect);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating that the requested resource has been permanently moved to a new URI, and future references should use the new URI.
|
||||
/// Corresponds to HTTP status code 308 Permanent Redirect.
|
||||
/// </summary>
|
||||
public static Result<T> PermanentRedirect(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.PermanentRedirect);
|
||||
}
|
||||
}
|
||||
}
|
||||
186
src/MaksIT.Results/Result.ServerError.cs
Normal file
186
src/MaksIT.Results/Result.ServerError.cs
Normal file
@ -0,0 +1,186 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server encountered an unexpected condition that prevented it from fulfilling the request.
|
||||
/// Corresponds to HTTP status code 500 Internal Server Error.
|
||||
/// </summary>
|
||||
public static Result InternalServerError(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server does not support the functionality required to fulfill the request.
|
||||
/// Corresponds to HTTP status code 501 Not Implemented.
|
||||
/// </summary>
|
||||
public static Result NotImplemented(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.NotImplemented);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server, while acting as a gateway or proxy, received an invalid response from the upstream server.
|
||||
/// Corresponds to HTTP status code 502 Bad Gateway.
|
||||
/// </summary>
|
||||
public static Result BadGateway(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.BadGateway);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server is currently unable to handle the request due to temporary overload or maintenance of the server.
|
||||
/// Corresponds to HTTP status code 503 Service Unavailable.
|
||||
/// </summary>
|
||||
public static Result ServiceUnavailable(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.ServiceUnavailable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
|
||||
/// Corresponds to HTTP status code 504 Gateway Timeout.
|
||||
/// </summary>
|
||||
public static Result GatewayTimeout(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.GatewayTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server does not support the HTTP protocol version used in the request.
|
||||
/// Corresponds to HTTP status code 505 HTTP Version Not Supported.
|
||||
/// </summary>
|
||||
public static Result HttpVersionNotSupported(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.HttpVersionNotSupported);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process.
|
||||
/// Corresponds to HTTP status code 506 Variant Also Negotiates.
|
||||
/// </summary>
|
||||
public static Result VariantAlsoNegotiates(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.VariantAlsoNegotiates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server is unable to store the representation needed to complete the request.
|
||||
/// Corresponds to HTTP status code 507 Insufficient Storage.
|
||||
/// </summary>
|
||||
public static Result InsufficientStorage(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.InsufficientStorage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server detected an infinite loop while processing a request with depth: infinity. Usually encountered in WebDAV scenarios.
|
||||
/// Corresponds to HTTP status code 508 Loop Detected.
|
||||
/// </summary>
|
||||
public static Result LoopDetected(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.LoopDetected);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating further extensions to the request are required for the server to fulfill it.
|
||||
/// Corresponds to HTTP status code 510 Not Extended.
|
||||
/// </summary>
|
||||
public static Result NotExtended(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.NotExtended);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the client needs to authenticate to gain network access.
|
||||
/// Corresponds to HTTP status code 511 Network Authentication Required.
|
||||
/// </summary>
|
||||
public static Result NetworkAuthenticationRequired(params string[] messages) {
|
||||
return new Result(false, new List<string>(messages), HttpStatusCode.NetworkAuthenticationRequired);
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server encountered an unexpected condition that prevented it from fulfilling the request.
|
||||
/// Corresponds to HTTP status code 500 Internal Server Error.
|
||||
/// </summary>
|
||||
public static Result<T> InternalServerError(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.InternalServerError);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server does not support the functionality required to fulfill the request.
|
||||
/// Corresponds to HTTP status code 501 Not Implemented.
|
||||
/// </summary>
|
||||
public static Result<T> NotImplemented(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.NotImplemented);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server, while acting as a gateway or proxy, received an invalid response from the upstream server.
|
||||
/// Corresponds to HTTP status code 502 Bad Gateway.
|
||||
/// </summary>
|
||||
public static Result<T> BadGateway(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.BadGateway);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server is currently unable to handle the request due to temporary overload or maintenance of the server.
|
||||
/// Corresponds to HTTP status code 503 Service Unavailable.
|
||||
/// </summary>
|
||||
public static Result<T> ServiceUnavailable(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.ServiceUnavailable);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server, while acting as a gateway or proxy, did not receive a timely response from the upstream server.
|
||||
/// Corresponds to HTTP status code 504 Gateway Timeout.
|
||||
/// </summary>
|
||||
public static Result<T> GatewayTimeout(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.GatewayTimeout);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server does not support the HTTP protocol version used in the request.
|
||||
/// Corresponds to HTTP status code 505 HTTP Version Not Supported.
|
||||
/// </summary>
|
||||
public static Result<T> HttpVersionNotSupported(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.HttpVersionNotSupported);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server has an internal configuration error: the chosen variant resource is configured to engage in transparent content negotiation itself, and is therefore not a proper end point in the negotiation process.
|
||||
/// Corresponds to HTTP status code 506 Variant Also Negotiates.
|
||||
/// </summary>
|
||||
public static Result<T> VariantAlsoNegotiates(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.VariantAlsoNegotiates);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server is unable to store the representation needed to complete the request.
|
||||
/// Corresponds to HTTP status code 507 Insufficient Storage.
|
||||
/// </summary>
|
||||
public static Result<T> InsufficientStorage(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.InsufficientStorage);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the server detected an infinite loop while processing a request with depth: infinity. Usually encountered in WebDAV scenarios.
|
||||
/// Corresponds to HTTP status code 508 Loop Detected.
|
||||
/// </summary>
|
||||
public static Result<T> LoopDetected(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.LoopDetected);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating further extensions to the request are required for the server to fulfill it.
|
||||
/// Corresponds to HTTP status code 510 Not Extended.
|
||||
/// </summary>
|
||||
public static Result<T> NotExtended(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.NotExtended);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the client needs to authenticate to gain network access.
|
||||
/// Corresponds to HTTP status code 511 Network Authentication Required.
|
||||
/// </summary>
|
||||
public static Result<T> NetworkAuthenticationRequired(T? value, params string[] messages) {
|
||||
return new Result<T>(value, false, new List<string>(messages), HttpStatusCode.NetworkAuthenticationRequired);
|
||||
}
|
||||
}
|
||||
}
|
||||
169
src/MaksIT.Results/Result.Succes.cs
Normal file
169
src/MaksIT.Results/Result.Succes.cs
Normal file
@ -0,0 +1,169 @@
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server returned the requested data.
|
||||
/// Corresponds to HTTP status code 200 OK.
|
||||
/// </summary>
|
||||
public static Result Ok(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and a new resource was created.
|
||||
/// Corresponds to HTTP status code 201 Created.
|
||||
/// </summary>
|
||||
public static Result Created(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.Created);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request has been accepted for processing, but the processing is not yet complete.
|
||||
/// Corresponds to HTTP status code 202 Accepted.
|
||||
/// </summary>
|
||||
public static Result Accepted(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.Accepted);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful but the response contains metadata from a source other than the origin server.
|
||||
/// Corresponds to HTTP status code 203 Non-Authoritative Information.
|
||||
/// </summary>
|
||||
public static Result NonAuthoritativeInformation(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.NonAuthoritativeInformation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful but there is no content to send in the response.
|
||||
/// Corresponds to HTTP status code 204 No Content.
|
||||
/// </summary>
|
||||
public static Result NoContent(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.NoContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful, but the user-agent should reset the document view that caused the request.
|
||||
/// Corresponds to HTTP status code 205 Reset Content.
|
||||
/// </summary>
|
||||
public static Result ResetContent(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.ResetContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server is delivering only part of the resource due to a range header sent by the client.
|
||||
/// Corresponds to HTTP status code 206 Partial Content.
|
||||
/// </summary>
|
||||
public static Result PartialContent(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.PartialContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the response contains multiple status codes, typically used for WebDAV.
|
||||
/// Corresponds to HTTP status code 207 Multi-Status.
|
||||
/// </summary>
|
||||
public static Result MultiStatus(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.MultiStatus);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the information has already been reported in a previous response.
|
||||
/// Corresponds to HTTP status code 208 Already Reported.
|
||||
/// </summary>
|
||||
public static Result AlreadyReported(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), HttpStatusCode.AlreadyReported);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server fulfilled the request for the resource using the delta encoding method.
|
||||
/// Corresponds to HTTP status code 226 IM Used.
|
||||
/// </summary>
|
||||
public static Result IMUsed(params string[] messages) {
|
||||
return new Result(true, new List<string>(messages), (HttpStatusCode)226); // 226 is the official status code for IM Used
|
||||
}
|
||||
}
|
||||
public partial class Result<T> : Result {
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server returned the requested data.
|
||||
/// Corresponds to HTTP status code 200 OK.
|
||||
/// </summary>
|
||||
public static Result<T> Ok(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.OK);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and a new resource was created.
|
||||
/// Corresponds to HTTP status code 201 Created.
|
||||
/// </summary>
|
||||
public static Result<T> Created(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.Created);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request has been accepted for processing, but the processing is not yet complete.
|
||||
/// Corresponds to HTTP status code 202 Accepted.
|
||||
/// </summary>
|
||||
public static Result<T> Accepted(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.Accepted);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful but the response contains metadata from a source other than the origin server.
|
||||
/// Corresponds to HTTP status code 203 Non-Authoritative Information.
|
||||
/// </summary>
|
||||
public static Result<T> NonAuthoritativeInformation(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.NonAuthoritativeInformation);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful but there is no content to send in the response.
|
||||
/// Corresponds to HTTP status code 204 No Content.
|
||||
/// </summary>
|
||||
public static Result<T> NoContent(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.NoContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful, but the user-agent should reset the document view that caused the request.
|
||||
/// Corresponds to HTTP status code 205 Reset Content.
|
||||
/// </summary>
|
||||
public static Result<T> ResetContent(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.ResetContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server is delivering only part of the resource due to a range header sent by the client.
|
||||
/// Corresponds to HTTP status code 206 Partial Content.
|
||||
/// </summary>
|
||||
public static Result<T> PartialContent(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.PartialContent);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the response contains multiple status codes, typically used for WebDAV.
|
||||
/// Corresponds to HTTP status code 207 Multi-Status.
|
||||
/// </summary>
|
||||
public static Result<T> MultiStatus(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.MultiStatus);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the information has already been reported in a previous response.
|
||||
/// Corresponds to HTTP status code 208 Already Reported.
|
||||
/// </summary>
|
||||
public static Result<T> AlreadyReported(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), HttpStatusCode.AlreadyReported);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a result indicating the request was successful and the server fulfilled the request for the resource using the delta encoding method.
|
||||
/// Corresponds to HTTP status code 226 IM Used.
|
||||
/// </summary>
|
||||
public static Result<T> IMUsed(T? value, params string[] messages) {
|
||||
return new Result<T>(value, true, new List<string>(messages), (HttpStatusCode)226); // 226 is the official status code for IM Used
|
||||
}
|
||||
}
|
||||
}
|
||||
71
src/MaksIT.Results/Result.cs
Normal file
71
src/MaksIT.Results/Result.cs
Normal file
@ -0,0 +1,71 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.Results {
|
||||
|
||||
public partial class Result {
|
||||
public bool IsSuccess { get; }
|
||||
public List<string> Messages { get; }
|
||||
public HttpStatusCode StatusCode { get; }
|
||||
|
||||
protected Result(bool isSuccess, List<string> messages, HttpStatusCode statusCode) {
|
||||
IsSuccess = isSuccess;
|
||||
Messages = messages ?? new List<string>();
|
||||
StatusCode = statusCode;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current Result to an IActionResult.
|
||||
/// </summary>
|
||||
/// <returns>IActionResult that represents the HTTP response.</returns>
|
||||
public virtual IActionResult ToActionResult() {
|
||||
if (IsSuccess) {
|
||||
return new StatusCodeResult((int)StatusCode);
|
||||
}
|
||||
else {
|
||||
var problemDetails = new ProblemDetails {
|
||||
Status = (int)StatusCode,
|
||||
Title = "An error occurred",
|
||||
Detail = string.Join("; ", Messages),
|
||||
Instance = null // You can customize the instance URI if needed
|
||||
};
|
||||
return new ObjectResult(problemDetails) { StatusCode = (int)StatusCode };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public partial class Result<T> : Result {
|
||||
public T? Value { get; }
|
||||
|
||||
protected Result(T? value, bool isSuccess, List<string> messages, HttpStatusCode statusCode)
|
||||
: base(isSuccess, messages, statusCode) {
|
||||
Value = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="Result{U}"/> by applying a transformation function to the current value.
|
||||
/// </summary>
|
||||
/// <typeparam name="U">The type of the new value.</typeparam>
|
||||
/// <param name="newValueFunc">A function that transforms the current value to the new value, which can be null.</param>
|
||||
/// <returns>A new <see cref="Result{U}"/> object containing the transformed value, along with the original success status, messages, and status code.</returns>
|
||||
public Result<U?> WithNewValue<U>(Func<T?, U?> newValueFunc) {
|
||||
return new Result<U?>(newValueFunc(Value), IsSuccess, Messages, StatusCode);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the current Result<T> to an IActionResult.
|
||||
/// </summary>
|
||||
/// <returns>IActionResult that represents the HTTP response.</returns>
|
||||
public override IActionResult ToActionResult() {
|
||||
if (IsSuccess) {
|
||||
if (Value is not null) {
|
||||
return new ObjectResult(Value) { StatusCode = (int)StatusCode };
|
||||
}
|
||||
return ToActionResult();
|
||||
}
|
||||
else {
|
||||
return ToActionResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user