From 790e2494e3a6272378990fba3deeed2000488d14 Mon Sep 17 00:00:00 2001 From: James Kolpack Date: Fri, 20 May 2016 22:47:28 -0400 Subject: [PATCH] Add ResultsAdmin, delete action --- Core/DAL/DataService.cs | 36 ++++++++++++- Web/Controllers/ResultsAdminController.cs | 42 +++++++++++++++ .../ResultsAdmin/ResultStatusViewModel.cs | 51 +++++++++++++++++++ Web/Views/ResultsAdmin/Delete.cshtml | 22 ++++++++ Web/Views/ResultsAdmin/Index.cshtml | 28 ++++++++++ Web/Web.csproj | 4 ++ 6 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 Web/Controllers/ResultsAdminController.cs create mode 100644 Web/ViewModels/ResultsAdmin/ResultStatusViewModel.cs create mode 100644 Web/Views/ResultsAdmin/Delete.cshtml create mode 100644 Web/Views/ResultsAdmin/Index.cshtml diff --git a/Core/DAL/DataService.cs b/Core/DAL/DataService.cs index 8e6a5cb..47001a3 100644 --- a/Core/DAL/DataService.cs +++ b/Core/DAL/DataService.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Data.Entity; using System.Linq; @@ -22,6 +23,13 @@ namespace LeafWeb.Core.DAL _db.Dispose(); } + private void RemoveCollectionFromDbSet(ICollection collection, IDbSet set) where T : class + { + if (collection == null) return; + foreach (var entity in collection.ToArray()) + set.Remove(entity); + } + #region Fluxnet Sites public IQueryable GetFluxnetSites() @@ -51,6 +59,31 @@ namespace LeafWeb.Core.DAL return _db.LeafInputs.FirstOrDefault(li => li.Id == id); } + public void DeleteLeafInput(LeafInput leafInput) + { + RemoveCollectionFromDbSet(leafInput.InputFiles, _db.LeafInputFiles); + RemoveCollectionFromDbSet(leafInput.OutputFiles, _db.LeafOutputFiles); + RemoveCollectionFromDbSet(leafInput.StatusHistory, _db.LeafInputStatus); + // Data + if (leafInput.LeafInputData != null) + foreach (var leafInputData in leafInput.LeafInputData.ToArray()) + RemoveLeafInputDataNoUpdate(leafInputData); + _db.LeafInputs.Remove(leafInput); + _db.SaveChanges(); + } + + private void RemoveLeafInputDataNoUpdate(LeafInputData leafInputData) + { + if (leafInputData.Data != null) + foreach (var curve in leafInputData.Data.ToArray()) + _db.LeafInputDataCurves.Remove(curve); + if (leafInputData.Photosynthetic != null) + _db.LeafInputDataPhotosynthetic.Remove(leafInputData.Photosynthetic); + if (leafInputData.Site != null) + _db.LeafInputDataSite.Remove(leafInputData.Site); + _db.LeafInputData.Remove(leafInputData); + } + public LeafOutputFile GetLeafOutput_ChartFile(int leafInputId) { return GetLeafOutput_FilenameLike(leafInputId, LeafOutputFile.Filename_LeafChart); @@ -76,7 +109,7 @@ namespace LeafWeb.Core.DAL public IQueryable GetLeafInputs(params LeafInputStatusType[] statuses) { return - from file in _db.LeafInputs + from file in GetLeafInputs() where statuses.Contains(file.CurrentStatus) select file; } @@ -174,5 +207,6 @@ namespace LeafWeb.Core.DAL } #endregion + } } diff --git a/Web/Controllers/ResultsAdminController.cs b/Web/Controllers/ResultsAdminController.cs new file mode 100644 index 0000000..0475c63 --- /dev/null +++ b/Web/Controllers/ResultsAdminController.cs @@ -0,0 +1,42 @@ +using System; +using System.Linq; +using System.Web.Mvc; +using LeafWeb.Web.Attributes; +using LeafWeb.Web.ViewModels.LeafInput; +using LeafWeb.Web.ViewModels.Results; +using LeafWeb.Web.ViewModels.ResultsAdmin; + +namespace LeafWeb.Web.Controllers +{ + public class ResultsAdminController : ControllerBase + { + public ActionResult Index() + { + var viewModel = + DataService.GetLeafInputs() + .OrderByDescending(f => f.Id) + .ToList() + .Select(leafInput => new ResultStatusViewModel(leafInput)); + return View(viewModel); + } + + public ActionResult Delete(int id) + { + var leafInput = DataService.GetLeafInput(id); + var viewModel = new LeafInputViewModel(leafInput); + return View(viewModel); + } + + [HttpPost, ActionName("Delete")] + [ActionLog] + public ActionResult DeleteConfirmed(int id) + { + // TODO: don't allow currently running LeafInput to be deleted + var leafInput = DataService.GetLeafInput(id); + DataService.DeleteLeafInput(leafInput); + + SetStatusMessage($"LeafInput '{leafInput.Identifier}' deleted"); + return RedirectToAction("Index"); + } + } +} \ No newline at end of file diff --git a/Web/ViewModels/ResultsAdmin/ResultStatusViewModel.cs b/Web/ViewModels/ResultsAdmin/ResultStatusViewModel.cs new file mode 100644 index 0000000..d00bdc9 --- /dev/null +++ b/Web/ViewModels/ResultsAdmin/ResultStatusViewModel.cs @@ -0,0 +1,51 @@ +using System.Linq; +using AutoMapper; + +namespace LeafWeb.Web.ViewModels.ResultsAdmin +{ + public class LeafInputViewModel + { + private static readonly IMapper Mapper; + + public int LeafInputId { get; set; } + public string LeafInputName { get; set; } + public string LeafInputIdentifier { get; set; } + public string LeafInputSiteId { get; set; } + public string LeafInputPhotosynthesisType { get; set; } + public bool HasLeafChart { get; set; } + public string CurrentStatus { get; set; } + //public string[] ErrorMessages { get; set; } + //public string[] LeafOutputFilenames { get; set; } + //public bool HasLeafChartOutputFile { get; set; } + + static LeafInputViewModel() + { + var config = + new MapperConfiguration(cfg => + { + cfg.CreateMap() + .ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id)) + .ForMember(dest => dest.HasLeafChart, opt => opt.ResolveUsing(src => src.OutputFiles.Any(o => o.IsLeafChartFile))) + .ForMember(dest => dest.LeafInputName, opt => opt.MapFrom(src => src.Name)) + .ForMember(dest => dest.LeafInputIdentifier, opt => opt.MapFrom(src => src.Identifier)) + .ForMember(dest => dest.LeafInputSiteId, opt => opt.MapFrom(src => src.SiteId)) + .ForMember(dest => dest.LeafInputPhotosynthesisType, opt => opt.MapFrom(src => src.PhotosynthesisType.Name)) + //.ForMember(dest => dest.ErrorMessages, + // opt => opt.ResolveUsing( + // src => + // src.StatusHistory? + // .Where(sh => sh.Status == LeafInputStatusType.Exception) + // .Select(sh => sh.Description) + // .ToArray() + // ?? new string[] {})) + ; + }); + Mapper = config.CreateMapper(); + } + + public LeafInputViewModel(Core.Entities.LeafInput leafInput) + { + Mapper.Map(leafInput, this); + } + } +} \ No newline at end of file diff --git a/Web/Views/ResultsAdmin/Delete.cshtml b/Web/Views/ResultsAdmin/Delete.cshtml new file mode 100644 index 0000000..de93307 --- /dev/null +++ b/Web/Views/ResultsAdmin/Delete.cshtml @@ -0,0 +1,22 @@ +@model LeafWeb.Web.ViewModels.ResultsAdmin.LeafInputViewModel + +@{ + ViewBag.Title = "Delete LeafInput"; +} + +

@ViewBag.Title

+ +
Are you sure you wish to delete this LeafInput?
+ +
+ + @Html.DisplayForModel() + + @using (Html.BeginForm("Delete", "ResultsAdmin", FormMethod.Post, new { @class = "form-horizontal" })) + { +
+ + @Html.ActionLink("Cancel", "Details", new { id = Model.LeafInputId }, new { @class = "btn" }) +
+ } +
\ No newline at end of file diff --git a/Web/Views/ResultsAdmin/Index.cshtml b/Web/Views/ResultsAdmin/Index.cshtml new file mode 100644 index 0000000..3ea60a1 --- /dev/null +++ b/Web/Views/ResultsAdmin/Index.cshtml @@ -0,0 +1,28 @@ +@model IEnumerable + +@{ + ViewBag.Title = "Results Administration"; + var grid = new WebGrid(Model, rowsPerPage: 45); +} + +

@ViewBag.Title

+ +@grid.GetHtml(columns: + grid.Columns( + grid.Column("LeafInputIdentifier", "Identifier"), + grid.Column("LeafInputSiteId", "Site Id"), + grid.Column("LeafInputName", "Submitted By"), + grid.Column("CurrentStatus", "Status"), + grid.Column("Chart", "Chart", item => + item.HasLeafChart + ? Html.ActionLink("Chart", "Index", "Chart", new { leafInputId = item.LeafInputId }, new { }) + : Html.Raw("")), + grid.Column("Total Results: " + Model.Count(), format: + @
+ @Html.ActionLink("Edit", "Edit", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) + @Html.ActionLink("Details", "Details", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role = "button" }) + @Html.ActionLink("Delete", "Delete", new { id = item.LeafInputId }, new { @class = "btn btn-default btn-xs", role="button" }) +
) + ), + htmlAttributes: new { @class = "table table-striped table-bordered table-hover table-condensed" } + ) diff --git a/Web/Web.csproj b/Web/Web.csproj index 161a839..ff81908 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -636,6 +636,7 @@ + @@ -662,6 +663,7 @@ + @@ -699,6 +701,8 @@ + + Web.config