166 lines
6.2 KiB
C#
166 lines
6.2 KiB
C#
using System;
|
|
using System.Net;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Serilog;
|
|
using Landmark.OLA.Application.Common.Domain;
|
|
using Landmark.OLA.Application.DataAccess;
|
|
|
|
|
|
namespace Landmark.OLA.Application.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class UiSettingsController : ControllerBase
|
|
{
|
|
private readonly UiSettings _uiSettings;
|
|
private readonly IBranchDataAccess _branchStorage;
|
|
private readonly IConfigurationAccess _configurationAccess;
|
|
|
|
public UiSettingsController(UiSettings uiSettings,
|
|
IBranchDataAccess branchDataAccess,
|
|
IConfigurationAccess configurationAccess)
|
|
{
|
|
_uiSettings = uiSettings;
|
|
_branchStorage = branchDataAccess;
|
|
_configurationAccess = configurationAccess;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to retrieve UI Settings
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns the UI Settings; with the dynamic branch mappings injected.
|
|
/// Cookies: None required or set.
|
|
/// Security: None required or provided.
|
|
/// </remarks>
|
|
// GET: api/Content/
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(UiSettings), 200)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult Get()
|
|
{
|
|
try
|
|
{
|
|
var settings = _uiSettings;
|
|
settings.BranchMapping = _branchStorage.GetBranchConfig();
|
|
settings.BranchNameMapping = _branchStorage.GetBranchNameConfig();
|
|
settings.Configuration = _configurationAccess.GetStoredSettings();
|
|
return Ok(settings);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)HttpStatusCode.InternalServerError);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to get stored config (run-time changeable)
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns the current StoredConfig
|
|
/// Cookies: None required or set.
|
|
/// Security: Requires Azure AD token and Admin role.
|
|
/// </remarks>
|
|
// GET: api/UiSettings/StoredSettings
|
|
[HttpGet("StoredSettings", Name = "GetStoredSettings")]
|
|
[ProducesResponseType(typeof(StoredSettings), 200)]
|
|
[ProducesResponseType(500)]
|
|
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public IActionResult GetStoredSettings()
|
|
{
|
|
try
|
|
{
|
|
var config = _configurationAccess.GetStoredSettings();
|
|
return Ok(config);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to save the initial storedConfig.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a saved configuration with id. This call will fail if ANY configs currently exist.
|
|
/// Cookies: None required or set.
|
|
/// Security: Requires Azure AD Jwt token and admin role.
|
|
/// </remarks>
|
|
// POST: api/UiSettings/StoredSettings
|
|
[HttpPost("StoredSettings", Name = "CreateStoredSettings")]
|
|
[ProducesResponseType(typeof(string), 201)]
|
|
[ProducesResponseType(403)]
|
|
[ProducesResponseType(typeof(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public object CreateStoredSettings([FromBody] StoredSettings config)
|
|
{
|
|
try
|
|
{
|
|
if (config != null)
|
|
{
|
|
config.Id = (Guid.NewGuid()).ToString();
|
|
config.LastUpdated = DateTime.Now;
|
|
_configurationAccess.CreateStoredSettings(config);
|
|
|
|
HttpContext.Response.StatusCode = 201;
|
|
return new { id = config.Id, message = "Successfully saved configuration" };
|
|
}
|
|
return BadRequest("StoredConfig is required.");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use update the StoredConfig
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a status after attempting to update the StoredConfig.
|
|
/// Cookies: None required or set.
|
|
/// Security: Must have a valid JWT token retrieved from Azure AD, with ADMIN role.
|
|
/// </remarks>
|
|
// PUT: api/UiSettings/StoredSettings
|
|
[HttpPut("StoredSettings", Name = "UpdateStoredSettings")]
|
|
[ProducesResponseType(typeof(string), 200)]
|
|
[ProducesResponseType(typeof(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary), 400)]
|
|
[ProducesResponseType(403)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public IActionResult UpdateStoredSettings([FromBody] StoredSettings config)
|
|
{
|
|
try
|
|
{
|
|
var cosmosId = config?.Id;
|
|
if (!String.IsNullOrEmpty(cosmosId))
|
|
{
|
|
config.LastUpdated = DateTime.Now;
|
|
_configurationAccess.UpdateStoredSettings(config);
|
|
return CreatedAtAction(nameof(UpdateStoredSettings), new { id = config.Id }, config);
|
|
}
|
|
return BadRequest("StoredSettings invalid.");
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
Log.Error(ex.Message);
|
|
return NotFound();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
}
|