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.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using SurveyBackend.Core.Contexts;
|
||||||
|
using SurveyBackend.Core.Services;
|
||||||
using SurveyBackend.DTOs;
|
using SurveyBackend.DTOs;
|
||||||
|
using SurveyBackend.DTOs.User;
|
||||||
using SurveyBackend.Mappers;
|
using SurveyBackend.Mappers;
|
||||||
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
using IAuthorizationService = SurveyBackend.Core.Services.IAuthorizationService;
|
||||||
|
|
||||||
|
|
@ -14,14 +17,19 @@ namespace SurveyBackend.Controllers;
|
||||||
public class AuthController : ControllerBase
|
public class AuthController : ControllerBase
|
||||||
{
|
{
|
||||||
private readonly IAuthorizationService _authorizationService;
|
private readonly IAuthorizationService _authorizationService;
|
||||||
|
private readonly IUserContext _userContext;
|
||||||
|
private readonly IUserService _userService;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Нет ну вы прикалываетесь что ли мне ща каждый контроллер описывать?
|
/// Нет ну вы прикалываетесь что ли мне ща каждый контроллер описывать?
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="authorizationService"></param>
|
/// <param name="authorizationService"></param>
|
||||||
public AuthController(IAuthorizationService authorizationService)
|
public AuthController(IAuthorizationService authorizationService, IUserContext userContext,
|
||||||
|
IUserService userService)
|
||||||
{
|
{
|
||||||
_authorizationService = authorizationService;
|
_authorizationService = authorizationService;
|
||||||
|
_userContext = userContext;
|
||||||
|
_userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -55,7 +63,19 @@ public class AuthController : ControllerBase
|
||||||
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
public async Task<IActionResult> Register([FromBody] UserRegistrationDto registerData)
|
||||||
{
|
{
|
||||||
var token = await _authorizationService.RegisterUser(
|
var token = await _authorizationService.RegisterUser(
|
||||||
AuthMapper.UserRegistrationToModel(registerData));
|
UserMapper.UserRegistrationToModel(registerData));
|
||||||
return Ok(new { token = token });
|
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>
|
/// <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>
|
/// <summary>
|
||||||
/// Схема регистрации пользователя
|
/// Схема регистрации пользователя
|
||||||
|
|
@ -10,11 +10,6 @@ public record UserRegistrationDto
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public required string Email { get; set; }
|
public required string Email { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Юзернейм
|
|
||||||
/// </summary>
|
|
||||||
public string Username { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Имя
|
/// Имя
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
using SurveyBackend.Core.Models;
|
using SurveyBackend.Core.Models;
|
||||||
using SurveyBackend.DTOs;
|
using SurveyBackend.DTOs;
|
||||||
|
using SurveyBackend.DTOs.User;
|
||||||
|
|
||||||
namespace SurveyBackend.Mappers;
|
namespace SurveyBackend.Mappers;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Маппер всего связанного с авторизацией
|
/// Маппер всего связанного с авторизацией
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static class AuthMapper
|
public static class UserMapper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Перегнать схему регистрации в нового юзера
|
/// Перегнать схему регистрации в нового юзера
|
||||||
|
|
@ -20,4 +21,12 @@ public static class AuthMapper
|
||||||
LastName = dto.LastName,
|
LastName = dto.LastName,
|
||||||
Password = dto.Password,
|
Password = dto.Password,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
public static UserOutputDto ModelToOutput(User model) => new UserOutputDto
|
||||||
|
{
|
||||||
|
Id = model.Id,
|
||||||
|
FirstName = model.FirstName,
|
||||||
|
LastName = model.LastName,
|
||||||
|
Email = model.Email,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ namespace SurveyBackend.Core.Services;
|
||||||
public interface IUserService
|
public interface IUserService
|
||||||
{
|
{
|
||||||
public Task<User> GetUserByEmail(string email);
|
public Task<User> GetUserByEmail(string email);
|
||||||
|
public Task<User> GetUserById(int id);
|
||||||
public Task<bool> IsEmailTaken(string email);
|
public Task<bool> IsEmailTaken(string email);
|
||||||
public Task CreateUserAsync(User user);
|
public Task CreateUserAsync(User user);
|
||||||
}
|
}
|
||||||
|
|
@ -19,6 +19,11 @@ public class UserService : IUserService
|
||||||
return await _userRepository.GetUserByEmail(email) ?? throw new NotFoundException("Email not found");
|
return await _userRepository.GetUserByEmail(email) ?? throw new NotFoundException("Email not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<User> GetUserById(int id)
|
||||||
|
{
|
||||||
|
return await _userRepository.GetByIdAsync(id) ?? throw new NotFoundException("User not found");
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<bool> IsEmailTaken(string email)
|
public async Task<bool> IsEmailTaken(string email)
|
||||||
{
|
{
|
||||||
return await _userRepository.GetUserByEmail(email) != null;
|
return await _userRepository.GetUserByEmail(email) != null;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue