140 lines
3.9 KiB
C#
140 lines
3.9 KiB
C#
using BlueWest.Data;
|
|
using BlueWest.WebApi.Context;
|
|
using BlueWest.WebApi.Context.Users;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BlueWest.WebApi.Controllers
|
|
{
|
|
/// <inheritdoc />
|
|
[ApiController]
|
|
[Route("application/users")]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
[Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
|
|
[EnableCors(Constants.CorsPolicyName)]
|
|
|
|
public class ApplicationUserController : ControllerBase
|
|
{
|
|
private readonly ApplicationUserDbContext _dbContext;
|
|
|
|
/// <inheritdoc />
|
|
public ApplicationUserController(ApplicationUserDbContext context)
|
|
{
|
|
_dbContext = context;
|
|
}
|
|
|
|
#region Users
|
|
|
|
/// <summary>
|
|
/// Get Application users
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet]
|
|
public ActionResult GetApplicationUsers(
|
|
int skip = 0,
|
|
int take = 50,
|
|
int orderDir = 1)
|
|
{
|
|
|
|
var (success, users) = _dbContext.GetUsers( skip, take, orderDir);
|
|
if (!success) return new NotFoundResult();
|
|
return Ok(users);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Updates a User
|
|
/// </summary>
|
|
/// <param name="UserCode">The UserId ISO 3166 code</param>
|
|
/// <param name="UserToUpdate">User payload data</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[HttpPut("{UserCode}")]
|
|
public ActionResult UpdateApplicationUser(string applicationUserId, ApplicationUserUnique UserToUpdate)
|
|
{
|
|
var (success, updatedUser) = _dbContext.UpdateApplicationUser(UserToUpdate, applicationUserId);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(updatedUser);
|
|
}
|
|
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
/*
|
|
#region GetUserById
|
|
|
|
/// <summary>
|
|
/// Get User by Id
|
|
/// </summary>
|
|
/// <param name="UserId">ISO 3166-1 UserId numeric code</param>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("{UserId}", Name = nameof(GetUserById))]
|
|
public ActionResult GetUserById(int UserId)
|
|
{
|
|
var (success, User) = _dbContext.GetOneUserById(UserId);
|
|
|
|
if (success)
|
|
{
|
|
return Ok(User);
|
|
}
|
|
|
|
return new NotFoundResult();
|
|
}
|
|
|
|
#endregion
|
|
*/
|
|
|
|
#region Roles
|
|
|
|
/// <summary>
|
|
/// Get Application users
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[HttpGet("roles")]
|
|
public ActionResult GetApplicationRoles(
|
|
int skip = 0,
|
|
int take = 50,
|
|
int orderDir = 1)
|
|
{
|
|
|
|
var (success, users) = _dbContext.GetRoles( skip, take, orderDir);
|
|
if (!success) return new NotFoundResult();
|
|
return Ok(users);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Application Constants
|
|
/// </summary>
|
|
public static class Constants
|
|
{
|
|
/// <summary>
|
|
/// Policy Name
|
|
/// </summary>
|
|
public const string CorsPolicyName = "_myAllowSpecificOrigins";
|
|
}
|
|
|