From 1caaa4759e1dee14ef08ca28146bce6fc0348509 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 16 Feb 2024 23:56:45 +0300 Subject: [PATCH] refactor: rewrite the api for the dto project --- .../Attributes/BadRequestResponseAttribute.cs | 6 ++--- .../Attributes/NotFoundResponseAttribute.cs | 6 ++--- Endpoint/Controllers/V1/CampusController.cs | 26 +++++++++++++++---- .../Controllers/V1/DisciplineController.cs | 26 ++++++++++++++----- Endpoint/Endpoint.csproj | 1 + Endpoint/Models/ErrorResponseVm.cs | 18 ------------- 6 files changed, 47 insertions(+), 36 deletions(-) delete mode 100644 Endpoint/Models/ErrorResponseVm.cs diff --git a/Endpoint/Common/Attributes/BadRequestResponseAttribute.cs b/Endpoint/Common/Attributes/BadRequestResponseAttribute.cs index b81f692..85f9ed8 100644 --- a/Endpoint/Common/Attributes/BadRequestResponseAttribute.cs +++ b/Endpoint/Common/Attributes/BadRequestResponseAttribute.cs @@ -1,9 +1,9 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Mirea.Api.Endpoint.Models; +using Mirea.Api.Dto.Responses; using System; -namespace Mirea.Api.Endpoint.Common.Extensions; +namespace Mirea.Api.Endpoint.Common.Attributes; [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] -public class BadRequestResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponseVm), StatusCodes.Status400BadRequest); \ No newline at end of file +public class BadRequestResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponse), StatusCodes.Status400BadRequest); \ No newline at end of file diff --git a/Endpoint/Common/Attributes/NotFoundResponseAttribute.cs b/Endpoint/Common/Attributes/NotFoundResponseAttribute.cs index 8f05a69..39527ea 100644 --- a/Endpoint/Common/Attributes/NotFoundResponseAttribute.cs +++ b/Endpoint/Common/Attributes/NotFoundResponseAttribute.cs @@ -1,9 +1,9 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Mirea.Api.Endpoint.Models; +using Mirea.Api.Dto.Responses; using System; -namespace Mirea.Api.Endpoint.Common.Extensions; +namespace Mirea.Api.Endpoint.Common.Attributes; [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] -public class NotFoundResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponseVm), StatusCodes.Status404NotFound); \ No newline at end of file +public class NotFoundResponseAttribute() : ProducesResponseTypeAttribute(typeof(ErrorResponse), StatusCodes.Status404NotFound); \ No newline at end of file diff --git a/Endpoint/Controllers/V1/CampusController.cs b/Endpoint/Controllers/V1/CampusController.cs index c383608..4ee93c8 100644 --- a/Endpoint/Controllers/V1/CampusController.cs +++ b/Endpoint/Controllers/V1/CampusController.cs @@ -3,7 +3,10 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusBasicInfoList; using Mirea.Api.DataAccess.Application.Cqrs.Campus.Queries.GetCampusDetails; -using Mirea.Api.Endpoint.Common.Extensions; +using Mirea.Api.Dto.Responses; +using Mirea.Api.Endpoint.Common.Attributes; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace Mirea.Api.Endpoint.Controllers.V1 @@ -16,11 +19,18 @@ namespace Mirea.Api.Endpoint.Controllers.V1 /// Basic information about campuses. [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] - public async Task> GetBasicInfo() + public async Task>> GetBasicInfo() { var result = await mediator.Send(new GetCampusBasicInfoListQuery()); - return Ok(result); + return Ok(result.Campuses + .Select(c => new CampusBasicInfoResponse() + { + Id = c.Id, + CodeName = c.CodeName, + FullName = c.FullName + }) + ); } /// @@ -32,14 +42,20 @@ namespace Mirea.Api.Endpoint.Controllers.V1 [ProducesResponseType(StatusCodes.Status200OK)] [BadRequestResponse] [NotFoundResponse] - public async Task> GetDetails(int id) + public async Task> GetDetails(int id) { var result = await mediator.Send(new GetCampusDetailsQuery() { Id = id }); - return Ok(result); + return Ok(new CampusDetailsResponse() + { + Id = result.Id, + CodeName = result.CodeName, + FullName = result.FullName, + Address = result.Address + }); } } } diff --git a/Endpoint/Controllers/V1/DisciplineController.cs b/Endpoint/Controllers/V1/DisciplineController.cs index 6589d31..056b208 100644 --- a/Endpoint/Controllers/V1/DisciplineController.cs +++ b/Endpoint/Controllers/V1/DisciplineController.cs @@ -3,7 +3,10 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineDetails; using Mirea.Api.DataAccess.Application.Cqrs.Discipline.Queries.GetDisciplineList; -using Mirea.Api.Endpoint.Common.Extensions; +using Mirea.Api.Dto.Responses; +using Mirea.Api.Endpoint.Common.Attributes; +using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; namespace Mirea.Api.Endpoint.Controllers.V1 @@ -19,7 +22,7 @@ namespace Mirea.Api.Endpoint.Controllers.V1 [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [BadRequestResponse] - public async Task> Get([FromQuery] int? page, [FromQuery] int? pageSize) + public async Task>> Get([FromQuery] int? page, [FromQuery] int? pageSize) { var result = await mediator.Send(new GetDisciplineListQuery() { @@ -27,27 +30,36 @@ namespace Mirea.Api.Endpoint.Controllers.V1 PageSize = pageSize }); - return Ok(result); + return Ok(result.Disciplines + .Select(d => new DisciplineResponse() + { + Id = d.Id, + Name = d.Name + }) + ); } /// /// Gets details of a specific discipline by ID. /// /// Discipline ID. - /// Details of the specified discipline. - /// + /// Details of the specified discipline. [HttpGet("{id:int}")] [ProducesResponseType(StatusCodes.Status200OK)] [BadRequestResponse] [NotFoundResponse] - public async Task> GetDetails(int id) + public async Task> GetDetails(int id) { var result = await mediator.Send(new GetDisciplineInfoQuery() { Id = id }); - return Ok(result); + return Ok(new DisciplineResponse() + { + Id = result.Id, + Name = result.Name + }); } } } diff --git a/Endpoint/Endpoint.csproj b/Endpoint/Endpoint.csproj index 84dcfc2..0191a2e 100644 --- a/Endpoint/Endpoint.csproj +++ b/Endpoint/Endpoint.csproj @@ -28,6 +28,7 @@ + diff --git a/Endpoint/Models/ErrorResponseVm.cs b/Endpoint/Models/ErrorResponseVm.cs deleted file mode 100644 index d409ae1..0000000 --- a/Endpoint/Models/ErrorResponseVm.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Mirea.Api.Endpoint.Models; - -/// -/// 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