Setup wallet and transaction retrieval, better swagger docs, and proper dtos

This commit is contained in:
2023-08-19 16:43:58 -04:00
parent 658bd7ca2a
commit dc9ab74598
16 changed files with 224 additions and 21 deletions

View File

@ -23,6 +23,8 @@ using System.Threading.Tasks;
using System.Linq;
using IO.Swagger.Services;
using System.Security.Claims;
using AutoMapper;
using Newtonsoft.Json.Linq;
namespace IO.Swagger.Controllers
{
@ -34,17 +36,20 @@ namespace IO.Swagger.Controllers
{
private readonly IUserRepository repository;
private readonly JwtService jwt;
private readonly IMapper mapper;
/// <summary>
/// The controller for the authotization endpoints
/// </summary>
/// <param name="repository"></param>
/// <param name="jwt"></param>
/// <param name="mapper"></param>
/// <exception cref="ArgumentNullException"></exception>
public AuthApiController(IUserRepository repository, JwtService jwt)
public AuthApiController(IUserRepository repository, JwtService jwt, IMapper mapper)
{
this.repository = repository ?? throw new ArgumentNullException(nameof(repository));
this.jwt = jwt ?? throw new ArgumentNullException(nameof(jwt));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
@ -57,13 +62,15 @@ namespace IO.Swagger.Controllers
[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)]
[ValidateModelState]
[SwaggerOperation("GetUserDetails")]
[ProducesResponseType(typeof(UserDto), 200)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> GetUserDetails()
{
var userIdString = HttpContext.User.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value;
if (!int.TryParse(userIdString, out int userId))
return Unauthorized();
var user = await repository.RetrieveUser(userId);
return user == null ? NoContent() : Ok(user);
return Ok(mapper.Map<UserDto>(user));
}
/// <summary>
@ -71,17 +78,21 @@ namespace IO.Swagger.Controllers
/// </summary>
/// <param name="body"></param>
/// <response code="200">Logged in successfully</response>
/// <response code="400">Bad Request</response>
/// <response code="401">Unauthorized</response>
[HttpPost]
[Route("/v1/api/auth/login")]
[ValidateModelState]
[SwaggerOperation("LoginUser")]
[ProducesResponseType(typeof(TokenDto), 200)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(401)]
public virtual async Task<IActionResult> LoginUser([FromBody]AuthLoginBody body)
{
if (!ModelState.IsValid)
return BadRequest(ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)));
var user = await repository.LoginUser(body);
return user == null ? Unauthorized() : Ok(new { token = jwt.GenerateJwt(user.Id) });
return user == null ? Unauthorized() : Ok(new TokenDto{ Token = jwt.GenerateJwt(user.Id) });
}
/// <summary>
@ -96,13 +107,16 @@ namespace IO.Swagger.Controllers
[ValidateModelState]
[SwaggerOperation("RegisterUser")]
[ProducesResponseType(typeof(TokenDto), 200)]
[ProducesResponseType(typeof(IEnumerable<string>), 400)]
[ProducesResponseType(409)]
public async Task<IActionResult> RegisterUser([FromBody]AuthRegisterBody body)
{
if (!ModelState.IsValid)
return BadRequest(ModelState.Values.SelectMany(v => v.Errors.Select(e => e.ErrorMessage)));
var user = await repository.RegisterUser(body);
return user == null ? StatusCode(409) : Ok(new { token = jwt.GenerateJwt(user.Id) });
return user == null ? StatusCode(409) : Ok(new TokenDto{ Token = jwt.GenerateJwt(user.Id) });
}
}
}