Leaf Input functional
This commit is contained in:
@@ -17,5 +17,32 @@ namespace LeafWeb.Web.Controllers
|
||||
DataService.Dispose();
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
protected bool IsHttpParamActionMatch()
|
||||
{
|
||||
return ControllerContext.RouteData.Values["action"].ToString()
|
||||
.Equals("Action", StringComparison.InvariantCultureIgnoreCase);
|
||||
}
|
||||
|
||||
protected enum StatusType
|
||||
{
|
||||
Info,
|
||||
Success,
|
||||
Error
|
||||
}
|
||||
|
||||
protected void SetStatusMessage(string msg, StatusType statusType = StatusType.Info)
|
||||
{
|
||||
TempData["StatusMessage"] = msg;
|
||||
switch (statusType)
|
||||
{
|
||||
case StatusType.Success:
|
||||
TempData["StatusMessage-Type"] = "alert-success";
|
||||
break;
|
||||
case StatusType.Error:
|
||||
TempData["StatusMessage-Type"] = "alert-error";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,71 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using LeafWeb.Core.DAL;
|
||||
using LeafWeb.Core.Models;
|
||||
using LeafWeb.Web.Attributes;
|
||||
using LeafWeb.Web.ViewModels.LeafInput;
|
||||
|
||||
namespace LeafWeb.Web.Controllers
|
||||
{
|
||||
public class LeafInputController : Controller
|
||||
{
|
||||
public ActionResult Index()
|
||||
{
|
||||
// initialize the session storage to retain SessionID between requests
|
||||
public class LeafInputController : ControllerBase
|
||||
{
|
||||
public ActionResult Index()
|
||||
{
|
||||
// initialize the session storage to retain SessionID between requests
|
||||
Session["placeholder"] = 0;
|
||||
return View();
|
||||
}
|
||||
return View();
|
||||
}
|
||||
|
||||
private FileInfo[] GetBackloadDirectoryFiles(string directoryName)
|
||||
{
|
||||
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
|
||||
var directory = new DirectoryInfo(path);
|
||||
if (!directory.Exists)
|
||||
{
|
||||
return new FileInfo[] {};
|
||||
}
|
||||
|
||||
return directory.GetFiles();
|
||||
}
|
||||
|
||||
private void DeleteBackloadDirectory(string directoryName)
|
||||
{
|
||||
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
|
||||
var directory = new DirectoryInfo(path);
|
||||
if (directory.Exists)
|
||||
{
|
||||
directory.Delete(true);
|
||||
}
|
||||
}
|
||||
|
||||
[HttpParamAction]
|
||||
[HttpPost]
|
||||
public ActionResult Index(CreateViewModel viewModel)
|
||||
{
|
||||
public ActionResult Index(CreateViewModel viewModel)
|
||||
{
|
||||
// directory name is the sessionID
|
||||
var directory = Session.SessionID;
|
||||
var path = Path.Combine(Server.MapPath("~/Files/"), directory + "\\");
|
||||
var files = Directory.GetFiles(path);
|
||||
var files = GetBackloadDirectoryFiles(Session.SessionID);
|
||||
|
||||
if (!files.Any())
|
||||
{
|
||||
ModelState.AddModelError("", "Must select at least one file");
|
||||
}
|
||||
|
||||
if (ModelState.IsValid && !IsHttpParamActionMatch())
|
||||
{
|
||||
// Go to confirmation
|
||||
var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray());
|
||||
return View("Confirm", confirmViewModel);
|
||||
}
|
||||
return View("Index");
|
||||
}
|
||||
|
||||
[HttpParamAction]
|
||||
[HttpPost]
|
||||
public ActionResult Confirm(CreateViewModel viewModel)
|
||||
{
|
||||
// directory name is the sessionID
|
||||
var files = GetBackloadDirectoryFiles(Session.SessionID);
|
||||
|
||||
if (!files.Any())
|
||||
{
|
||||
@@ -31,12 +75,25 @@ namespace LeafWeb.Web.Controllers
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
// convert viewModel into Model
|
||||
|
||||
var model = viewModel.GetFileInput();
|
||||
// load files into LeafInputFile
|
||||
var leafInputFiles =
|
||||
from f in files
|
||||
let bytes = System.IO.File.ReadAllBytes(f.FullName)
|
||||
select new LeafInputFile {Filename = f.Name, Contents = bytes};
|
||||
|
||||
//
|
||||
// TODO: Save to db
|
||||
|
||||
DeleteBackloadDirectory(Session.SessionID);
|
||||
|
||||
SetStatusMessage(
|
||||
HttpUtility.HtmlEncode(
|
||||
$"A data set has submitted for '{viewModel.Identifier}' from '{viewModel.SiteId}'. "
|
||||
+ $"When complete, an email will be delivered to {viewModel.Name} <{viewModel.Email}> with results."),
|
||||
StatusType.Success);
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
return View();
|
||||
}
|
||||
}
|
||||
return View("Index", viewModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user