diff --git a/Newsbot.Collector.Api/Controllers/AccountController.cs b/Newsbot.Collector.Api/Controllers/AccountController.cs new file mode 100644 index 0000000..b11be6a --- /dev/null +++ b/Newsbot.Collector.Api/Controllers/AccountController.cs @@ -0,0 +1,89 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Newsbot.Collector.Api.Domain.Requests; +using Newsbot.Collector.Api.Domain.Response; +using Newsbot.Collector.Api.Services; +using Newsbot.Collector.Domain.Dto; +using Newsbot.Collector.Domain.Entities; + +namespace Newsbot.Collector.Api.Controllers; + +[ApiController] +[Route("/api/account")] +public class AccountController : ControllerBase +{ + private IIdentityService _identityService; + + public AccountController(IIdentityService identityService) + { + _identityService = identityService; + } + + [HttpPost("register")] + public IActionResult Register([FromBody] RegisterUserRequest user) + { + if (!ModelState.IsValid) + { + return new BadRequestObjectResult(new AuthFailedResponse + { + Errors = ModelState.Values + .Select(x => x.Errors + .Select(y => y.ErrorMessage).FirstOrDefault()) + }); + } + + if (user.Email is null) + { + return new BadRequestResult(); + } + + if (user.Password is null) + { + return new BadRequestResult(); + } + + var response = _identityService.Register(user.Email, user.Password); + + if (!response.IsSuccessful) + { + return new BadRequestObjectResult( new AuthFailedResponse + { + Errors = response.ErrorMessage + }); + } + + return new OkObjectResult(new AuthSuccessfulResponse + { + Token = response.Token + }); + } + + [HttpPost("login")] + public IActionResult Login([FromBody] UserLoginRequest request) + { + if (request.Email is null) + { + return new BadRequestResult(); + } + + if (request.Password is null) + { + return new BadRequestResult(); + } + + var response = _identityService.Login(request.Email, request.Password); + + if (!response.IsSuccessful) + { + return new BadRequestObjectResult( new AuthFailedResponse + { + Errors = response.ErrorMessage + }); + } + + return new OkObjectResult(new AuthSuccessfulResponse + { + Token = response.Token + }); + } +} \ No newline at end of file diff --git a/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs b/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs index 891c195..79ecf83 100644 --- a/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs +++ b/Newsbot.Collector.Api/Domain/Response/AuthFailedResponse.cs @@ -2,5 +2,5 @@ namespace Newsbot.Collector.Api.Domain.Response; public class AuthFailedResponse { - public IEnumerable? Errors { get; set; } + public IEnumerable? Errors { get; set; } } \ No newline at end of file diff --git a/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj index a766358..969a610 100644 --- a/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj +++ b/Newsbot.Collector.Api/Newsbot.Collector.Api.csproj @@ -37,8 +37,4 @@ - - - - diff --git a/Newsbot.Collector.Api/Program.cs b/Newsbot.Collector.Api/Program.cs index b6d9ec4..a0f05c9 100644 --- a/Newsbot.Collector.Api/Program.cs +++ b/Newsbot.Collector.Api/Program.cs @@ -49,6 +49,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Configure Identity builder.Services.AddScoped(); diff --git a/Newsbot.Collector.Domain/Dto/UserNewDto.cs b/Newsbot.Collector.Domain/Dto/UserNewDto.cs new file mode 100644 index 0000000..676429e --- /dev/null +++ b/Newsbot.Collector.Domain/Dto/UserNewDto.cs @@ -0,0 +1,7 @@ +namespace Newsbot.Collector.Domain.Dto; + +public class UserNewDto +{ + public string? Username { get; set; } + public string? Password { get; set; } +} \ No newline at end of file diff --git a/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj index 0f6f10f..b355dd0 100644 --- a/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj +++ b/Newsbot.Collector.Domain/Newsbot.Collector.Domain.csproj @@ -7,7 +7,9 @@ + +