NonInventPurchasingSystem/CPRNIMS.WebApps/Controllers/SMTP/SMTPMgmtController.cs
2026-01-20 07:44:30 +08:00

106 lines
3.6 KiB
C#

using Azure;
using CPRNIMS.Domain.UIContracts.SMTP;
using CPRNIMS.Infrastructure.Entities.Items;
using CPRNIMS.Infrastructure.Helper;
using CPRNIMS.Infrastructure.ViewModel.Account;
using CPRNIMS.Infrastructure.ViewModel.Canvass;
using CPRNIMS.Infrastructure.ViewModel.Items;
using CPRNIMS.Infrastructure.ViewModel.SMTP;
using CPRNIMS.WebApps.Controllers.Base;
using Microsoft.AspNetCore.Mvc;
namespace CPRNIMS.WebApps.Controllers.SMTP
{
public class SMTPMgmtController : BaseMethod
{
List<SMTPCredentialVM>? response;
private readonly ISMTP _sMTP;
public SMTPMgmtController(TokenHelper tokenHelper, ErrorLogHelper errorMessageService,
IWebHostEnvironment webHostEnvironment
, ISMTP sMTP
)
: base(tokenHelper, errorMessageService, webHostEnvironment)
{
_sMTP = sMTP;
}
public async Task<IActionResult> Index()
{
if (GetUser() == null)
{
RedirectToAction("Logout", "Home");
}
await GetStoreCredAsync(await GetUser(), await _tokenHelper.GetJwtTokenAsync(await GetUser()));
return View();
}
#region Get
public async Task<IActionResult> GetAllSmtp()
{
try
{
var viewModels = new SMTPCredentialVM();
response = await _sMTP.GetAllSmtp(await GetUser(), viewModels);
if (response == null)
{
response = new List<SMTPCredentialVM>();
ViewBag.UserRoles = UserRoles;
return Json(new { success = false, data = response });
}
ViewBag.UserRoles = UserRoles;
return Json(new { success = true, data = response });
}
catch (Exception ex)
{
var message = ex.InnerException?.ToString() ?? ex.Message.ToString();
await PostErrorMessage(message, "WebApps");
throw;
}
}
[HttpPost]
public async Task<IActionResult> GetMySMTP(SMTPCredentialVM viewModels)
{
try
{
response = await _sMTP.GetMySmtp(await GetUser(), viewModels);
if (response == null)
{
ViewBag.UserRoles = UserRoles;
return Json(new { success = false, data = response });
}
//To manage the table buttons
ViewBag.UserRoles = UserRoles;
return Json(new { success = true, data = response });
}
catch (Exception ex)
{
var message = ex.InnerException?.ToString() ?? ex.Message.ToString();
await PostErrorMessage(message, "WebApps");
throw;
}
}
#endregion
#region Post Put
public async Task<IActionResult> PostPutSmtp(SMTPCredentialVM viewModel)
{
try
{
var postPutSmtp = await _sMTP.PostPutSmtp(await GetUser(), viewModel);
if (postPutSmtp.StatusResponse != "Error")
{
return Json(new { success = true });
}
return Json(new { success = false, Response = postPutSmtp.Message });
}
catch (Exception ex)
{
var message = ex.InnerException?.ToString() ?? ex.Message.ToString();
await PostErrorMessage(message, "WebApps");
throw;
}
}
#endregion
}
}