transactions
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
using back.context;
|
||||
using back.DataModels;
|
||||
using back.DataModels;
|
||||
using back.services.bussines;
|
||||
using back.services.bussines.UserService;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace back.controllers;
|
||||
|
||||
public record UserLoginFromModel(string Email, string Password, string? SystemKey);
|
||||
public record ForgotPasswordFromModel(string Email);
|
||||
public record RegisterFromModel(string Name, string Email, string Password);
|
||||
|
||||
[ApiController, Route("api/[controller]")]
|
||||
public class UsersController(UserContext userContext) : ControllerBase
|
||||
public class UsersController(IUserService user) : ControllerBase
|
||||
{
|
||||
private readonly UserContext _userContext = userContext;
|
||||
private readonly IUserService _user = user;
|
||||
// GET: api/<UsersController>
|
||||
//[HttpGet]
|
||||
//public async Task<ActionResult<IEnumerable<UserModel>>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
||||
@@ -28,27 +32,59 @@ public class UsersController(UserContext userContext) : ControllerBase
|
||||
// return Ok(user);
|
||||
//}
|
||||
|
||||
[HttpPost]
|
||||
[HttpPost("[action]")]
|
||||
public async Task<IActionResult> Login(
|
||||
[FromHeader(Name = "X-client-thumbprint")] string clientId,
|
||||
[FromBody] UserModel user
|
||||
[FromBody] UserLoginFromModel user
|
||||
)
|
||||
{
|
||||
if (string.IsNullOrEmpty(clientId))
|
||||
return BadRequest("Client ID cannot be null or empty");
|
||||
|
||||
if (user == null || string.IsNullOrEmpty(user.Email) || string.IsNullOrEmpty(user.Password))
|
||||
return BadRequest(UserContext.Errors.BadRequest.Description);
|
||||
var existingUser = await _userContext.Login(user.Email, user.Password, clientId);
|
||||
return BadRequest(Errors.BadRequest.Description);
|
||||
|
||||
if (user.Email.Equals("@system", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
if (string.IsNullOrEmpty(user.SystemKey))
|
||||
return Unauthorized(Errors.Unauthorized.Description);
|
||||
var systemUser = await _user.ValidateSystemUser(user.Email, user.Password, user.SystemKey, clientId);
|
||||
if (systemUser == null)
|
||||
return Unauthorized(Errors.Unauthorized.Description);
|
||||
return Ok(systemUser);
|
||||
}
|
||||
|
||||
var existingUser = await _user.Login(user.Email, user.Password, clientId);
|
||||
if (existingUser == null)
|
||||
return Unauthorized(UserContext.Errors.Unauthorized.Description);
|
||||
return Ok(existingUser.ToDto());
|
||||
return Unauthorized(Errors.Unauthorized.Description);
|
||||
return Ok(existingUser);
|
||||
}
|
||||
|
||||
//// POST api/<UsersController>
|
||||
//[HttpPost]
|
||||
//public async Task<IActionResult> Post([FromBody] UserModel user)
|
||||
//{
|
||||
// if (user == null)
|
||||
// return BadRequest("User cannot be null");
|
||||
// var createdUser = await _userContext.Create(user);
|
||||
// return CreatedAtAction(nameof(Get), new { id = createdUser.Id }, createdUser);
|
||||
//}
|
||||
[HttpPost("forgot-password")]
|
||||
public async Task<IActionResult> ForgotPassword([FromBody] ForgotPasswordFromModel user)
|
||||
{
|
||||
if (string.IsNullOrEmpty(user.Email))
|
||||
return BadRequest("Email cannot be null or empty");
|
||||
await _user.SendResetPassword(user.Email);
|
||||
return Ok("If the email exists, a reset password link has been sent.");
|
||||
}
|
||||
|
||||
// POST api/<UsersController>
|
||||
[HttpPost("[action]")]
|
||||
public async Task<IActionResult> Register(
|
||||
[FromHeader(Name = "X-client-thumbprint")] string clientId,
|
||||
[FromBody] RegisterFromModel user)
|
||||
{
|
||||
if (user == null)
|
||||
return BadRequest("User cannot be null");
|
||||
try
|
||||
{
|
||||
var createdUser = await _user.Create(clientId, new User() { Email = user.Email, Password = user.Password });
|
||||
return Created();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user