-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathBaseAdminConsoleController.cs
More file actions
68 lines (60 loc) · 2.94 KB
/
BaseAdminConsoleController.cs
File metadata and controls
68 lines (60 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using Bit.Core.AdminConsole.Utilities.v2;
using Bit.Core.AdminConsole.Utilities.v2.Results;
using Bit.Core.Models.Api;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
using CommandError = Bit.Core.AdminConsole.Utilities.v2.Error;
namespace Bit.Api.AdminConsole.Controllers;
public abstract class BaseAdminConsoleController : Controller
{
/// <summary>
/// Maps a void <see cref="CommandResult"/> to an HTTP response.
/// Returns 204 No Content on success, or the appropriate error status code on failure.
/// </summary>
protected static IResult Handle(CommandResult commandResult) =>
commandResult.Match<IResult>(
error => MapError(error),
_ => TypedResults.NoContent()
);
/// <summary>
/// Maps a <see cref="CommandResult{T}"/> to an HTTP response.
/// On success, delegates to <paramref name="success"/> so the caller can choose the response shape
/// (e.g. <c>TypedResults.Created</c> for POST, <c>TypedResults.Ok</c> for GET/PUT).
/// On failure, returns the appropriate error status code.
/// </summary>
protected static IResult Handle<T>(CommandResult<T> commandResult, Func<T, IResult> success) =>
commandResult.Match<IResult>(
error => MapError(error),
success
);
protected static class Error
{
public static NotFound<ErrorResponseModel> NotFound(string message = "Resource not found.") =>
TypedResults.NotFound(new ErrorResponseModel(message));
public static UnauthorizedHttpResult Unauthorized() =>
TypedResults.Unauthorized();
public static BadRequest<ErrorResponseModel> BadRequest(string message) =>
TypedResults.BadRequest(new ErrorResponseModel(message));
public static JsonHttpResult<ErrorResponseModel> InternalError(
string message = "Something went wrong with your request. Please contact support for assistance.") =>
TypedResults.Json(
new ErrorResponseModel(message),
statusCode: StatusCodes.Status500InternalServerError);
}
private static IResult MapError(CommandError error) =>
error switch
{
BadRequestError badRequest => TypedResults.BadRequest(new ErrorResponseModel(badRequest.Message)),
NotFoundError notFound => TypedResults.NotFound(new ErrorResponseModel(notFound.Message)),
ConflictError conflict => TypedResults.Json(
new ErrorResponseModel(conflict.Message),
statusCode: StatusCodes.Status409Conflict),
InternalError internalError => TypedResults.Json(
new ErrorResponseModel(internalError.Message),
statusCode: StatusCodes.Status500InternalServerError),
_ => TypedResults.Json(
new ErrorResponseModel(error.Message),
statusCode: StatusCodes.Status500InternalServerError
)
};
}