using back.DataModels; using back.DTO; using back.services.bussines; using back.services.bussines.UserService; using Mapster; using Microsoft.AspNetCore.Mvc; 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(IUserService user) : ControllerBase { private readonly IUserService _user = user; // GET: api/ //[HttpGet] //public async Task>> Get([FromQuery] int page = 1, [FromQuery] int pageSize = 20) //{ // var users = await _userContext.GetPage(page, pageSize); // var totalItems = await _userContext.GetTotalItems(); // Response.Headers.Append("X-Total-Count", totalItems.ToString()); // return Ok(users); //} //// GET api//5 //[HttpGet("{id}")] //public async Task Get(Guid id) //{ // var user = await _userContext.GetById(id); // if (user == null) // return NotFound(); // return Ok(user); //} [HttpPost("[action]")] public async Task Login( [FromHeader(Name = "X-client-thumbprint")] string clientId, [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(Errors.BadRequest.Description); if (user.Email.Equals(DataModels.User.SystemUser.Email, 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.Adapt()); } var existingUser = await _user.Login(user.Email, user.Password, clientId); if (existingUser == null) return Unauthorized(Errors.Unauthorized.Description); return Ok(existingUser); } [HttpPost("forgot-password")] public async Task 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/ [HttpPost("[action]")] public async Task 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); } } }