From 77136cc7ec44bae019f44a5d44da51002ee5489f Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:33:53 +0300 Subject: [PATCH] feat: add an extension to simplify attributes --- .../Extensions/BadRequestResponseAttribute.cs | 8 ++++++++ Endpoint/Common/Extensions/ErrorResponseVm.cs | 18 ++++++++++++++++++ .../Extensions/NotFoundResponseAttribute.cs | 8 ++++++++ 3 files changed, 34 insertions(+) create mode 100644 Endpoint/Common/Extensions/BadRequestResponseAttribute.cs create mode 100644 Endpoint/Common/Extensions/ErrorResponseVm.cs create mode 100644 Endpoint/Common/Extensions/NotFoundResponseAttribute.cs diff --git a/Endpoint/Common/Extensions/BadRequestResponseAttribute.cs b/Endpoint/Common/Extensions/BadRequestResponseAttribute.cs new file mode 100644 index 0000000..171da23 --- /dev/null +++ b/Endpoint/Common/Extensions/BadRequestResponseAttribute.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Http; +using System; +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Common.Extensions; + +[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] +public class BadRequestResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponseVm), StatusCodes.Status400BadRequest); \ No newline at end of file diff --git a/Endpoint/Common/Extensions/ErrorResponseVm.cs b/Endpoint/Common/Extensions/ErrorResponseVm.cs new file mode 100644 index 0000000..7c4a099 --- /dev/null +++ b/Endpoint/Common/Extensions/ErrorResponseVm.cs @@ -0,0 +1,18 @@ +namespace Mirea.Api.Endpoint.Common.Extensions; + +/// +/// A class for providing information about an error +/// +public class ErrorResponseVm +{ + /// + /// The text or translation code of the error. This field may not contain information in specific scenarios. + /// For example, it might be empty for HTTP 204 responses where no content is returned or if the validation texts have not been configured. + /// + public required string Error { get; set; } + /// + /// In addition to returning the response code in the header, it is also duplicated in this field. + /// Represents the HTTP response code. + /// + public required int Code { get; set; } +} \ No newline at end of file diff --git a/Endpoint/Common/Extensions/NotFoundResponseAttribute.cs b/Endpoint/Common/Extensions/NotFoundResponseAttribute.cs new file mode 100644 index 0000000..15db1cc --- /dev/null +++ b/Endpoint/Common/Extensions/NotFoundResponseAttribute.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Http; +using System; +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Common.Extensions; + +[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] +public class NotFoundResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponseVm), StatusCodes.Status404NotFound); \ No newline at end of file