add "me" endpoint
This commit is contained in:
parent
98b38f645f
commit
ae1a8d2b97
7 changed files with 49 additions and 10 deletions
|
|
@ -1,6 +1,9 @@
|
|||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using SurveyBackend.Core.Contexts;
|
||||
using SurveyBackend.Core.Services;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.DTOs.User;
|
||||
using SurveyBackend.Mappers;
|
||||
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
||||
|
||||
|
|
@ -14,14 +17,19 @@ namespace SurveyBackend.Controllers;
|
|||
public class AuthController : ControllerBase
|
||||
{
|
||||
private readonly IAuthorizationService _authorizationService;
|
||||
private readonly IUserContext _userContext;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
/// <summary>
|
||||
/// Нет ну вы прикалываетесь что ли мне ща каждый контроллер описывать?
|
||||
/// </summary>
|
||||
/// <param name="authorizationService"></param>
|
||||
public AuthController(IAuthorizationService authorizationService)
|
||||
public AuthController(IAuthorizationService authorizationService, IUserContext userContext,
|
||||
IUserService userService)
|
||||
{
|
||||
_authorizationService = authorizationService;
|
||||
_userContext = userContext;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -55,7 +63,19 @@ public class AuthController : ControllerBase
|
|||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||
{
|
||||
var token = await _authorizationService.RegisterUser(
|
||||
AuthMapper.UserRegistrationToModel(registerData));
|
||||
UserMapper.UserRegistrationToModel(registerData));
|
||||
return Ok(new { token = token });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("me")]
|
||||
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetMe()
|
||||
{
|
||||
var userId = _userContext.UserId;
|
||||
var user = await _userService.GetUserById(userId);
|
||||
var result = UserMapper.ModelToOutput(user);
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
namespace SurveyBackend.DTOs;
|
||||
namespace SurveyBackend.DTOs.User;
|
||||
|
||||
/// <summary>
|
||||
/// Схема авторизации пользователя
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
namespace SurveyBackend.DTOs.User;
|
||||
|
||||
public class UserOutputDto
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public required string FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
namespace SurveyBackend.DTOs;
|
||||
namespace SurveyBackend.DTOs.User;
|
||||
|
||||
/// <summary>
|
||||
/// Схема регистрации пользователя
|
||||
|
|
@ -10,11 +10,6 @@ public record UserRegistrationDto
|
|||
/// </summary>
|
||||
public required string Email { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Юзернейм
|
||||
/// </summary>
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Имя
|
||||
/// </summary>
|
||||
|
|
@ -1,12 +1,13 @@
|
|||
using SurveyBackend.Core.Models;
|
||||
using SurveyBackend.DTOs;
|
||||
using SurveyBackend.DTOs.User;
|
||||
|
||||
namespace SurveyBackend.Mappers;
|
||||
|
||||
/// <summary>
|
||||
/// Маппер всего связанного с авторизацией
|
||||
/// </summary>
|
||||
public static class AuthMapper
|
||||
public static class UserMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// Перегнать схему регистрации в нового юзера
|
||||
|
|
@ -20,4 +21,12 @@ public static class AuthMapper
|
|||
LastName = dto.LastName,
|
||||
Password = dto.Password,
|
||||
};
|
||||
|
||||
public static UserOutputDto ModelToOutput(User model) => new UserOutputDto
|
||||
{
|
||||
Id = model.Id,
|
||||
FirstName = model.FirstName,
|
||||
LastName = model.LastName,
|
||||
Email = model.Email,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue