MRB webassembly
This commit is contained in:
401
MesaFabApproval.API/Controllers/ApprovalController.cs
Normal file
401
MesaFabApproval.API/Controllers/ApprovalController.cs
Normal file
@ -0,0 +1,401 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ApprovalController : ControllerBase {
|
||||
private readonly ILogger<ApprovalController> _logger;
|
||||
private readonly IApprovalService _approvalService;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public ApprovalController(ILogger<ApprovalController> logger, IApprovalService approvalService,
|
||||
IMonInWorkerClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_approvalService = approvalService ?? throw new ArgumentNullException("IApprovalService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("approval")]
|
||||
public async Task<IActionResult> CreateApproval(Approval approval) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to generate a new approval");
|
||||
|
||||
if (approval is null) throw new ArgumentNullException("Approval cannot be null");
|
||||
|
||||
await _approvalService.CreateApproval(approval);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot create new approval, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "CreateApproval";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("approval/issue")]
|
||||
public async Task<IActionResult> GetApprovalsForIssueId(int issueId, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get approvals for issue {issueId}");
|
||||
|
||||
if (issueId <= 0) throw new ArgumentException($"{issueId} is not a valid issue ID");
|
||||
|
||||
IEnumerable<Approval> approvals = await _approvalService.GetApprovalsForIssueId(issueId, bypassCache);
|
||||
|
||||
return Ok(approvals);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get approvals, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetApprovalsForIssueId";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("approval/user")]
|
||||
public async Task<IActionResult> GetApprovalsForUserId(int userId, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get approvals for user ID {userId}");
|
||||
|
||||
if (userId <= 0) throw new ArgumentException($"{userId} is not a valid user ID");
|
||||
|
||||
IEnumerable<Approval> approvals = await _approvalService.GetApprovalsForUserId(userId, bypassCache);
|
||||
|
||||
return Ok(approvals);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get approvals, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetApprovalsForUserId";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("approval/members")]
|
||||
public async Task<IActionResult> GetApprovalGroupMembers(int subRoleId) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get approval group members for group {subRoleId}");
|
||||
|
||||
if (subRoleId <= 0) throw new ArgumentException($"{subRoleId} is not a valid sub role ID");
|
||||
|
||||
IEnumerable<User> members = await _approvalService.GetApprovalGroupMembers(subRoleId);
|
||||
|
||||
return Ok(members);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get approval group members, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetApprovalsGroupMembers";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("approval")]
|
||||
public async Task<IActionResult> UpdateApproval(Approval approval) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to update approval");
|
||||
|
||||
if (approval is null) throw new ArgumentNullException($"approval cannot be null");
|
||||
|
||||
await _approvalService.UpdateApproval(approval);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot update approval, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "UpdateApproval";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("approval/approve")]
|
||||
public async Task<IActionResult> Approve(Approval approval) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"attempting to submit approval");
|
||||
|
||||
if (approval is null) throw new ArgumentNullException($"approval cannot be null");
|
||||
|
||||
await _approvalService.Approve(approval);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot approve, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "Approve";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("approval/deny")]
|
||||
public async Task<IActionResult> Deny(Approval approval) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"attempting to deny approval");
|
||||
|
||||
if (approval is null) throw new ArgumentNullException($"approval cannot be null");
|
||||
|
||||
await _approvalService.Deny(approval);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Approval denial failed, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "Deny";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("approval/roleId")]
|
||||
public async Task<IActionResult> GetRoleIdForRoleName(string roleName) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get role ID by role name");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(roleName)) throw new ArgumentException("role name cannot be null or empty");
|
||||
|
||||
int roleId = await _approvalService.GetRoleIdForRoleName(roleName);
|
||||
|
||||
return Ok(roleId);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get role ID, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetRoleIdForRoleName";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("approval/subRoles")]
|
||||
public async Task<IActionResult> GetSubRolesForSubRoleName(string subRoleName, int roleId) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get sub roles by sub role name");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(subRoleName)) throw new ArgumentException("sub role name cannot be null or empty");
|
||||
if (roleId <= 0) throw new ArgumentException($"{roleId} is not a valid role ID");
|
||||
|
||||
IEnumerable<SubRole> subRoles = await _approvalService.GetSubRolesForSubRoleName(subRoleName, roleId);
|
||||
|
||||
return Ok(subRoles);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get role ID, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetSubRoleIdForSubRoleName";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
178
MesaFabApproval.API/Controllers/AuthenticationController.cs
Normal file
178
MesaFabApproval.API/Controllers/AuthenticationController.cs
Normal file
@ -0,0 +1,178 @@
|
||||
using System.Security.Authentication;
|
||||
using System.Security.Principal;
|
||||
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using MesaFabApprovalAPI.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[AllowAnonymous]
|
||||
public class AuthenticationController : ControllerBase {
|
||||
private readonly ILogger<AuthenticationController> _logger;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
private readonly IAuthenticationService _authenticationService;
|
||||
|
||||
public AuthenticationController(ILogger<AuthenticationController> logger,
|
||||
IMonInWorkerClient monInClient,
|
||||
IAuthenticationService authenticationService) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
_authenticationService = authenticationService ??
|
||||
throw new ArgumentNullException("IAuthenticationService not injected");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("auth/login")]
|
||||
public async Task<IActionResult> Login(AuthAttempt login) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform authentication");
|
||||
|
||||
if (login is null) throw new ArgumentNullException("Login cannot be null");
|
||||
|
||||
LoginResult loginResult = await _authenticationService.AuthenticateUser(login);
|
||||
|
||||
if (loginResult.IsAuthenticated)
|
||||
return Ok(loginResult);
|
||||
|
||||
return Unauthorized();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot authenticate user, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "Login";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("auth/login/localWindows")]
|
||||
public async Task<IActionResult> LoginLocalWindows(WindowsIdentity identity) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform local Windows authentication");
|
||||
|
||||
if (identity is null) throw new ArgumentNullException("identity cannot be null");
|
||||
|
||||
LoginResult loginResult = await _authenticationService.AttemptLocalUserAuth(identity);
|
||||
|
||||
if (loginResult.IsAuthenticated)
|
||||
return Ok(loginResult);
|
||||
|
||||
return Unauthorized();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot authenticate local Windows user, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "LoginLocalWindows";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("auth/refresh")]
|
||||
public async Task<IActionResult> Refresh(AuthAttempt authAttempt) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to refresh auth tokens");
|
||||
|
||||
if (authAttempt is null) throw new ArgumentNullException("AuthAttempt cannot be null");
|
||||
if (authAttempt.AuthTokens is null) throw new ArgumentNullException("AuthTokens cannot be null");
|
||||
|
||||
LoginResult loginResult = await _authenticationService.RefreshAuthTokens(authAttempt);
|
||||
|
||||
return Ok(loginResult);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (AuthenticationException ex) {
|
||||
_logger.LogInformation($"Unable to refresh tokens, because {ex.Message}");
|
||||
return Unauthorized();
|
||||
} catch (Exception ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Cannot authenticate user, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "RefreshTokens";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpOptions]
|
||||
[Route("auth/refresh")]
|
||||
public IActionResult RefreshOptions() {
|
||||
try {
|
||||
_logger.LogInformation("Auth refresh options");
|
||||
|
||||
return Ok();
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError($"Error in auth refresh options. Exception: {ex.Message}");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
56
MesaFabApproval.API/Controllers/CAController.cs
Normal file
56
MesaFabApproval.API/Controllers/CAController.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class CAController : ControllerBase {
|
||||
private readonly ILogger<ApprovalController> _logger;
|
||||
private readonly ICAService _caService;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public CAController(ILogger<ApprovalController> logger, ICAService caService,
|
||||
IMonInWorkerClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_caService = caService ?? throw new ArgumentNullException("ICAService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("ca/isValidCANumber")]
|
||||
public async Task<IActionResult> IsValidCANumber(int number) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to determine if {number} is a valid CA#");
|
||||
|
||||
if (number <= 0) return Ok(false);
|
||||
|
||||
bool isValid = await _caService.IsValidCANumber(number);
|
||||
|
||||
return Ok(isValid);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot determine if {number} is a valid CA#, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "IsValidCANumber";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
57
MesaFabApproval.API/Controllers/CustomerController.cs
Normal file
57
MesaFabApproval.API/Controllers/CustomerController.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class CustomerController : ControllerBase {
|
||||
private readonly ILogger<CustomerController> _logger;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly ICustomerService _customerService;
|
||||
|
||||
public CustomerController(ILogger<CustomerController> logger, IMonInWorkerClient monInClient, IMemoryCache cache, ICustomerService customerService) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
|
||||
_customerService = customerService ?? throw new ArgumentNullException("ICustomerService");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("customer/all")]
|
||||
public async Task<IActionResult> GetAllCustomerNames() {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all customer names");
|
||||
|
||||
IEnumerable<string> customerNames = await _customerService.GetCustomerNames();
|
||||
|
||||
return Ok(customerNames);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get user by LoginID, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetAllCustomerNames";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
MesaFabApproval.API/Controllers/ECNController.cs
Normal file
56
MesaFabApproval.API/Controllers/ECNController.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class ECNController : ControllerBase {
|
||||
private readonly ILogger<ApprovalController> _logger;
|
||||
private readonly IECNService _ecnService;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public ECNController(ILogger<ApprovalController> logger, IECNService ecnService,
|
||||
IMonInWorkerClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_ecnService = ecnService ?? throw new ArgumentNullException("IECNService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("ecn/isValidEcnNumber")]
|
||||
public async Task<IActionResult> IsValidEcnNumber(int number) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to determine if {number} is a valid ECN#");
|
||||
|
||||
if (number <= 0) return Ok(false);
|
||||
|
||||
bool isValid = await _ecnService.IsValidECNNumber(number);
|
||||
|
||||
return Ok(isValid);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot determine if {number} is a valid ECN#, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "IsValidEcnNumber";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
910
MesaFabApproval.API/Controllers/MRBController.cs
Normal file
910
MesaFabApproval.API/Controllers/MRBController.cs
Normal file
@ -0,0 +1,910 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.StaticFiles;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class MRBController : ControllerBase {
|
||||
private readonly ILogger<MRBController> _logger;
|
||||
private readonly IMRBService _mrbService;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public MRBController(ILogger<MRBController> logger, IMRBService mrbService, IMonInWorkerClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_mrbService = mrbService ?? throw new ArgumentNullException("IMRBService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/new")]
|
||||
public async Task<IActionResult> CreateNewMRB(MRB mrb) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to generate a new MRB");
|
||||
|
||||
if (mrb is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
|
||||
await _mrbService.CreateNewMRB(mrb);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot create new MRB, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "CreateNewMRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("mrb/delete")]
|
||||
public async Task<IActionResult> DeleteMRB(int mrbNumber) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete MRB# {mrbNumber}");
|
||||
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRB number");
|
||||
|
||||
await _mrbService.DeleteMRB(mrbNumber);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot delete MRB {mrbNumber}, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "DeleteMRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/all")]
|
||||
public async Task<IActionResult> GetAllMRBs(bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all MRBs");
|
||||
|
||||
IEnumerable<MRB> allMrbs = await _mrbService.GetAllMRBs(bypassCache);
|
||||
|
||||
return Ok(allMrbs);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get all MRBs, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetAllMRBs";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/numberIsValid")]
|
||||
public async Task<IActionResult> NumberIsValid(int number) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to determine if {number} is a valid MRB#");
|
||||
|
||||
bool isValid = await _mrbService.MRBNumberIsValid(number);
|
||||
|
||||
return Ok(isValid);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Unable to determine if {number} is a valid MRB#, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "IsValidMRBNumber";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/getById")]
|
||||
public async Task<IActionResult> GetMRBById(int id, bool bypassCache = false) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get an MRB by Id");
|
||||
|
||||
if (id <= 0) throw new ArgumentException("Invalid MRB number");
|
||||
|
||||
MRB mrb = await _mrbService.GetMRBById(id, bypassCache);
|
||||
|
||||
return Ok(mrb);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get MRB by Id, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBbyId";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/getByTitle")]
|
||||
public async Task<IActionResult> GetMRBByTitle(string title, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get an MRB by Title");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(title)) throw new ArgumentException("Title cannot be null or empty");
|
||||
|
||||
MRB mrb = await _mrbService.GetMRBByTitle(title, bypassCache);
|
||||
|
||||
return Ok(mrb);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get MRB by title, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBbyTitle";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("mrb")]
|
||||
public async Task<IActionResult> UpdateMRB(MRB mrb) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an MRB");
|
||||
|
||||
if (mrb is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
|
||||
await _mrbService.UpdateMRB(mrb);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot update MRB, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "UpdateMRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrbAction")]
|
||||
public async Task<IActionResult> CreateMRBAction(MRBAction mrbAction) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to generate a new MRB");
|
||||
|
||||
if (mrbAction is null) throw new ArgumentNullException("MRB action cannot be null");
|
||||
|
||||
await _mrbService.CreateMRBAction(mrbAction);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot create new MRB action, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "CreateNewMRBAction";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrbAction")]
|
||||
public async Task<IActionResult> GetMRBActionsForMRB(int mrbNumber, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all MRBs");
|
||||
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRB number");
|
||||
|
||||
IEnumerable<MRBAction> mrbActions = await _mrbService.GetMRBActionsForMRB(mrbNumber, bypassCache);
|
||||
|
||||
return Ok(mrbActions);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get all MRBs, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBActionsForMRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("mrbAction")]
|
||||
public async Task<IActionResult> UpdateMRBAction(MRBAction mrbAction) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update an MRB action");
|
||||
|
||||
if (mrbAction is null) throw new ArgumentNullException("MRB action cannot be null");
|
||||
|
||||
await _mrbService.UpdateMRBAction(mrbAction);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot update MRB action, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "UpdateMRBAction";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("mrbAction")]
|
||||
public async Task<IActionResult> DeleteMRBAction(int mrbActionID, int mrbNumber) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete MRB action {mrbActionID}");
|
||||
|
||||
if (mrbActionID <= 0) throw new ArgumentException($"{mrbActionID} is not a valid MRB ActionID");
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRBNumber");
|
||||
|
||||
await _mrbService.DeleteMRBAction(mrbActionID, mrbNumber);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot delete MRB action, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "DeleteMRBAction";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/attachments")]
|
||||
public async Task<IActionResult> GetAttachmentsForMRB(int mrbNumber, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get MRB attachments for MRB {mrbNumber}");
|
||||
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRB number");
|
||||
|
||||
List<MRBAttachment> attachments = (await _mrbService.GetAllAttachmentsForMRB(mrbNumber, bypassCache)).ToList();
|
||||
|
||||
return Ok(attachments);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get attachments for MRB {mrbNumber}, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBAttachments";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("mrb/action/attachments")]
|
||||
public async Task<IActionResult> GetActionAttachmentsForMRB(int mrbNumber, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to get action attachments for MRB {mrbNumber}");
|
||||
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRB#");
|
||||
|
||||
List<MRBActionAttachment> attachments =
|
||||
(await _mrbService.GetAllActionAttachmentsForMRB(mrbNumber, bypassCache)).ToList();
|
||||
|
||||
return Ok(attachments);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get action attachments for MRB# {mrbNumber}, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBActionAttachments";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/attach")]
|
||||
public async Task<IActionResult> SaveMRBAttachment([FromForm] IEnumerable<IFormFile> files, int mrbNumber) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to save MRB attachments");
|
||||
|
||||
if (files is null) throw new ArgumentNullException("Files cannot be null");
|
||||
if (files.Count() <= 0) throw new ArgumentException("Files cannot be empty");
|
||||
if (mrbNumber <= 0) throw new ArgumentException($"{mrbNumber} is not a valid MRB number");
|
||||
|
||||
IEnumerable<UploadResult> uploadResults = (await _mrbService.UploadAttachments(files, mrbNumber)).ToList();
|
||||
|
||||
return Ok(uploadResults);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot save MRB attachments, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "SaveMRBAttachments";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/action/attach")]
|
||||
public async Task<IActionResult> SaveMRBActionAttachment([FromForm] IEnumerable<IFormFile> files, int actionId) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to save MRB action attachments");
|
||||
|
||||
if (files is null) throw new ArgumentNullException("Files cannot be null");
|
||||
if (files.Count() <= 0) throw new ArgumentException("Files cannot be empty");
|
||||
if (actionId <= 0) throw new ArgumentException($"{actionId} is not a valid MRB action ID");
|
||||
|
||||
IEnumerable<UploadResult> uploadResults = (await _mrbService.UploadActionAttachments(files, actionId)).ToList();
|
||||
|
||||
return Ok(uploadResults);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot save MRB action attachments, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "SaveMRBActionAttachments";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[AllowAnonymous]
|
||||
[HttpGet]
|
||||
[Route("mrb/attachmentFile")]
|
||||
public async Task<IActionResult> GetMRBAttachmentFile(string path, string fileName) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get MRB attachment file");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Path cannot be null or empty");
|
||||
if (!System.IO.File.Exists(path)) throw new ArgumentException("No file exists at provided path");
|
||||
if (string.IsNullOrWhiteSpace(fileName)) throw new ArgumentException("Filename cannot be null or empty");
|
||||
|
||||
byte[] fs = System.IO.File.ReadAllBytes(path);
|
||||
|
||||
const string defaultContentType = "application/octet-stream";
|
||||
|
||||
FileExtensionContentTypeProvider contentTypeProvider = new FileExtensionContentTypeProvider();
|
||||
|
||||
if (!contentTypeProvider.TryGetContentType(path, out string? contentType)) {
|
||||
contentType = defaultContentType;
|
||||
}
|
||||
|
||||
return new FileContentResult(fs, contentType) {
|
||||
FileDownloadName = fileName
|
||||
};
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get MRB attachment file, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetMRBAttachmentFile";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("mrb/attach")]
|
||||
public async Task<IActionResult> DeleteMRBAttachment(MRBAttachment attachment) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to delete MRB attachment");
|
||||
|
||||
if (attachment is null) throw new ArgumentNullException("Attachment cannot be null");
|
||||
|
||||
await _mrbService.DeleteAttachment(attachment);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get MRB attachment file, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "DeleteMRBAttachment";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/notify/new-approvals")]
|
||||
public async Task<IActionResult> NotifyNewApprovals(MRB mrb) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify new approvers");
|
||||
|
||||
if (mrb is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
|
||||
await _mrbService.NotifyNewApprovals(mrb);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Unable to notify new approvers, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "NotifyNewMRBApprovers";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/notify/approvers")]
|
||||
public async Task<IActionResult> NotifyApprovers(MRBNotification notification) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify approvers");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("notification cannot be null");
|
||||
if (notification.MRB is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message)) throw new ArgumentException("message cannot be null or empty");
|
||||
|
||||
await _mrbService.NotifyApprovers(notification);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Unable to notify approvers, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "NotifyMRBApprovers";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/notify/originator")]
|
||||
public async Task<IActionResult> NotifyOriginator(MRBNotification notification) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify originator");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("MRBNotification cannot be null");
|
||||
if (notification.MRB is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message)) throw new ArgumentException("Message cannot be null or empty");
|
||||
|
||||
await _mrbService.NotifyOriginator(notification);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Unable to notify originator, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "NotifyMRBOriginator";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("mrb/notify/qa-pre-approver")]
|
||||
public async Task<IActionResult> NotifyQAPreApprover(MRBNotification notification) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to notify QA pre approver");
|
||||
|
||||
if (notification is null) throw new ArgumentNullException("MRBNotification cannot be null");
|
||||
if (notification.MRB is null) throw new ArgumentNullException("MRB cannot be null");
|
||||
if (string.IsNullOrWhiteSpace(notification.Message)) throw new ArgumentException("Message cannot be null or empty");
|
||||
|
||||
await _mrbService.NotifyQAPreApprover(notification);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Unable to notify QA pre approver, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "NotifyQAPreApprover";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
264
MesaFabApproval.API/Controllers/PCRBController.cs
Normal file
264
MesaFabApproval.API/Controllers/PCRBController.cs
Normal file
@ -0,0 +1,264 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Authorize]
|
||||
public class PCRBController : ControllerBase {
|
||||
private readonly ILogger<MRBController> _logger;
|
||||
private readonly IPCRBService _pcrbService;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
|
||||
public PCRBController(ILogger<MRBController> logger, IPCRBService pcrbService, IMonInWorkerClient monInClient) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_pcrbService = pcrbService ?? throw new ArgumentNullException("IPCRBService not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("pcrb")]
|
||||
public async Task<IActionResult> CreateNewPCRB(PCRB pcrb) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to generate a new PCRB");
|
||||
|
||||
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
||||
|
||||
await _pcrbService.CreateNewPCRB(pcrb);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot create new PCRB, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "CreateNewPCRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("pcrb/all")]
|
||||
public async Task<IActionResult> GetAllPCRBs(bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all PCRBs");
|
||||
|
||||
IEnumerable<PCRB> allPCRBs = await _pcrbService.GetAllPCRBs(bypassCache);
|
||||
|
||||
return Ok(allPCRBs);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get all PCRBs, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetAllPCRBs";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("pcrb/getByTitle")]
|
||||
public async Task<IActionResult> GetPCRBByTitle(string title, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get an PCRB by Title");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(title)) throw new ArgumentException("Title cannot be null or empty");
|
||||
|
||||
PCRB pcrb = await _pcrbService.GetPCRBByTitle(title, bypassCache);
|
||||
|
||||
return Ok(pcrb);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get PCRB by title, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetPCRBbyTitle";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("pcrb/getByPlanNumber")]
|
||||
public async Task<IActionResult> GetPCRBByPlanNumber(int planNumber, bool bypassCache) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get an PCRB by Title");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan #");
|
||||
|
||||
PCRB pcrb = await _pcrbService.GetPCRBByPlanNumber(planNumber, bypassCache);
|
||||
|
||||
return Ok(pcrb);
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get PCRB by plan number, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetPCRBbyPlanNumber";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPut]
|
||||
[Route("pcrb")]
|
||||
public async Task<IActionResult> UpdatePCRB(PCRB pcrb) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to update a PCRB");
|
||||
|
||||
if (pcrb is null) throw new ArgumentNullException("PCRB cannot be null");
|
||||
|
||||
await _pcrbService.UpdatePCRB(pcrb);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot update PCRB, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "UpdatePCRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete]
|
||||
[Route("pcrb")]
|
||||
public async Task<IActionResult> DeletePCRB(int planNumber) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation($"Attempting to delete PCRB# {planNumber}");
|
||||
|
||||
if (planNumber <= 0) throw new ArgumentException($"{planNumber} is not a valid PCRB plan #");
|
||||
|
||||
await _pcrbService.DeletePCRB(planNumber);
|
||||
|
||||
return Ok();
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = ex.Message;
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot delete PCRB# {planNumber}, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "DeletePCRB";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
223
MesaFabApproval.API/Controllers/UserController.cs
Normal file
223
MesaFabApproval.API/Controllers/UserController.cs
Normal file
@ -0,0 +1,223 @@
|
||||
using MesaFabApproval.API.Services;
|
||||
using MesaFabApproval.Shared.Models;
|
||||
using MesaFabApproval.Shared.Services;
|
||||
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace MesaFabApproval.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase {
|
||||
private readonly ILogger<UserController> _logger;
|
||||
private readonly IMonInWorkerClient _monInClient;
|
||||
private readonly IMemoryCache _cache;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public UserController(ILogger<UserController> logger,
|
||||
IMonInWorkerClient monInClient,
|
||||
IMemoryCache cache,
|
||||
IUserService userService) {
|
||||
_logger = logger ?? throw new ArgumentNullException("ILogger not injected");
|
||||
_monInClient = monInClient ?? throw new ArgumentNullException("IMonInWorkerClient not injected");
|
||||
_cache = cache ?? throw new ArgumentNullException("IMemoryCache not injected");
|
||||
_userService = userService ?? throw new ArgumentNullException("IUserService not injected");
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("/user/loginId")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetUserByLoginId(string loginId) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get user by LoginID");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(loginId))
|
||||
throw new ArgumentException("LoginID cannot be null or empty");
|
||||
|
||||
User? user = _cache.Get<User>($"user{loginId}");
|
||||
|
||||
if (user is null) {
|
||||
user = await _userService.GetUserByLoginId(loginId);
|
||||
|
||||
_cache.Set($"user{loginId}", user, DateTimeOffset.Now.AddDays(1));
|
||||
}
|
||||
|
||||
if (user is not null) return Ok(user);
|
||||
|
||||
throw new Exception($"User with LoginID {loginId} not found");
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get user by LoginID, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetUserByLoginId";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("/user/userId")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetUserByUserId(int userId) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get user by LoginID");
|
||||
|
||||
if (userId <= 0) throw new ArgumentException($"{userId} is not a valid user ID");
|
||||
|
||||
User? user = _cache.Get<User>($"user{userId}");
|
||||
|
||||
if (user is null) {
|
||||
user = await _userService.GetUserByUserId(userId);
|
||||
|
||||
_cache.Set($"user{userId}", user, DateTimeOffset.Now.AddDays(1));
|
||||
}
|
||||
|
||||
if (user is not null) return Ok(user);
|
||||
|
||||
throw new Exception($"User with UserID {userId} not found");
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get user by User ID, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetUserByUserId";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("/users/active")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetAllActiveUsers() {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all active users");
|
||||
|
||||
IEnumerable<User>? activeUsers = _cache.Get<IEnumerable<User>>($"activeUsers");
|
||||
|
||||
if (activeUsers is null) {
|
||||
activeUsers = await _userService.GetAllActiveUsers();
|
||||
|
||||
_cache.Set($"activeUsers", activeUsers, DateTimeOffset.Now.AddDays(1));
|
||||
}
|
||||
|
||||
if (activeUsers is not null) return Ok(activeUsers);
|
||||
|
||||
throw new Exception($"No active users found");
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get all active users, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetAllActiveUsers";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("/approver")]
|
||||
[Authorize]
|
||||
public async Task<IActionResult> GetApproverUserIdsForSubRoleCategoryItem(string subRoleCategoryItem) {
|
||||
DateTime start = DateTime.Now;
|
||||
bool isArgumentError = false;
|
||||
bool isInternalError = false;
|
||||
string errorMessage = "";
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get approver user IDs");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(subRoleCategoryItem))
|
||||
throw new ArgumentException("SubRoleCategoryItem cannot be null or empty");
|
||||
|
||||
IEnumerable<int>? approverUserIds = _cache.Get<IEnumerable<int>>($"approvers{subRoleCategoryItem}");
|
||||
|
||||
if (approverUserIds is null) {
|
||||
approverUserIds = await _userService.GetApproverUserIdsBySubRoleCategoryItem(subRoleCategoryItem);
|
||||
|
||||
_cache.Set($"approvers{subRoleCategoryItem}", approverUserIds, DateTimeOffset.Now.AddDays(1));
|
||||
}
|
||||
|
||||
if (approverUserIds is not null) return Ok(approverUserIds);
|
||||
|
||||
throw new Exception($"Approvers for SubRoleCategoryItem {subRoleCategoryItem} not found");
|
||||
} catch (ArgumentException ex) {
|
||||
isArgumentError = true;
|
||||
errorMessage = $"Invalid argument. {ex.Message}";
|
||||
return BadRequest(errorMessage);
|
||||
} catch (Exception ex) {
|
||||
isInternalError = true;
|
||||
errorMessage = $"Cannot get approver user IDs, because {ex.Message}";
|
||||
return Problem(errorMessage);
|
||||
} finally {
|
||||
string metricName = "GetApproverUserIds";
|
||||
DateTime end = DateTime.Now;
|
||||
double millisecondsDiff = (end - start).TotalMilliseconds;
|
||||
_monInClient.PostAverage(metricName + "Latency", millisecondsDiff);
|
||||
|
||||
if (isArgumentError) {
|
||||
_logger.LogWarning(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
} else if (isInternalError) {
|
||||
_logger.LogError(errorMessage);
|
||||
_monInClient.PostStatus(metricName, StatusValue.Critical);
|
||||
} else {
|
||||
_monInClient.PostStatus(metricName, StatusValue.Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user