Расширил описание методов в сваггере

parent 962d7eab
...@@ -3,11 +3,13 @@ using Payment.WebApi.Enums; ...@@ -3,11 +3,13 @@ using Payment.WebApi.Enums;
using Payment.WebApi.Models.DbModels; using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Models.RequestModels; using Payment.WebApi.Models.RequestModels;
using Payment.WebApi.Repositories.Interfaces; using Payment.WebApi.Repositories.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
namespace Payment.WebApi.Controllers; namespace Payment.WebApi.Controllers;
[ApiController] [ApiController]
[Route("Users/[action]")] [Route("Users/[action]")]
[Produces("applications/json")]
public class UserController : Controller public class UserController : Controller
{ {
private readonly IUserRepository _userRepository; private readonly IUserRepository _userRepository;
...@@ -19,15 +21,25 @@ public class UserController : Controller ...@@ -19,15 +21,25 @@ public class UserController : Controller
[HttpGet] [HttpGet]
[ActionName("GetById")] [ActionName("GetById")]
public ActionResult<User> GetUser(int id) [ProducesResponseType(200)]
[ProducesResponseType(204)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Получение данных по пользователю по его айди. Если пользователь не найден, то возвращается 204 статус",
OperationId = "GetByUserId",
Tags = new []{"Users"}
)
]
public ActionResult<UserResponseModel> GetUser(int id)
{ {
try try
{ {
var user = _userRepository.GetFirstOrDefaultById(id); var user = _userRepository.GetFirstOrDefaultById(id);
if (user is null) if (user is null)
return NotFound(); return NoContent();
return Ok(user); return Ok(UserMap(user));
} }
catch (Exception e) catch (Exception e)
{ {
...@@ -37,6 +49,15 @@ public class UserController : Controller ...@@ -37,6 +49,15 @@ public class UserController : Controller
[HttpPost] [HttpPost]
[ActionName("Create")] [ActionName("Create")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Создание пользователя",
OperationId = "CreateUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Create(UserRequestModel requestModel) public ActionResult<DefaultResponse> Create(UserRequestModel requestModel)
{ {
try try
...@@ -64,6 +85,15 @@ public class UserController : Controller ...@@ -64,6 +85,15 @@ public class UserController : Controller
[HttpPut] [HttpPut]
[ActionName("Edit")] [ActionName("Edit")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Редактирование данных пользователя, пока что без почты",
OperationId = "UpdateUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Edit(UserRequestModel requestModel) public ActionResult<DefaultResponse> Edit(UserRequestModel requestModel)
{ {
try try
...@@ -91,6 +121,15 @@ public class UserController : Controller ...@@ -91,6 +121,15 @@ public class UserController : Controller
[HttpDelete] [HttpDelete]
[ActionName("Delete")] [ActionName("Delete")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Деактивация пользователя. К деактивированным пользователям имеет доступ только администратор системы",
OperationId = "DeleteUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Delete(int id) public ActionResult<DefaultResponse> Delete(int id)
{ {
var user = _userRepository.GetFirstOrDefaultById(id); var user = _userRepository.GetFirstOrDefaultById(id);
...@@ -117,4 +156,21 @@ public class UserController : Controller ...@@ -117,4 +156,21 @@ public class UserController : Controller
user.Age = user.Age; user.Age = user.Age;
return user; return user;
} }
private static UserResponseModel UserMap(User user)
{
return new UserResponseModel
{
FirstName = user.FirstName,
LastName = user.LastName,
MiddleName = user.MiddleName,
Age = user.Age,
Email = user.Email,
Balances = user.Balances.Select(s => new UserBalancesResponseModel
{
Currency = s.Currency,
Amount = s.Amount
}).ToList()
};
}
} }
\ No newline at end of file
namespace Payment.WebApi.Models.RequestModels;
public class UserResponseModel
{
public string FirstName { get; set; }
public string? LastName { get; set; }
public string? MiddleName { get; set; }
public string Email { get; set; }
public int Age { get; set; }
public List<UserBalancesResponseModel> Balances { get; set; }
}
public class UserBalancesResponseModel
{
public decimal Amount { get; set; }
public string Currency { get; set; }
}
\ No newline at end of file
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
</PackageReference> </PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" /> <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
......
...@@ -9,7 +9,10 @@ var services = builder.Services; ...@@ -9,7 +9,10 @@ var services = builder.Services;
services.AddControllers(); services.AddControllers();
services.AddEndpointsApiExplorer(); services.AddEndpointsApiExplorer();
services.AddSwaggerGen(); services.AddSwaggerGen(s =>
{
s.EnableAnnotations();
});
services.AddAppServices(builder.Configuration); services.AddAppServices(builder.Configuration);
var app = builder.Build(); var app = builder.Build();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment