forked from ddrilling/AsbCloudServer
ControllerBase. Add BadRequestBuilder and extentionMethod.
This commit is contained in:
parent
800cf932ca
commit
6f83e8c7cc
@ -1,7 +1,9 @@
|
|||||||
using AsbCloudApp.Data;
|
using AsbCloudApp.Data;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace AsbCloudWebApi
|
namespace Microsoft.AspNetCore.Mvc
|
||||||
{
|
{
|
||||||
public static class Extentions
|
public static class Extentions
|
||||||
{
|
{
|
||||||
@ -26,5 +28,58 @@ namespace AsbCloudWebApi
|
|||||||
? uid
|
? uid
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static IActionResult MakeBadRequest(this ControllerBase controller, string paramName, params string[] errors)
|
||||||
|
{
|
||||||
|
return controller.BadRequest(new
|
||||||
|
{
|
||||||
|
name = paramName,
|
||||||
|
errors,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static BadRequestBuilder BadRequestBuilder(this ControllerBase controller, string paramName, params string[] errors)
|
||||||
|
=> new BadRequestBuilder(paramName, errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class BadRequestBuilder
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, List<string>> body;
|
||||||
|
|
||||||
|
private List<string> GetOrCreateNew(string paramName)
|
||||||
|
{
|
||||||
|
List<string> par;
|
||||||
|
if (body.ContainsKey(paramName))
|
||||||
|
par = body[paramName];
|
||||||
|
else
|
||||||
|
{
|
||||||
|
par = new List<string>();
|
||||||
|
body[paramName] = par;
|
||||||
|
}
|
||||||
|
return par;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestBuilder(string paramName, params string[] errors)
|
||||||
|
{
|
||||||
|
body = new();
|
||||||
|
body[paramName] = new List<string>(errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestBuilder Add(string paramName, params string[] errors)
|
||||||
|
{
|
||||||
|
var par = GetOrCreateNew(paramName);
|
||||||
|
par.AddRange(errors);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadRequestObjectResult Build()
|
||||||
|
{
|
||||||
|
var o = body.Select(e => new { name = e.Key, errors = e.Value.ToArray() });
|
||||||
|
|
||||||
|
return new BadRequestObjectResult(o);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static implicit operator BadRequestObjectResult(BadRequestBuilder d) => d.Build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using AsbCloudApp.Services;
|
using AsbCloudApp.Services;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
Loading…
Reference in New Issue
Block a user