Add ResultsAdmin, delete action
This commit is contained in:
+35
-1
@@ -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<T>(ICollection<T> collection, IDbSet<T> set) where T : class
|
||||
{
|
||||
if (collection == null) return;
|
||||
foreach (var entity in collection.ToArray())
|
||||
set.Remove(entity);
|
||||
}
|
||||
|
||||
#region Fluxnet Sites
|
||||
|
||||
public IQueryable<FluxnetSite> 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<LeafInput> 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
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Core.Entities.LeafInput, LeafInputViewModel>()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
@model LeafWeb.Web.ViewModels.ResultsAdmin.LeafInputViewModel
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Delete LeafInput";
|
||||
}
|
||||
|
||||
<h1>@ViewBag.Title</h1>
|
||||
|
||||
<div class="center-content label label-warning">Are you sure you wish to delete this LeafInput?</div>
|
||||
|
||||
<div class="center-content well">
|
||||
|
||||
@Html.DisplayForModel()
|
||||
|
||||
@using (Html.BeginForm("Delete", "ResultsAdmin", FormMethod.Post, new { @class = "form-horizontal" }))
|
||||
{
|
||||
<div class="form-actions">
|
||||
<input type="submit" value="Delete" class="btn btn-warning" />
|
||||
@Html.ActionLink("Cancel", "Details", new { id = Model.LeafInputId }, new { @class = "btn" })
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
@@ -0,0 +1,28 @@
|
||||
@model IEnumerable<LeafWeb.Web.ViewModels.Results.ResultStatusViewModel>
|
||||
|
||||
@{
|
||||
ViewBag.Title = "Results Administration";
|
||||
var grid = new WebGrid(Model, rowsPerPage: 45);
|
||||
}
|
||||
|
||||
<h1>@ViewBag.Title</h1>
|
||||
|
||||
@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:
|
||||
@<div class="btn-group" role="group">
|
||||
@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" })
|
||||
</div>)
|
||||
),
|
||||
htmlAttributes: new { @class = "table table-striped table-bordered table-hover table-condensed" }
|
||||
)
|
||||
@@ -636,6 +636,7 @@
|
||||
<Compile Include="Backload\Controller\BackloadController.obsolete.cs" />
|
||||
<Compile Include="Backload\Helper\ResultCreator.Classic.cs" />
|
||||
<Compile Include="Backload\Helper\ResultCreator.cs" />
|
||||
<Compile Include="Controllers\ResultsAdminController.cs" />
|
||||
<Compile Include="Services\DownloadUrlService.cs" />
|
||||
<Compile Include="Services\LeafGasCharter.cs" />
|
||||
<Compile Include="Controllers\ControllerBase.cs" />
|
||||
@@ -662,6 +663,7 @@
|
||||
<Compile Include="ViewModels\Chart\ChartViewModel.cs" />
|
||||
<Compile Include="ViewModels\LeafInput\ConfirmViewModel.cs" />
|
||||
<Compile Include="ViewModels\LeafInput\CreateViewModel.cs" />
|
||||
<Compile Include="ViewModels\ResultsAdmin\ResultStatusViewModel.cs" />
|
||||
<Compile Include="ViewModels\Results\ResultStatusViewModel.cs" />
|
||||
<Compile Include="ViewModels\SelectListViewModel.cs" />
|
||||
</ItemGroup>
|
||||
@@ -699,6 +701,8 @@
|
||||
<Content Include="Views\Pages\Information.cshtml" />
|
||||
<Content Include="Views\Results\DownloadNotFound.cshtml" />
|
||||
<Content Include="Views\Chart\DataError.cshtml" />
|
||||
<Content Include="Views\ResultsAdmin\Index.cshtml" />
|
||||
<Content Include="Views\ResultsAdmin\Delete.cshtml" />
|
||||
<None Include="Web.Debug.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</None>
|
||||
|
||||
Reference in New Issue
Block a user