From 65bc2b98ab2f738b04f79c28f76d07cb7eff1bc2 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Thu, 4 Feb 2016 13:46:08 -0500 Subject: [PATCH] Save leafinput to DB, update style for file upload --- Core/DAL/DataService.cs | 18 ++++++++++++ Web/Controllers/LeafInputController.cs | 36 +++++++++++------------- Web/Views/LeafInput/Index.cshtml | 10 ++++--- Web/Views/Shared/_ValidationField.cshtml | 13 +++++++++ Web/Web.csproj | 1 + 5 files changed, 54 insertions(+), 24 deletions(-) create mode 100644 Web/Views/Shared/_ValidationField.cshtml diff --git a/Core/DAL/DataService.cs b/Core/DAL/DataService.cs index 4aa23ca..629569d 100644 --- a/Core/DAL/DataService.cs +++ b/Core/DAL/DataService.cs @@ -37,5 +37,23 @@ namespace LeafWeb.Core.DAL select fs; } #endregion + + + #region Fluxnet Sites + + public IQueryable GetLeafInputs() + { + return _db.LeafInputs; + } + + public void AddLeafInput(LeafInput leafInput) + { + leafInput.Created = DateTime.Now; + + _db.LeafInputs.Add(leafInput); + _db.SaveChanges(); + } + + #endregion } } diff --git a/Web/Controllers/LeafInputController.cs b/Web/Controllers/LeafInputController.cs index 1dc82ce..efe18f3 100644 --- a/Web/Controllers/LeafInputController.cs +++ b/Web/Controllers/LeafInputController.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; @@ -21,12 +22,10 @@ namespace LeafWeb.Web.Controllers { var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); var directory = new DirectoryInfo(path); - if (!directory.Exists) - { - return new FileInfo[] {}; - } - - return directory.GetFiles(); + return + !directory.Exists + ? new FileInfo[] {} + : directory.GetFiles(); } private void DeleteBackloadDirectory(string directoryName) @@ -34,9 +33,7 @@ namespace LeafWeb.Web.Controllers var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\"); var directory = new DirectoryInfo(path); if (directory.Exists) - { directory.Delete(true); - } } [HttpParamAction] @@ -47,11 +44,9 @@ namespace LeafWeb.Web.Controllers var files = GetBackloadDirectoryFiles(Session.SessionID); if (!files.Any()) - { - ModelState.AddModelError("", "Must select at least one file"); - } + ModelState.AddModelError("Files", "Must select at least one file"); - if (ModelState.IsValid && !IsHttpParamActionMatch()) + if (ModelState.IsValid && !IsHttpParamActionMatch()) // HttpParamMatch indicates it's backing out from Confirm { // Go to confirmation var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray()); @@ -69,26 +64,27 @@ namespace LeafWeb.Web.Controllers if (!files.Any()) { - ModelState.AddModelError("", "Must select at least one file"); + ModelState.AddModelError("Files", "Must select at least one file"); } if (ModelState.IsValid) { // convert viewModel into Model - var model = viewModel.GetFileInput(); + var leafInput = viewModel.GetFileInput(); // load files into LeafInputFile - var leafInputFiles = - from f in files + leafInput.LeafInputFiles = + (from f in files let bytes = System.IO.File.ReadAllBytes(f.FullName) - select new LeafInputFile {Filename = f.Name, Contents = bytes}; + select new LeafInputFile {Filename = f.Name, Contents = bytes}).ToList(); - // TODO: Save to db + // Save to db + DataService.AddLeafInput(leafInput); DeleteBackloadDirectory(Session.SessionID); SetStatusMessage( HttpUtility.HtmlEncode( - $"A data set has submitted for '{viewModel.Identifier}' from '{viewModel.SiteId}'. " + $"A data set has submitted for '{viewModel.Identifier}' from '{viewModel.SiteId}'. " + Environment.NewLine + $"When complete, an email will be delivered to {viewModel.Name} <{viewModel.Email}> with results."), StatusType.Success); return RedirectToAction("Index"); diff --git a/Web/Views/LeafInput/Index.cshtml b/Web/Views/LeafInput/Index.cshtml index dd49a2d..5c38eee 100644 --- a/Web/Views/LeafInput/Index.cshtml +++ b/Web/Views/LeafInput/Index.cshtml @@ -28,7 +28,7 @@
-
+
@@ -44,15 +44,16 @@
-
+
-
 
+ @*
 
*@
+ @Html.Partial("_ValidationField", "Files") @@ -70,7 +71,8 @@

Processing...

-
+
+
{% if (!i && !o.options.autoUpload) { %} diff --git a/Web/Views/Shared/_ValidationField.cshtml b/Web/Views/Shared/_ValidationField.cshtml new file mode 100644 index 0000000..9971b1b --- /dev/null +++ b/Web/Views/Shared/_ValidationField.cshtml @@ -0,0 +1,13 @@ +@model string +@{ + var fieldName = Model; +} +@if (ViewData.ModelState[fieldName] != null && ViewData.ModelState[fieldName].Errors.Any()) +{ + + @foreach (var error in ViewData.ModelState[fieldName].Errors) + { + @error.ErrorMessage + } + +} \ No newline at end of file diff --git a/Web/Web.csproj b/Web/Web.csproj index 0caa75d..1401284 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -939,6 +939,7 @@ + Web.config