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

parent 962d7eab
......@@ -3,11 +3,13 @@ using Payment.WebApi.Enums;
using Payment.WebApi.Models.DbModels;
using Payment.WebApi.Models.RequestModels;
using Payment.WebApi.Repositories.Interfaces;
using Swashbuckle.AspNetCore.Annotations;
namespace Payment.WebApi.Controllers;
[ApiController]
[Route("Users/[action]")]
[Produces("applications/json")]
public class UserController : Controller
{
private readonly IUserRepository _userRepository;
......@@ -19,15 +21,25 @@ public class UserController : Controller
[HttpGet]
[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
{
var user = _userRepository.GetFirstOrDefaultById(id);
if (user is null)
return NotFound();
return NoContent();
return Ok(user);
return Ok(UserMap(user));
}
catch (Exception e)
{
......@@ -37,6 +49,15 @@ public class UserController : Controller
[HttpPost]
[ActionName("Create")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Создание пользователя",
OperationId = "CreateUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Create(UserRequestModel requestModel)
{
try
......@@ -64,6 +85,15 @@ public class UserController : Controller
[HttpPut]
[ActionName("Edit")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Редактирование данных пользователя, пока что без почты",
OperationId = "UpdateUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Edit(UserRequestModel requestModel)
{
try
......@@ -74,7 +104,7 @@ public class UserController : Controller
{
ResponseStatusType = ResponseStatusType.NotFound
});
_userRepository.Update(UpdateUser(user, requestModel));
_userRepository.Save();
......@@ -91,6 +121,15 @@ public class UserController : Controller
[HttpDelete]
[ActionName("Delete")]
[ProducesResponseType(200)]
[ProducesResponseType(500)]
[SwaggerOperation
(
Description = "Деактивация пользователя. К деактивированным пользователям имеет доступ только администратор системы",
OperationId = "DeleteUser",
Tags = new []{"Users"}
)
]
public ActionResult<DefaultResponse> Delete(int id)
{
var user = _userRepository.GetFirstOrDefaultById(id);
......@@ -102,7 +141,7 @@ public class UserController : Controller
user.IsActive = false;
_userRepository.Save();
return Ok(new DefaultResponse
{
ResponseStatusType = ResponseStatusType.Ok
......@@ -117,4 +156,21 @@ public class UserController : Controller
user.Age = user.Age;
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 @@
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.5" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
......
......@@ -9,7 +9,10 @@ var services = builder.Services;
services.AddControllers();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
services.AddSwaggerGen(s =>
{
s.EnableAnnotations();
});
services.AddAppServices(builder.Configuration);
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