using BlueWest.Data.Application.Users; using BlueWest.Domain; 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 { /// [ApiController] [Route("application/users")] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)] [EnableCors(Constants.CorsPolicyName)] [ServiceFilter(typeof(SessionAuthorizationFilter))] public class ApplicationUserController : ControllerBase { private readonly ApplicationUserDbContext _dbContext; /// public ApplicationUserController(ApplicationUserDbContext context) { _dbContext = context; } #region Users /// /// Get Application users /// /// [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); } /// /// Updates a User /// /// The UserId ISO 3166 code /// User payload data /// [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 /// /// Get User by Id /// /// ISO 3166-1 UserId numeric code /// [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 /// /// Get Application users /// /// [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 } } /// /// Application Constants /// public static class Constants { /// /// Policy Name /// public const string CorsPolicyName = "_myAllowSpecificOrigins"; }