From 77136cc7ec44bae019f44a5d44da51002ee5489f Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:33:53 +0300 Subject: [PATCH 1/6] 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 From 7b584b2dc5ebc02e2e68124e77bf5de780e7cbe7 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:34:33 +0300 Subject: [PATCH 2/6] feat: add a basic controller --- Endpoint/Controllers/BaseController.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Endpoint/Controllers/BaseController.cs diff --git a/Endpoint/Controllers/BaseController.cs b/Endpoint/Controllers/BaseController.cs new file mode 100644 index 0000000..82c4c7a --- /dev/null +++ b/Endpoint/Controllers/BaseController.cs @@ -0,0 +1,7 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Controllers; + +[ApiController] +[Route("api/[controller]/[action]")] +public class BaseController : ControllerBase; \ No newline at end of file From 335298fd910359fb960b55eb1f309a7059c8ba83 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:34:56 +0300 Subject: [PATCH 3/6] feat: add a basic controller for API version 1 --- Endpoint/Controllers/V1/BaseControllerV1.cs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Endpoint/Controllers/V1/BaseControllerV1.cs diff --git a/Endpoint/Controllers/V1/BaseControllerV1.cs b/Endpoint/Controllers/V1/BaseControllerV1.cs new file mode 100644 index 0000000..6ebe1f3 --- /dev/null +++ b/Endpoint/Controllers/V1/BaseControllerV1.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Controllers.V1; + +[ApiVersion("1.0")] +[Produces("application/json")] +[Route("api/v{version:apiVersion}/[controller]/[action]")] +public class BaseControllerV1 : BaseController; \ No newline at end of file From a4dfccbc229737ae4df526322342ed1ea84d768c Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:33:53 +0300 Subject: [PATCH 4/6] 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 From fb6ca5df7715b5e95fb8c226606a2b738d7a5922 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:34:33 +0300 Subject: [PATCH 5/6] feat: add a basic controller --- Endpoint/Controllers/BaseController.cs | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Endpoint/Controllers/BaseController.cs diff --git a/Endpoint/Controllers/BaseController.cs b/Endpoint/Controllers/BaseController.cs new file mode 100644 index 0000000..82c4c7a --- /dev/null +++ b/Endpoint/Controllers/BaseController.cs @@ -0,0 +1,7 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Controllers; + +[ApiController] +[Route("api/[controller]/[action]")] +public class BaseController : ControllerBase; \ No newline at end of file From 8852351ca63044f3a377acd84d4b6b5ea3d7fa96 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Sun, 28 Jan 2024 04:34:56 +0300 Subject: [PATCH 6/6] feat: add a basic controller for API version 1 --- Endpoint/Controllers/V1/BaseControllerV1.cs | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Endpoint/Controllers/V1/BaseControllerV1.cs diff --git a/Endpoint/Controllers/V1/BaseControllerV1.cs b/Endpoint/Controllers/V1/BaseControllerV1.cs new file mode 100644 index 0000000..6ebe1f3 --- /dev/null +++ b/Endpoint/Controllers/V1/BaseControllerV1.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Mirea.Api.Endpoint.Controllers.V1; + +[ApiVersion("1.0")] +[Produces("application/json")] +[Route("api/v{version:apiVersion}/[controller]/[action]")] +public class BaseControllerV1 : BaseController; \ No newline at end of file