223 lines
7.8 KiB
C#
223 lines
7.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
|
using Serilog;
|
|
using Landmark.OLA.Application.DataAccess;
|
|
using Landmark.OLA.Models.Loan;
|
|
using Landmark.OLA.Models.Loan.Validators;
|
|
|
|
namespace Landmark.OLA.Application.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class BranchConfigController : ControllerBase
|
|
{
|
|
private readonly IBranchDataAccess branchStorage;
|
|
|
|
public BranchConfigController(
|
|
IBranchDataAccess branchDataAccess)
|
|
{
|
|
branchStorage = branchDataAccess;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to get the branch configuration
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a full branch configuration.
|
|
/// Cookies: None required or set.
|
|
/// Security: None.
|
|
/// </remarks>
|
|
// GET: api/BranchConfig
|
|
[HttpGet(Name = "GetBranchConfig")]
|
|
[ProducesResponseType(typeof(BranchConfig), 200)]
|
|
[ProducesResponseType(404)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult Get()
|
|
{
|
|
try
|
|
{
|
|
BranchConfig config = branchStorage.GetBranchConfig();
|
|
return Ok(config);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int) System.Net.HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to get the branch names configuration
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a full branch configuration.
|
|
/// Cookies: None required or set.
|
|
/// Security: None.
|
|
/// </remarks>
|
|
// GET: api/BranchNameConfig
|
|
[HttpGet("BranchNameConfig", Name = "GetBranchNameConfig")]
|
|
[ProducesResponseType(typeof(BranchNameConfig), 200)]
|
|
[ProducesResponseType(404)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult GetBranchNameConfig()
|
|
{
|
|
try
|
|
{
|
|
BranchNameConfig config = branchStorage.GetBranchNameConfig();
|
|
return Ok(config);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)System.Net.HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to save the initial branch config.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a saved branch config 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/BranchConfig
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(string), 201)]
|
|
[ProducesResponseType(403)]
|
|
[ProducesResponseType(typeof(ModelStateDictionary), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public object Post([FromBody] BranchConfig branchConfig)
|
|
{
|
|
try
|
|
{
|
|
if (branchConfig != null)
|
|
{
|
|
var validator = new BranchConfigValidator();
|
|
var validatorResult = validator.Validate(branchConfig);
|
|
|
|
var catchError = "";
|
|
if (!validatorResult.IsValid)
|
|
{
|
|
foreach (var error in validatorResult.Errors)
|
|
{
|
|
catchError += error;
|
|
ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
|
|
}
|
|
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
branchConfig.Id = (Guid.NewGuid()).ToString();
|
|
branchConfig.Date = DateTime.Now;
|
|
branchStorage.CreateBranchConfig(branchConfig);
|
|
|
|
HttpContext.Response.StatusCode = 201;
|
|
return new {id = branchConfig.Id, message = "Successfully saved document"};
|
|
}
|
|
|
|
return BadRequest("Branch Config is required.");
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int) System.Net.HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use update the branch config
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a status after attempting to update the branch configuration.
|
|
/// Cookies: None required or set.
|
|
/// Security: Must have a valid JWT token retrieved from Azure AD, with ADMIN role.
|
|
/// </remarks>
|
|
// PUT: api/Storage
|
|
[HttpPut]
|
|
[ProducesResponseType(typeof(string), 200)]
|
|
[ProducesResponseType(typeof(ModelStateDictionary), 400)]
|
|
[ProducesResponseType(403)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public IActionResult Put([FromBody] BranchConfig branchConfig)
|
|
{
|
|
try
|
|
{
|
|
var validator = new BranchConfigValidator();
|
|
var validatorResult = validator.Validate(branchConfig);
|
|
|
|
if (!validatorResult.IsValid)
|
|
{
|
|
foreach (var error in validatorResult.Errors)
|
|
{
|
|
ModelState.AddModelError(error.PropertyName, error.ErrorMessage);
|
|
}
|
|
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
var cosmosId = branchConfig?.Id;
|
|
if (!string.IsNullOrEmpty(cosmosId))
|
|
{
|
|
branchConfig.Date = DateTime.Now;
|
|
branchStorage.UpdateBranchConfig(branchConfig);
|
|
return CreatedAtAction(nameof(Put), new {id = branchConfig.Id}, branchConfig);
|
|
}
|
|
|
|
return BadRequest("Branch config invalid.");
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
Log.Error(ex.Message);
|
|
return NotFound();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int) System.Net.HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Use to get an updated branch names configuration
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Returns a full branch names configuration after updating it internally.
|
|
/// Cookies: None required or set.
|
|
/// Security: None.
|
|
/// </remarks>
|
|
// GET: api/UpdateBranchNameConfig
|
|
[HttpGet("UpdateBranchNameConfig", Name = "GetUpdateBranchNameConfig")]
|
|
[ProducesResponseType(typeof(BranchNameConfig), 200)]
|
|
[ProducesResponseType(404)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(500)]
|
|
public IActionResult UpdateBranchNameConfig()
|
|
{
|
|
try
|
|
{
|
|
branchStorage.UpdateBranchNameConfig();
|
|
BranchNameConfig config = branchStorage.GetBranchNameConfig();
|
|
return Ok(config);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
Log.Error(ex.Message);
|
|
return NotFound();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error(e.Message);
|
|
return StatusCode((int)System.Net.HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
} |