Automap viewmodels for details display

This commit is contained in:
2020-07-02 08:55:52 -04:00
parent 2b5de1c4cd
commit 3dedac5591
14 changed files with 300 additions and 206 deletions
+141 -62
View File
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using LeafWeb.Core.Entities;
using LeafWeb.WebCms.App_Start;
using LeafWeb.WebCms.Models;
using NUnit.Framework;
@@ -9,71 +10,133 @@ namespace LeafWeb.WebCms.Tests.Models
[TestFixture]
public class LeafInputDetailsTests
{
private LeafInput GetLeafInput()
{
return new LeafInput
{
CurrentStatus = LeafInputStatusType.Complete,
OutputFiles = new[] { new LeafOutputFile { Filename = "OutputFilename.txt" } },
Added = DateTime.Today,
Email = "test@email.com",
Identifier = "Ident I Fier",
Name = "My Name",
PhotosynthesisType = new PhotosynthesisType { Id = "1", Name = "1", SortOrder = 1 },
InputFiles = new[]
{
new LeafInputFile
{
Filename = "MyFilename.ext",
Id = 3
}
},
StatusHistory = new List<LeafInputStatus> {
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(4,0,0)),
Description = "Added",
Details = "Uploaded from Web",
Id = 1,
Status = LeafInputStatusType.Pending
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(3,0,0)),
Description = "Uploading input data",
Id = 2,
Status = LeafInputStatusType.Starting
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(2,12,0)),
Description = "Running on server",
Id = 3,
Status = LeafInputStatusType.Running
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(1,0,0)),
Description = "Downloading results",
Id = 4,
Status = LeafInputStatusType.Finishing
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(0,12,0)),
Description = "All done",
Id = 5,
Status = LeafInputStatusType.Complete
}
}
};
}
[SetUp]
public void SetUp()
{
AutoMapperConfig.RegisterMappings();
}
private LeafInput GetLeafInput =>
new LeafInput
{
CurrentStatus = LeafInputStatusType.Complete,
OutputFiles = new[] {new LeafOutputFile {Filename = "OutputFilename.txt"}},
Added = DateTime.Today,
Email = "test@email.com",
Identifier = "Ident I Fier",
Name = "My Name",
PhotosynthesisType = new PhotosynthesisType {Id = "1", Name = "1", SortOrder = 1},
InputFiles = new[]
{
new LeafInputFile
{
Filename = "MyFilename.ext",
Id = 3
}
},
StatusHistory = new List<LeafInputStatus>
{
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(4, 0, 0)),
Description = "Added",
Details = "Uploaded from Web",
Id = 1,
Status = LeafInputStatusType.Pending
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(3, 0, 0)),
Description = "Uploading input data",
Id = 2,
Status = LeafInputStatusType.Starting
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(2, 12, 0)),
Description = "Running on server",
Id = 3,
Status = LeafInputStatusType.Running
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(1, 0, 0)),
Description = "Downloading results",
Id = 4,
Status = LeafInputStatusType.Finishing
},
new LeafInputStatus
{
DateTime = DateTime.Today.Subtract(new TimeSpan(0, 12, 0)),
Description = "All done",
Id = 5,
Status = LeafInputStatusType.Complete
}
},
LeafInputData = new List<LeafInputData>
{
new LeafInputData
{
Site = new LeafInputDataSite {Latitude = 10, Longitude = 20, SiteId = "MySiteId"},
SiteName = "MySite",
Id = 123,
Data = new List<LeafInputDataCurve>
{
new LeafInputDataCurve
{
CO2S = 3.1
}
}
}
}
};
[Test]
private LeafInputData GetLeafInputData =>
new LeafInputData
{
Site = new LeafInputDataSite {Latitude = 10, Longitude = 20, SiteId = "MySiteId"},
SiteName = "MySite",
Id = 123,
Data = new List<LeafInputDataCurve>
{
new LeafInputDataCurve
{
CO2S = 3.1
}
}
};
private LeafInputDataCurve GetLeafInputDataCurve =>
new LeafInputDataCurve
{
CO2S = 3.1
};
[Test]
public void CanConstructLeafInputDataCurve()
{
var leafInputDataCurve = GetLeafInputDataCurve;
var viewModel = new LeafInputDataCurveViewModel(leafInputDataCurve);
Assert.That(viewModel.CO2S, Is.EqualTo(3.1));
}
[Test]
public void CanConstructLeafInputData()
{
var leafInputData = GetLeafInputData;
var viewModel = new LeafInputDataViewModel(leafInputData);
Assert.That(viewModel.SiteName, Is.EqualTo("MySite"));
Assert.That(viewModel.Data, Is.Not.Null);
Assert.That(viewModel.Data, Has.Count.GreaterThan(0));
Assert.That(viewModel.Data[0].CO2S, Is.EqualTo(3.1));
}
[Test]
public void CanConstructFromLeafInputFile()
{
var leafInput = GetLeafInput();
var leafInput = GetLeafInput;
var viewModel = new LeafInputDetails(leafInput);
//Assert.That(viewModel.CurrentStatus, Is.EqualTo(leafInput.CurrentStatus.ToString()));
@@ -86,7 +149,23 @@ namespace LeafWeb.WebCms.Tests.Models
//Assert.That(viewModel., Is.EqualTo(leafInput.Identifier));
Assert.That(viewModel.SiteId, Is.EqualTo(leafInput.SiteId));
//Assert.That(viewModel.LeafInputPhotosynthesisType, Is.EqualTo(leafInput.PhotosynthesisType.Name));
Assert.That(viewModel.LeafInputData, Has.Count.EqualTo(1));
Assert.That(viewModel.LeafInputData[0].SiteName, Is.EqualTo("MySite"));
Assert.That(viewModel.LeafInputData[0].Id, Is.EqualTo(123));
Assert.That(viewModel.LeafInputData[0].Site, Is.Not.Null);
Assert.That(viewModel.LeafInputData[0].Site.SiteId, Is.EqualTo("MySiteId"));
Assert.That(viewModel.LeafInputData[0].Site.Latitude, Is.EqualTo(10));
Assert.That(viewModel.LeafInputData[0].Site.Longitude, Is.EqualTo(20));
Assert.That(viewModel.LeafInputData[0].Site.Elevation, Is.Null);
Assert.That(viewModel.LeafInputData[0].Data, Is.Not.Null);
Assert.That(viewModel.LeafInputData[0].Data, Has.Count.GreaterThan(0));
Assert.That(viewModel.LeafInputData[0].Data[0].CO2S, Is.EqualTo(3.1));
//Assert.That(viewModel.LeafInputData[0].Id, Is.EqualTo(123));
}
}
}
}
+57
View File
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
using LeafWeb.WebCms.Models;
namespace LeafWeb.WebCms.App_Start
{
public static class AutoMapperConfig
{
public static void RegisterMappings()
{
Mapper.CreateMap<LeafInputCreate, LeafInput>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
Mapper.CreateMap<LeafInput, LeafInputCreate>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
// LeafInputDetails
Mapper.CreateMap<LeafInputFile, string>().ConvertUsing(file => file?.Contents.GetString());
Mapper.CreateMap<LeafOutputFile, string>().ConvertUsing(file => file?.FileContents.Contents.GetString());
Mapper.CreateMap<LeafInputStatusType, string>().ConvertUsing(st => st.ToString());
Mapper.CreateMap<LeafInputStatusType, LeafInputStatus>().ConvertUsing(st => new LeafInputStatus());
Mapper.CreateMap<LeafInput, LeafInputDetails>()
.ForMember(dest => dest.LeafInputId, opt => opt.MapFrom(src => src.Id))
.ForMember(dest => dest.HasLeafChart,
opt => opt.ResolveUsing(src => src.OutputFiles.Any(o => o.IsLeafChartFile)));
// LeafInputData
Mapper.CreateMap<LeafInputData, LeafInputDataViewModel>();
// LeafInputDataCurveViewModel
Mapper.CreateMap<LeafInputDataCurve, LeafInputDataCurveViewModel>();
// LeafInputDataSite
Mapper.CreateMap<LeafInputDataSite, LeafInputDataSiteViewModel>();
// LeafInputStatus
Mapper.CreateMap<LeafInputStatus, LeafInputStatusViewModel>()
.ForMember(dest => dest.Status, opt => opt.MapFrom(li => li.Status.ToString()));
//AutoMapper.AutoMapperMappingException {"Missing type map configuration or unsupported mapping.
//
//Mapping types:
//HashSet`1 -> LeafInputData
//System.Collections.Generic.HashSet`1[[LeafWeb.Core.Entities.LeafInputDataCurve, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> LeafWeb.Core.Entities.LeafInputData
//
//Destination path:
//LeafInputDetails.LeafInputData.LeafInputData.LeafInputData0[0]
//
//Source value:
//System.Collections.Generic.HashSet`1[LeafWeb.Core.Entities.LeafInputDataCurve]"}
Mapper.CreateMap<ICollection<LeafInputDataCurve>, LeafInputData>()
.ForAllMembers(opt => opt.Ignore());
}
}
}
+4 -2
View File
@@ -17,8 +17,10 @@ namespace LeafWeb.WebCms.App_Start
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
DataService.RegisterInitializer();
HangfireBootstrapper.Instance.Start();
BackloadBundles.RegisterBundles(BundleTable.Bundles);
AutoMapperConfig.RegisterMappings();
HangfireBootstrapper.Instance.Start();
BackloadBundles.RegisterBundles(BundleTable.Bundles);
// route for downloading results, see also URL rewrite rules in web.config
RouteTable.Routes.MapRoute(
+1 -3
View File
@@ -49,9 +49,7 @@ namespace LeafWeb.WebCms.Controllers
{
return CurrentUmbracoPage();
}
//if (q == null)
// return CurrentUmbracoPage();
//var collection = q.GetNameValueCollection();
return RedirectToCurrentUmbracoPage(q.GetNameValueCollection());
}
+1 -34
View File
@@ -35,40 +35,7 @@ namespace LeafWeb.WebCms.Models
[Display(Name = "Range")]
[RegularExpression(@"^([0-9]|[1-8][0-9]|9[0-9]|1[0-7][0-9]|180)$", ErrorMessage = "&plusmn; longitude degrees")]
public string lonr { get; set; }
static LeafDataQuery()
{
//Mapper.CreateMap<LeafInputCreate, LeafInput>()
// .ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
//Mapper.CreateMap<LeafInput, LeafInputCreate>()
// .ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
}
public LeafDataQuery()
{
//PhotosynthesisType = new SelectListViewModel();
}
public NameValueCollection GetNameValueCollection(string prefix="")
{
var nvs = new NameValueCollection();
prefix =
string.IsNullOrEmpty(prefix)
? ""
: prefix + ".";
foreach (var pi in typeof(LeafDataQuery).GetProperties())
{
var value = pi.GetValue(this, null);
if (value == null)
continue;
nvs.Add(prefix + pi.Name, value?.ToString());
}
return nvs;
}
public bool HasExtendedParameters =>
!(
string.IsNullOrEmpty(siteid)
-8
View File
@@ -40,14 +40,6 @@ namespace LeafWeb.WebCms.Models
[EnforceTrue(ErrorMessage = "Terms of Service must be accepted to continue")]
public bool TermsOfService { get; set; }
static LeafInputCreate()
{
Mapper.CreateMap<LeafInputCreate, LeafInput>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
Mapper.CreateMap<LeafInput, LeafInputCreate>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
}
public LeafInputCreate()
{
PhotosynthesisType = new SelectListViewModel();
@@ -0,0 +1,19 @@
using AutoMapper;
using LeafWeb.Core.Entities;
namespace LeafWeb.WebCms.Models
{
public class LeafInputDataCurveViewModel
{
/// <summary>Sample CO2 concentration</summary>
//[ParseInfo(17, units: "umol/mol")]
public double? CO2S { get; set; }
public LeafInputDataCurveViewModel() {}
public LeafInputDataCurveViewModel(LeafInputDataCurve leafInputDataCurve)
{
Mapper.Map(leafInputDataCurve, this);
}
}
}
@@ -25,72 +25,8 @@ namespace LeafWeb.WebCms.Models
[DisplayFormat(DataFormatString = "{0} m")]
public double? Elevation { get; set; }
/*
/// <summary>the year when the A/Ci data is taken</summary>
[DisplayFormat(DataFormatString = "{0} year")]
public string SampleYear { get; set; }
/// <summary>the day of year (since 1 Jan) when the A/Ci data is taken</summary>
[ParseInfo(6, units: "day")]
public int? SampleDayOfYear { get; set; }
/// <summary>the approximate start day (since 1 Jan) of growing season</summary>
[ParseInfo(7, units: "day")]
public int? GrowSeasonStart { get; set; }
/// <summary>the approximate end day (since 1 Jan) of growing season</summary>
[ParseInfo(8, units: "day")]
public int? GrowSeasonEnd { get; set; }
/// <summary>stand age since the last disturbance</summary>
[ParseInfo(9, units: "year")]
public double? StandAge { get; set; }
/// <summary>the height of the canopy</summary>
[ParseInfo(10, units: "m")]
public double? CanopyHeight { get; set; }
/// <summary>the leaf area index in the middle of growing season</summary>
[ParseInfo(11, units: "m2/m2")]
public double? LeafAreaIndex { get; set; }
/// <summary>the species of the leaf sample</summary>
/// <remarks>don't leave blank between letters</remarks>
[ParseInfo(12, units: "dimensionless")]
public string SpeciesSampled { get; set; }
/// <summary>the average time interval between two consecutive A/Ci data points</summary>
[ParseInfo(13, alternateTitle: "AveTimeResolution", units: "minutes")]
public double? AverageTimeResolution { get; set; }
/// <summary>the height at which the leaf is located</summary>
[ParseInfo(14, units: "m")]
public double? SampleHeight { get; set; }
/// <summary>the age of the leaf</summary>
[ParseInfo(15, units: "day")]
public int? LeafAge { get; set; }
/// <summary>specific leaf area of the sample</summary>
[ParseInfo(16, units: "cm2/g")]
public double? SpecificLeafArea { get; set; }
/// <summary>dry leaf nitrogen content of the sample</summary>
[ParseInfo(17, units: "%")]
public double? LfNitrogenContent { get; set; }
/// <summary>dry leaf carbon content of the sample</summary>
[ParseInfo(18, units: "%")]
public double? LfCarbonContent { get; set; }
/// <summary>dry leaf phosphorus content of the sample</summary>
[ParseInfo(19, units: "%")]
public double? LfPhosphContent { get; set; }
*/
static LeafInputDataSiteViewModel()
{
Mapper.CreateMap<LeafInputDataSite, LeafInputDataSiteViewModel>();
}
public LeafInputDataSiteViewModel() {}
+30
View File
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using AutoMapper;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Utility;
namespace LeafWeb.WebCms.Models
{
/// <summary>
/// Descriptive information about the investigator,
/// contact information, the site,the sample leaf and its general environmental condition
/// </summary>
public class LeafInputDataViewModel
{
public int Id { get; set; }
[ParseInfo(3, alternateTitle: "Site name in full", exampleValue: "Your site's identifier / name")]
public string SiteName { get; set; }
public LeafInputDataSiteViewModel Site { get; set; }
public List<LeafInputDataCurveViewModel> Data { get; set; }
public LeafInputDataViewModel() {}
public LeafInputDataViewModel(LeafInputData data)
{
Mapper.Map(data, this);
}
}
}
+6 -23
View File
@@ -30,7 +30,7 @@ namespace LeafWeb.WebCms.Models
[Required(ErrorMessage = "Name required")]
[RegularExpression(@"[A-Za-z().]+(\s+[A-Za-z().]+)+", ErrorMessage = "Please provide full name")]
public string Name { get; set; }
[Display(Name = "Email address")]
[Required(ErrorMessage = "An email address is required")]
[DataType(DataType.EmailAddress)]
@@ -52,13 +52,6 @@ namespace LeafWeb.WebCms.Models
[Display(Name = "Time In Progress")]
public TimeSpan TimeInProgress { get; set; }
[UIHint("LeafInputDataSiteViewModels")]
public List<LeafInputDataSiteViewModel> Sites { get; set; }
[UIHint("LeafInputStatusViewModels")]
public List<LeafInputStatusViewModel> StatusHistory { get; set; }
[HiddenInput(DisplayValue = false)]
public bool HasLeafChart { get; set; }
@@ -76,22 +69,12 @@ namespace LeafWeb.WebCms.Models
[HiddenInput(DisplayValue = false)]
public bool IsCancellable { get; set; }
[UIHint("LeafInputDataViewModels")]
public List<LeafInputDataViewModel> LeafInputData { get; set; }
static LeafInputDetails()
{
Mapper.CreateMap<LeafInputFile, string>().ConvertUsing(file => file?.Contents.GetString());
Mapper.CreateMap<LeafOutputFile, string>().ConvertUsing(file => file?.FileContents.Contents.GetString());
Mapper.CreateMap<LeafInputStatusType, string>().ConvertUsing(st => st.ToString());
Mapper.CreateMap<LeafInputStatusType, LeafInputStatus>().ConvertUsing(st => new LeafInputStatus());
Mapper.CreateMap<LeafInputStatus, LeafInputStatusViewModel>();
Mapper.CreateMap<LeafInputDataSite, LeafInputDataSiteViewModel>();
Mapper.CreateMap<LeafInput, LeafInputDetails>()
.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.Sites, opt =>
opt.ResolveUsing(li => li.LeafInputData.Select(d => d.Site)));
}
[UIHint("LeafInputStatusViewModels")]
public List<LeafInputStatusViewModel> StatusHistory { get; set; }
public LeafInputDetails(LeafInput leafInput)
{
@@ -16,8 +16,6 @@ namespace LeafWeb.WebCms.Models
static LeafInputStatusViewModel()
{
Mapper.CreateMap<LeafInputStatus, LeafInputStatusViewModel>()
.ForMember(dest => dest.Status, opt => opt.MapFrom(li => li.Status.ToString()));
}
public LeafInputStatusViewModel() { }
+31
View File
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
namespace LeafWeb.WebCms.Utility
{
public static class NameValueCollectionUtil
{
public static NameValueCollection GetNameValueCollection(this object obj, string prefix = "")
{
var nvs = new NameValueCollection();
prefix =
string.IsNullOrEmpty(prefix)
? ""
: prefix + ".";
foreach (var pi in obj.GetType().GetProperties().Where(p => p.CanWrite))
{
var value = pi.GetValue(obj, null);
if (value == null)
continue;
nvs.Add(prefix + pi.Name, value.ToString());
}
return nvs;
}
}
}
@@ -1,14 +1,12 @@
@using System.Collections.ObjectModel
@model IEnumerable<LeafInputDataSiteViewModel>
@model ICollection<LeafInputDataViewModel>
@{
//Layout = "~/Views/Shared/DisplayTemplates/_FieldLayout.cshtml";
}
@Rend("Site Id", Model.Select(m => m.SiteId))
@Rend("Latitude [&deg;]", Model.Select(m => m.Latitude?.ToString()))
@Rend("Longitude [&deg;]", Model.Select(m => m.Longitude?.ToString()))
@Rend("Elevation [m]", Model.Select(m => m.Elevation?.ToString()))
@Rend("Site Id", Model.Select(m => m.SiteName))
@Rend("Latitude [&deg;]", Model.Select(m => m.Site.Latitude?.ToString()))
@Rend("Longitude [&deg;]", Model.Select(m => m.Site.Longitude?.ToString()))
@Rend("Elevation [m]", Model.Select(m => m.Site.Elevation?.ToString()))
@helper Rend(string label, IEnumerable<string> values)
{
+5 -1
View File
@@ -1066,7 +1066,7 @@
<Content Include="Views\Membership\PasswordReset.cshtml" />
<Content Include="Views\MacroPartials\Membership\PasswordResetRequest.cshtml" />
<Content Include="Views\Membership\PasswordResetRequest.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\LeafInputDataSiteViewModels.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\LeafInputDataViewModels.cshtml" />
<Content Include="Views\Shared\DisplayTemplates\_ViewDataCssClass.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
@@ -1084,6 +1084,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<Compile Include="App_Start\AutoMapperConfig.cs" />
<Compile Include="App_Start\HangfireStartup.cs" />
<Compile Include="App_Start\RegisterServices.cs" />
<Compile Include="Backload\Bundles\BackloadBundles.cs" />
@@ -1107,7 +1108,9 @@
<Compile Include="EventHandlers\MemberEvents.cs" />
<Compile Include="Models\ChartViewModel.cs" />
<Compile Include="Models\LeafDataQuery.cs" />
<Compile Include="Models\LeafInputDataCurveViewModel.cs" />
<Compile Include="Models\LeafInputDataSiteViewModel.cs" />
<Compile Include="Models\LeafInputDataViewModel.cs" />
<Compile Include="Models\PasswordResetForm.cs" />
<Compile Include="Models\PasswordResetRequestForm.cs" />
<Compile Include="Models\ContactForm.cs" />
@@ -1126,6 +1129,7 @@
<Compile Include="Services\PiscalQueue\PiscalQueueWorker.cs" />
<Compile Include="Services\PiscalQueue\PiscalService.cs" />
<Compile Include="Services\PiscalQueue\StartPending.cs" />
<Compile Include="Utility\NameValueCollectionUtil.cs" />
<Compile Include="Utility\QueryFilter.cs" />
<Compile Include="Utility\RequireRequestValueAttribute.cs" />
<Compile Include="Utility\Validation.cs" />