diff --git a/SurveyBackend/SurveyBackend.API/Controllers/AuthController.cs b/SurveyBackend/SurveyBackend.API/Controllers/AuthController.cs index a4ee704..e516621 100644 --- a/SurveyBackend/SurveyBackend.API/Controllers/AuthController.cs +++ b/SurveyBackend/SurveyBackend.API/Controllers/AuthController.cs @@ -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; /// /// Нет ну вы прикалываетесь что ли мне ща каждый контроллер описывать? /// /// - public AuthController(IAuthorizationService authorizationService) + public AuthController(IAuthorizationService authorizationService, IUserContext userContext, + IUserService userService) { _authorizationService = authorizationService; + _userContext = userContext; + _userService = userService; } /// @@ -55,7 +63,19 @@ public class AuthController : ControllerBase public async Task 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 GetMe() + { + var userId = _userContext.UserId; + var user = await _userService.GetUserById(userId); + var result = UserMapper.ModelToOutput(user); + return Ok(result); + } } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/UserLoginDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/User/UserLoginDto.cs similarity index 90% rename from SurveyBackend/SurveyBackend.API/DTOs/UserLoginDto.cs rename to SurveyBackend/SurveyBackend.API/DTOs/User/UserLoginDto.cs index 2cf7002..aa7a9b6 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/UserLoginDto.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/User/UserLoginDto.cs @@ -1,4 +1,4 @@ -namespace SurveyBackend.DTOs; +namespace SurveyBackend.DTOs.User; /// /// Схема авторизации пользователя diff --git a/SurveyBackend/SurveyBackend.API/DTOs/User/UserOutputDTO.cs b/SurveyBackend/SurveyBackend.API/DTOs/User/UserOutputDTO.cs new file mode 100644 index 0000000..814e91f --- /dev/null +++ b/SurveyBackend/SurveyBackend.API/DTOs/User/UserOutputDTO.cs @@ -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; } +} \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.API/DTOs/UserRegistrationDto.cs b/SurveyBackend/SurveyBackend.API/DTOs/User/UserRegistrationDto.cs similarity index 79% rename from SurveyBackend/SurveyBackend.API/DTOs/UserRegistrationDto.cs rename to SurveyBackend/SurveyBackend.API/DTOs/User/UserRegistrationDto.cs index b67622e..3ddcf05 100644 --- a/SurveyBackend/SurveyBackend.API/DTOs/UserRegistrationDto.cs +++ b/SurveyBackend/SurveyBackend.API/DTOs/User/UserRegistrationDto.cs @@ -1,4 +1,4 @@ -namespace SurveyBackend.DTOs; +namespace SurveyBackend.DTOs.User; /// /// Схема регистрации пользователя @@ -10,11 +10,6 @@ public record UserRegistrationDto /// public required string Email { get; set; } - /// - /// Юзернейм - /// - public string Username { get; set; } - /// /// Имя /// diff --git a/SurveyBackend/SurveyBackend.API/Mappers/AuthMapper.cs b/SurveyBackend/SurveyBackend.API/Mappers/UserMapper.cs similarity index 68% rename from SurveyBackend/SurveyBackend.API/Mappers/AuthMapper.cs rename to SurveyBackend/SurveyBackend.API/Mappers/UserMapper.cs index 47a01e6..0d9a216 100644 --- a/SurveyBackend/SurveyBackend.API/Mappers/AuthMapper.cs +++ b/SurveyBackend/SurveyBackend.API/Mappers/UserMapper.cs @@ -1,12 +1,13 @@ using SurveyBackend.Core.Models; using SurveyBackend.DTOs; +using SurveyBackend.DTOs.User; namespace SurveyBackend.Mappers; /// /// Маппер всего связанного с авторизацией /// -public static class AuthMapper +public static class UserMapper { /// /// Перегнать схему регистрации в нового юзера @@ -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, + }; } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Core/Services/IUserService.cs b/SurveyBackend/SurveyBackend.Core/Services/IUserService.cs index cd34c76..47828ec 100644 --- a/SurveyBackend/SurveyBackend.Core/Services/IUserService.cs +++ b/SurveyBackend/SurveyBackend.Core/Services/IUserService.cs @@ -5,6 +5,7 @@ namespace SurveyBackend.Core.Services; public interface IUserService { public Task GetUserByEmail(string email); + public Task GetUserById(int id); public Task IsEmailTaken(string email); public Task CreateUserAsync(User user); } \ No newline at end of file diff --git a/SurveyBackend/SurveyBackend.Services/Services/UserService.cs b/SurveyBackend/SurveyBackend.Services/Services/UserService.cs index 9c52a64..9336478 100644 --- a/SurveyBackend/SurveyBackend.Services/Services/UserService.cs +++ b/SurveyBackend/SurveyBackend.Services/Services/UserService.cs @@ -19,6 +19,11 @@ public class UserService : IUserService return await _userRepository.GetUserByEmail(email) ?? throw new NotFoundException("Email not found"); } + public async Task GetUserById(int id) + { + return await _userRepository.GetByIdAsync(id) ?? throw new NotFoundException("User not found"); + } + public async Task IsEmailTaken(string email) { return await _userRepository.GetUserByEmail(email) != null;