DamageAssessment_Backend/DamageAssesmentApi/DamageAssesment.Api.UsersAccess/Controllers/UsersAccessController.cs
2023-11-22 18:34:38 -05:00

128 lines
4.2 KiB
C#

using DamageAssesment.Api.UsersAccess.Interfaces;
using DamageAssesment.Api.UsersAccess.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace DamageAssesment.Api.UsersAccess.Controllers
{
[ApiController]
public class UsersAccessController : ControllerBase
{
private IUsersAccessProvider userAccessProvider;
public UsersAccessController(IUsersAccessProvider userAccessProvider)
{
this.userAccessProvider = userAccessProvider;
}
// [HttpPost("dadeschooltoken")]
// public async Task<ActionResult> DadeSchoolAuthenticateAsync(string username, string password)
//{
// var result = await userAccessProvider.DadeSchoolAuthenticateAsync(username, password);
// if (result.IsSuccess)
// {
// return Ok(result.TokenResponse);
// }
// return Unauthorized(result.ErrorMessage);
// }
// [Authorize(Policy = "Dadeschools")]
[HttpPost("token/{employecode}")]
public async Task<ActionResult> AuthenticateAsync(string employecode)
{
var result = await userAccessProvider.AuthenticateAsync(employecode);
if (result.IsSuccess)
{
return Ok(result.TokenResponse);
}
return Unauthorized(result.ErrorMessage);
}
// [Authorize(Policy = "Dadeschools")]
[HttpPost("refreshtoken")]
public async Task<ActionResult> RefreshTokenAsync(TokenResponse tokenResponse)
{
var result = await userAccessProvider.RefreshTokenAsync(tokenResponse);
if (result.IsSuccess)
{
return Ok(result.TokenResponse);
}
return Unauthorized(result.ErrorMessage);
}
// [Authorize(Policy = "DamageApp", Roles ="admin")]
[HttpGet("users")]
public async Task<ActionResult> GetUsersAsync()
{
var result = await userAccessProvider.GetUsersAsync();
if (result.IsSuccess)
{
return Ok(result.Users);
}
return NoContent();
}
// [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpGet("users/{Id}")]
public async Task<ActionResult> GetUsersAsync(int Id)
{
var result = await userAccessProvider.GetUsersAsync(Id);
if (result.IsSuccess)
{
return Ok(result.User);
}
return NotFound();
}
// [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpGet("roles")]
public async Task<ActionResult> GetRolesAsync()
{
var result = await userAccessProvider.GetRolesAsync();
if (result.IsSuccess)
{
return Ok(result.Roles);
}
return NoContent();
}
// [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpPost("users")]
public async Task<ActionResult> PostUserAsync(User user)
{
var result = await userAccessProvider.PostUserAsync(user);
if (result.IsSuccess)
{
return Ok(result.User);
}
return BadRequest(result.ErrorMessage);
}
// [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpPut("users/{Id}")]
public async Task<ActionResult> 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);
}
// [Authorize(Policy = "DamageApp", Roles = "admin")]
[HttpDelete("users/{Id}")]
public async Task<ActionResult> DeleteUserAsync(int Id)
{
var result = await userAccessProvider.DeleteUserAsync(Id);
if (result.IsSuccess)
{
return Ok(result.User);
}
return NotFound();
}
}
}