using DamageAssesment.Api.UsersAccess.Interfaces; using DamageAssesment.Api.UsersAccess.Models; using Microsoft.AspNetCore.Mvc; namespace DamageAssesment.Api.UsersAccess.Controllers { [Route("api")] [ApiController] public class UsersAccessController : ControllerBase { private IUsersAccessProvider userAccessProvider; public UsersAccessController(IUsersAccessProvider userAccessProvider) { this.userAccessProvider = userAccessProvider; } [HttpPost("authenticate")] public async Task AuthenticateAsync(UserCredentials userCredentials) { var result = await userAccessProvider.AuthenticateAsync(userCredentials); if (result.IsSuccess) { return Ok(result.TokenResponse); } return Unauthorized(result.ErrorMessage); } [HttpPost("refreshToken")] public async Task RefreshTokenAsync(TokenResponse tokenResponse) { var result = await userAccessProvider.RefreshTokenAsync(tokenResponse); if (result.IsSuccess) { return Ok(result.TokenResponse); } return Unauthorized(result.ErrorMessage); } [HttpGet("users")] public async Task GetUsersAsync() { var result = await userAccessProvider.GetUsersAsync(); if (result.IsSuccess) { return Ok(result.Users); } return NoContent(); } [HttpGet("users/{Id}")] public async Task GetUsersAsync(int Id) { var result = await userAccessProvider.GetUsersAsync(Id); if (result.IsSuccess) { return Ok(result.User); } return NotFound(); } [HttpPost("users")] public async Task PostUserAsync(User user) { var result = await userAccessProvider.PostUserAsync(user); if (result.IsSuccess) { return Ok(result.User); } return BadRequest(result.ErrorMessage); } [HttpPut("users/{Id}")] public async Task PutUserAsync(int Id, User user) { var result = await userAccessProvider.PutUserAsync(Id, user); if (result.IsSuccess) { return Ok(result.User); } if (result.ErrorMessage == "Not Found") return NotFound(result.ErrorMessage); return BadRequest(result.ErrorMessage); } [HttpDelete("users/{Id}")] public async Task DeleteSurveysAsync(int Id) { var result = await userAccessProvider.DeleteUserAsync(Id); if (result.IsSuccess) { return Ok(result.User); } return NotFound(); } } }