311 lines
11 KiB
C#
311 lines
11 KiB
C#
using SampleWorkflowWeb.Models;
|
|
using SampleWorkflowWeb.Models.Transfer;
|
|
using SampleWorkflowWeb.Services;
|
|
using Caspian.WorkflowFramework.Contracts;
|
|
using PackageNS = Caspian.WorkflowFramework.Models;
|
|
using Elsa.Common.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MongoLogger;
|
|
using Newtonsoft.Json;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace SampleWorkflowWeb.Controllers
|
|
{
|
|
public class TransferController : Controller, ILoggable
|
|
{
|
|
#region Fields
|
|
private readonly ILogger<HomeController> _logger;
|
|
private readonly ICardService _cardService;
|
|
private readonly IWorkflowService _workflowService;
|
|
#endregion
|
|
|
|
#region Ctor
|
|
public TransferController(ILogger<HomeController> logger,
|
|
ICardService cardService,
|
|
IWorkflowService workflowService)
|
|
{
|
|
_logger = logger;
|
|
_cardService = cardService;
|
|
_workflowService = workflowService;
|
|
}
|
|
#endregion
|
|
|
|
#region Index
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
#endregion
|
|
|
|
#region TransferAmount
|
|
[HttpGet]
|
|
public async Task<IActionResult> TransferAmount(long userId)
|
|
{
|
|
var response = await _workflowService.CreateAndExecuteAsync(new PackageNS.CreateAndExecuteWorkflowModel()
|
|
{
|
|
Name = "TransferWorkflow",
|
|
UserId = userId,
|
|
Version = VersionOptions.Published
|
|
});
|
|
|
|
if (response != null && response.Status == 0)
|
|
{
|
|
var input = new SelectionViewModel()
|
|
{
|
|
WorkflowId = response.Data?.InstanceId,
|
|
PageEventName = response.Data?.NextPageEventName
|
|
};
|
|
|
|
|
|
|
|
HttpContext.Session.SetObject("SelectionSession", input);
|
|
|
|
//return View(response.Data?.NextPageUrl, input);
|
|
return RedirectToAction(response.Data?.NextPageUrl);
|
|
}
|
|
else
|
|
{
|
|
return RedirectToAction("Error", response);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Selection
|
|
public async Task<IActionResult> Selection()
|
|
{
|
|
// Retrieve from session
|
|
var model = HttpContext.Session.GetObject<SelectionViewModel>("SelectionSession");
|
|
HttpContext.Session.Remove("SelectionSession");
|
|
|
|
ViewBag.SourceCards = _cardService.GetSourceCardsService();
|
|
ViewBag.DestinationCards = _cardService.GetDestinationCardsService();
|
|
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Selection(SelectionViewModel input)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
ViewBag.SourceCards = _cardService.GetSourceCardsService();
|
|
ViewBag.DestinationCards = _cardService.GetDestinationCardsService();
|
|
|
|
return View(input);
|
|
}
|
|
|
|
var publishModel = new PackageNS.PublishEventModel()
|
|
{
|
|
EventName = input.PageEventName,
|
|
WorkflowInstanceId = input.WorkflowId,
|
|
EventData = JsonConvert.SerializeObject(input)
|
|
};
|
|
|
|
var response = await _workflowService.PublishEventAsync(publishModel);
|
|
|
|
if (response != null && response.Status == 0)
|
|
{
|
|
if (response.Data?.NextPageUrl == "Confirm")
|
|
{
|
|
var fromPan = _cardService.GetCardService(input.From)?.PAN;
|
|
var fromFullName = _cardService.GetCardOwnerService(input.From);
|
|
|
|
var toPan = _cardService.GetCardService(input.To)?.PAN;
|
|
var toFullName = _cardService.GetCardOwnerService(input.To);
|
|
|
|
var confirmViewModel = new ConfirmViewModel()
|
|
{
|
|
From = input.From,
|
|
FromPan = fromPan,
|
|
FromFullName = fromFullName,
|
|
To = input.To,
|
|
ToPan = toPan,
|
|
ToFullName = toFullName,
|
|
Amount = input.Amount,
|
|
PersonType = input.PersonType,
|
|
WorkflowId = response.Data?.InstanceId,
|
|
PageEventName = response.Data?.NextPageEventName
|
|
};
|
|
|
|
HttpContext.Session.SetObject("ConfirmSession", confirmViewModel);
|
|
|
|
//return View(response.Data?.NextPageUrl, confirmViewModel);
|
|
return RedirectToAction(response.Data?.NextPageUrl);
|
|
}
|
|
else if (response.Data?.NextPageUrl == "Cartable")
|
|
{
|
|
var viewModel = new CartableViewModel()
|
|
{
|
|
From = input.From,
|
|
To = input.To,
|
|
Amount = input.Amount,
|
|
PersonType = input.PersonType,
|
|
WorkflowId = response.Data?.InstanceId,
|
|
PageEventName = response.Data?.NextPageEventName
|
|
};
|
|
|
|
HttpContext.Session.SetObject("CartableSession", viewModel);
|
|
|
|
return RedirectToAction(response.Data?.NextPageUrl);
|
|
}
|
|
else
|
|
{
|
|
ViewBag.SourceCards = _cardService.GetSourceCardsService();
|
|
ViewBag.DestinationCards = _cardService.GetDestinationCardsService();
|
|
|
|
return View(input);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
return RedirectToAction("Error", response);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Cartable
|
|
public async Task<IActionResult> Cartable()
|
|
{
|
|
// Retrieve from session
|
|
var model = HttpContext.Session.GetObject<CartableViewModel>("CartableSession");
|
|
HttpContext.Session.Remove("CartableSession");
|
|
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Cartable(CartableViewModel input)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
//ViewBag.SourceCards = _cardService.GetSourceCardsService();
|
|
//ViewBag.DestinationCards = _cardService.GetDestinationCardsService();
|
|
|
|
return View(input);
|
|
}
|
|
|
|
var publishModel = new PackageNS.PublishEventModel()
|
|
{
|
|
EventName = input.PageEventName,
|
|
WorkflowInstanceId = input.WorkflowId,
|
|
EventData = JsonConvert.SerializeObject(input)
|
|
};
|
|
|
|
var response = await _workflowService.PublishEventAsync(publishModel);
|
|
|
|
if (response != null && response.Status == 0)
|
|
{
|
|
var fromPan = _cardService.GetCardService(input.From)?.PAN;
|
|
var fromFullName = _cardService.GetCardOwnerService(input.From);
|
|
|
|
var toPan = _cardService.GetCardService(input.To)?.PAN;
|
|
var toFullName = _cardService.GetCardOwnerService(input.To);
|
|
|
|
var viewModel = new ConfirmViewModel()
|
|
{
|
|
From = input.From,
|
|
FromPan = fromPan,
|
|
FromFullName = fromFullName,
|
|
To = input.To,
|
|
ToPan = toPan,
|
|
ToFullName = toFullName,
|
|
Amount = input.Amount,
|
|
PersonType = input.PersonType,
|
|
WorkflowId = response.Data?.InstanceId,
|
|
PageEventName = response.Data?.NextPageEventName
|
|
};
|
|
|
|
HttpContext.Session.SetObject("ConfirmSession", viewModel);
|
|
|
|
return RedirectToAction(response.Data?.NextPageUrl);
|
|
}
|
|
else
|
|
{
|
|
return RedirectToAction("Error", response);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Confirm
|
|
public async Task<IActionResult> Confirm()
|
|
{
|
|
// Retrieve from session
|
|
var model = HttpContext.Session.GetObject<ConfirmViewModel>("ConfirmSession");
|
|
HttpContext.Session.Remove("ConfirmSession");
|
|
|
|
return View(model);
|
|
}
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Confirm(ConfirmViewModel input)
|
|
{
|
|
input.FromPan = _cardService.GetCardService(input.From)?.PAN;
|
|
input.FromFullName = _cardService.GetCardOwnerService(input.From);
|
|
|
|
input.ToPan = _cardService.GetCardService(input.To)?.PAN;
|
|
input.ToFullName = _cardService.GetCardOwnerService(input.To);
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return View(input);
|
|
}
|
|
|
|
//انتقال
|
|
var result = await _cardService.TransferAsync(input.FromPan, input.ToPan, input.Amount);
|
|
|
|
var publishModel = new PackageNS.PublishEventModel()
|
|
{
|
|
EventName = input.PageEventName,
|
|
WorkflowInstanceId = input.WorkflowId,
|
|
EventData = JsonConvert.SerializeObject(input)
|
|
};
|
|
|
|
var response = await _workflowService.PublishEventAsync(publishModel);
|
|
|
|
if (response != null && response.Status == 0)
|
|
{
|
|
var receiptInput = new ReceiptViewModel()
|
|
{
|
|
From = input.From,
|
|
FromPan = input.FromPan,
|
|
FromFullName = input.FromFullName,
|
|
To = input.To,
|
|
ToPan = input.ToPan,
|
|
ToFullName = input.ToFullName,
|
|
Amount = input.Amount,
|
|
WorkflowId = response.Data?.InstanceId,
|
|
PageEventName = response.Data?.NextPageEventName
|
|
};
|
|
|
|
HttpContext.Session.SetObject("ReceiptSession", receiptInput);
|
|
|
|
return RedirectToAction(response.Data?.NextPageUrl);
|
|
}
|
|
else
|
|
{
|
|
return RedirectToAction("Error", response);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Confirm
|
|
public async Task<IActionResult> Receipt()
|
|
{
|
|
// Retrieve from session
|
|
var model = HttpContext.Session.GetObject<ReceiptViewModel>("ReceiptSession");
|
|
HttpContext.Session.Remove("ReceiptSession");
|
|
|
|
return View(model);
|
|
}
|
|
#endregion
|
|
|
|
#region Error
|
|
public IActionResult Error(WorkflowResponse<object> input)
|
|
{
|
|
return View(input);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|