fix login

This commit is contained in:
mfnefd 2024-06-19 22:30:34 +04:00
parent e109746059
commit 4d8c2c8a15

View File

@ -1,4 +1,5 @@
using BusinessLogic.BusinessLogic; using BusinessLogic.BusinessLogic;
using BusinessLogic.Tools.Mail.MailTemplates;
using Contracts.BindingModels; using Contracts.BindingModels;
using Contracts.BusinessLogicContracts; using Contracts.BusinessLogicContracts;
using Contracts.Exceptions; using Contracts.Exceptions;
@ -7,122 +8,124 @@ using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers namespace RestAPI.Controllers
{ {
[Route("[controller]/[action]")] [Route("[controller]/[action]")]
[ApiController] [ApiController]
public class UserController public class UserController
{ {
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly IUserLogic _userLogic; private readonly IUserLogic _userLogic;
public UserController(ILogger<UserController> logger, IUserLogic userLogic) public UserController(ILogger<UserController> logger, IUserLogic userLogic)
{ {
_userLogic = userLogic; _userLogic = userLogic;
_logger = logger; _logger = logger;
} }
[HttpPost] [HttpPost]
public IResult Login(string email, string password) public IResult Login([FromBody] UserData data)
{ {
try try
{ {
var res = _userLogic.Login(email, password); var res = _userLogic.Login(data.email, data.password);
return Results.Ok(res); return Results.Ok(res);
} }
catch (ElementNotFoundException ex) catch (ElementNotFoundException ex)
{ {
_logger.LogInformation(ex, "User not found"); _logger.LogInformation(ex, "User not found");
return Results.NoContent(); return Results.NoContent();
} }
catch (AccountException ex) catch (AccountException ex)
{ {
_logger.LogWarning(ex, "Wrong login data"); _logger.LogWarning(ex, "Wrong login data");
return Results.BadRequest(ex.Message); return Results.BadRequest(ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error get user"); _logger.LogError(ex, "Error get user");
return Results.Problem(ex.Message); return Results.Problem(ex.Message);
} }
} }
[HttpPost] [HttpPost]
public IResult Registration([FromBody] UserBindingModel model) public IResult Registration([FromBody] UserBindingModel model)
{ {
try try
{ {
var res = _userLogic.Create(model); var res = _userLogic.Create(model);
return Results.Ok(res); return Results.Ok(res);
} }
catch (AccountException ex) catch (AccountException ex)
{ {
_logger.LogWarning(ex, "Wrong registration data"); _logger.LogWarning(ex, "Wrong registration data");
return Results.BadRequest(ex.Message); return Results.BadRequest(ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error create user"); _logger.LogError(ex, "Error create user");
return Results.Problem(ex.Message); return Results.Problem(ex.Message);
} }
} }
[HttpGet] [HttpGet]
public IResult Get([FromQuery] UserSearchModel model) public IResult Get([FromQuery] UserSearchModel model)
{ {
try try
{ {
var res = _userLogic.ReadElement(model); var res = _userLogic.ReadElement(model);
return Results.Ok(res); return Results.Ok(res);
} }
catch (ElementNotFoundException ex) catch (ElementNotFoundException ex)
{ {
_logger.LogInformation(ex, "User not found"); _logger.LogInformation(ex, "User not found");
return Results.NoContent(); return Results.NoContent();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error get user"); _logger.LogError(ex, "Error get user");
return Results.Problem(ex.Message); return Results.Problem(ex.Message);
} }
} }
[HttpPatch] [HttpPatch]
public IResult Update([FromBody] UserBindingModel model) public IResult Update([FromBody] UserBindingModel model)
{ {
try try
{ {
var res = _userLogic.Update(model); var res = _userLogic.Update(model);
return Results.Ok(res); return Results.Ok(res);
} }
catch (AccountException ex) catch (AccountException ex)
{ {
_logger.LogWarning(ex, "Wrong update data"); _logger.LogWarning(ex, "Wrong update data");
return Results.BadRequest(ex.Message); return Results.BadRequest(ex.Message);
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error update user"); _logger.LogError(ex, "Error update user");
return Results.Problem(ex.Message); return Results.Problem(ex.Message);
} }
} }
[HttpDelete] [HttpDelete]
public IResult Delete(Guid id) public IResult Delete(Guid id)
{ {
try try
{ {
var res = _userLogic.Delete(new() { Id = id }); var res = _userLogic.Delete(new() { Id = id });
return Results.Ok(res); return Results.Ok(res);
} }
catch (ElementNotFoundException ex) catch (ElementNotFoundException ex)
{ {
_logger.LogInformation(ex, "User not found"); _logger.LogInformation(ex, "User not found");
return Results.NoContent(); return Results.NoContent();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Error delete user"); _logger.LogError(ex, "Error delete user");
return Results.Problem(ex.Message); return Results.Problem(ex.Message);
} }
} }
} }
public record class UserData(string email, string password);
} }