Add Photosynthesis Type

This commit is contained in:
2016-02-09 13:47:11 -05:00
parent 15d911c86b
commit d101c2294f
14 changed files with 133 additions and 29 deletions
+10
View File
@@ -60,5 +60,15 @@ namespace LeafWeb.Core.DAL
} }
#endregion #endregion
public IQueryable<PhotosynthesisType> GetPhotosynthesisTypes()
{
return _db.PhotosynthesisTypes.OrderBy(pt => pt.SortOrder);
}
public PhotosynthesisType GetPhotosynthesisType(string id)
{
return _db.PhotosynthesisTypes.Find(id);
}
} }
} }
+3 -3
View File
@@ -22,9 +22,9 @@ namespace LeafWeb.Core.DAL
var photosynthesisTypes = new [] var photosynthesisTypes = new []
{ {
new PhotosynthesisType {Id = "C3_photosynthesis_leafweb", Name = "C3 Photosynthesis"}, new PhotosynthesisType {Id = "C3_photosynthesis_leafweb", Name = "C3 Photosynthesis", SortOrder = 1},
new PhotosynthesisType {Id = "C4_photosynthesis_leafweb", Name = "C4 Photosynthesis"}, new PhotosynthesisType {Id = "C4_photosynthesis_leafweb", Name = "C4 Photosynthesis", SortOrder = 2},
new PhotosynthesisType {Id = "CAM_photosynthesis_leafweb", Name = "CAM Photosynthesis"} new PhotosynthesisType {Id = "CAM_photosynthesis_leafweb", Name = "CAM Photosynthesis", SortOrder = 3}
}; };
context.PhotosynthesisTypes.AddRange(photosynthesisTypes); context.PhotosynthesisTypes.AddRange(photosynthesisTypes);
+2 -1
View File
@@ -23,7 +23,8 @@ namespace LeafWeb.Core.Models
[Required(ErrorMessage = "")] [Required(ErrorMessage = "")]
public string SiteId { get; set; } public string SiteId { get; set; }
public virtual PhotosynthesisType Photosynthesis { get; set; } [Required(ErrorMessage = "PhotosynthesisType required")]
public virtual PhotosynthesisType PhotosynthesisType { get; set; }
[DataType(DataType.Date)] [DataType(DataType.Date)]
[Required] [Required]
+2
View File
@@ -10,5 +10,7 @@ namespace LeafWeb.Core.Models
public string Id { get; set; } public string Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public int SortOrder { get; set; }
} }
} }
+5
View File
@@ -44,5 +44,10 @@ namespace LeafWeb.Web.Controllers
break; break;
} }
} }
protected SelectList GetPhotosynthesisTypeSelectList()
{
return new SelectList(DataService.GetPhotosynthesisTypes().ToList(), "Id", "Name");
}
} }
} }
+38 -21
View File
@@ -5,6 +5,7 @@ using System.Web;
using System.Web.Mvc; using System.Web.Mvc;
using LeafWeb.Core.Models; using LeafWeb.Core.Models;
using LeafWeb.Web.Attributes; using LeafWeb.Web.Attributes;
using LeafWeb.Web.ViewModels;
using LeafWeb.Web.ViewModels.LeafInput; using LeafWeb.Web.ViewModels.LeafInput;
namespace LeafWeb.Web.Controllers namespace LeafWeb.Web.Controllers
@@ -15,25 +16,9 @@ namespace LeafWeb.Web.Controllers
{ {
// initialize the session storage to retain SessionID between requests // initialize the session storage to retain SessionID between requests
Session["placeholder"] = 0; Session["placeholder"] = 0;
return View(); var viewModel = new CreateViewModel();
} HydrateCreateViewModel(viewModel);
return View(viewModel);
private FileInfo[] GetBackloadDirectoryFiles(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
return
!directory.Exists
? new FileInfo[] {}
: 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] [HttpParamAction]
@@ -50,9 +35,21 @@ namespace LeafWeb.Web.Controllers
{ {
// Go to confirmation // Go to confirmation
var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray()); var confirmViewModel = new ConfirmViewModel(viewModel, files.Select(f => f.Name).ToArray());
HydrateCreateViewModel(confirmViewModel);
return View("Confirm", confirmViewModel); return View("Confirm", confirmViewModel);
} }
return View("Index");
HydrateCreateViewModel(viewModel);
return View("Index", viewModel);
}
private void HydrateCreateViewModel(dynamic viewModel)
{
if (viewModel.PhotosynthesisType == null)
viewModel.PhotosynthesisType = new SelectListViewModel();
if (viewModel.PhotosynthesisType.ListItems == null)
viewModel.PhotosynthesisType.ListItems = GetPhotosynthesisTypeSelectList();
} }
[HttpParamAction] [HttpParamAction]
@@ -70,7 +67,7 @@ namespace LeafWeb.Web.Controllers
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
// convert viewModel into Model // convert viewModel into Model
var leafInput = viewModel.GetFileInput(); var leafInput = viewModel.GetFileInput(DataService);
// load files into LeafInputFile // load files into LeafInputFile
leafInput.LeafInputFiles = leafInput.LeafInputFiles =
(from f in files (from f in files
@@ -89,7 +86,27 @@ namespace LeafWeb.Web.Controllers
StatusType.Success); StatusType.Success);
return RedirectToAction("Index"); return RedirectToAction("Index");
} }
HydrateCreateViewModel(viewModel);
return View("Index", viewModel); return View("Index", viewModel);
} }
private FileInfo[] GetBackloadDirectoryFiles(string directoryName)
{
var path = Path.Combine(Server.MapPath("~/Files/"), directoryName + "\\");
var directory = new DirectoryInfo(path);
return
!directory.Exists
? new FileInfo[] { }
: 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);
}
} }
} }
@@ -7,10 +7,16 @@ namespace LeafWeb.Web.ViewModels.LeafInput
{ {
private static readonly IMapper Mapper; private static readonly IMapper Mapper;
[Display(Name = "Your name")]
public string Name { get; set; } public string Name { get; set; }
[Display(Name = "Your email")]
public string Email { get; set; } public string Email { get; set; }
[Display(Name = "Data identifier")]
public string Identifier { get; set; } public string Identifier { get; set; }
[Display(Name = "Site Id")]
public string SiteId { get; set; } public string SiteId { get; set; }
[Display(Name = "Photosynthetic Pathway")]
public SelectListViewModel PhotosynthesisType { get; set; }
public string[] Files { get; set; } public string[] Files { get; set; }
static ConfirmViewModel() static ConfirmViewModel()
+10 -2
View File
@@ -1,5 +1,6 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using AutoMapper; using AutoMapper;
using LeafWeb.Core.DAL;
namespace LeafWeb.Web.ViewModels.LeafInput namespace LeafWeb.Web.ViewModels.LeafInput
{ {
@@ -31,21 +32,28 @@ namespace LeafWeb.Web.ViewModels.LeafInput
[Required(ErrorMessage = "The site's name is required")] [Required(ErrorMessage = "The site's name is required")]
public string SiteId { get; set; } public string SiteId { get; set; }
[Display(Name = "Photosynthetic Pathways")]
[Required(ErrorMessage = "A photosynthesis pathway must be chosen")]
public SelectListViewModel PhotosynthesisType { get; set; }
static CreateViewModel() static CreateViewModel()
{ {
var config = var config =
new MapperConfiguration(cfg => new MapperConfiguration(cfg =>
{ {
cfg.CreateMap<CreateViewModel, Core.Models.LeafInput>(); cfg.CreateMap<CreateViewModel, Core.Models.LeafInput>()
.ForMember(dest => dest.PhotosynthesisType, opt => opt.Ignore());
}); });
Mapper = config.CreateMapper(); Mapper = config.CreateMapper();
} }
public Core.Models.LeafInput GetFileInput() public Core.Models.LeafInput GetFileInput(DataService db)
{ {
var leafInput = new Core.Models.LeafInput(); var leafInput = new Core.Models.LeafInput();
Mapper.Map(this, leafInput); Mapper.Map(this, leafInput);
leafInput.PhotosynthesisType = db.GetPhotosynthesisType(PhotosynthesisType.Selected);
return leafInput; return leafInput;
} }
} }
+21
View File
@@ -0,0 +1,21 @@
using System.Linq;
using System.Web.Mvc;
namespace LeafWeb.Web.ViewModels
{
public class SelectListViewModel
{
public SelectList ListItems { get; set; }
public string Selected { get; set; }
public SelectListItem SelectedItem
{
get { return ListItems.FirstOrDefault(li => li.Value == Selected); }
}
public override string ToString()
{
return Selected;
}
}
}
+10
View File
@@ -48,6 +48,16 @@
</dl> </dl>
@Html.HiddenFor(m => m.SiteId) @Html.HiddenFor(m => m.SiteId)
<dl class="dl-horizontal siteId">
<dt class="small">
@Html.DisplayNameFor(m => m.PhotosynthesisType)
</dt>
<dd>
@Html.DisplayTextFor(m => m.PhotosynthesisType.SelectedItem.Text)
</dd>
</dl>
<input type="hidden" name="PhotosynthesisType.Selected" value="@Model.PhotosynthesisType.Selected" />
<dl class="dl-horizontal files"> <dl class="dl-horizontal files">
<dt> <dt>
Files Files
+1
View File
@@ -21,6 +21,7 @@
@Html.EditorFor(m => m.EmailConfirm) @Html.EditorFor(m => m.EmailConfirm)
@Html.EditorFor(m => m.Identifier) @Html.EditorFor(m => m.Identifier)
@Html.EditorFor(m => m.SiteId) @Html.EditorFor(m => m.SiteId)
@Html.EditorFor(m => m.PhotosynthesisType)
<input type="submit" id="submit-form" class="hidden" /> <input type="submit" id="submit-form" class="hidden" />
} }
<!-- The file upload form used as target for the file upload widget --> <!-- The file upload form used as target for the file upload widget -->
@@ -0,0 +1,18 @@
@model LeafWeb.Web.ViewModels.SelectListViewModel
@{
Layout = "~/Views/Shared/EditorTemplates/_FieldLayout.cshtml";
}
@{
var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;
foreach (var li in Model.ListItems)
{
<div class="radio">
<label class="radio">
@Html.RadioButton(prefix + ".Selected", li.Value, li.Selected) @li.Text
</label>
</div>
}
ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}
@@ -2,14 +2,17 @@
@model object @model object
@{ @{
Layout = null; Layout = null;
var lowerPropertyName = @LeafWeb.Core.Utility.StringExtensions.LowercaseFirst(ViewData.ModelMetadata.PropertyName); var propertyName = ViewData.ModelMetadata.PropertyName;
var lowerPropertyName = @LeafWeb.Core.Utility.StringExtensions.LowercaseFirst(propertyName);
var values = ViewData.ModelMetadata.AdditionalValues; var values = ViewData.ModelMetadata.AdditionalValues;
var units = values.ContainsKey("Units") ? (string)values["Units"] : null; var units = values.ContainsKey("Units") ? (string)values["Units"] : null;
var currency = values.ContainsKey("Currency") ? (string)values["Currency"] : null; var currency = values.ContainsKey("Currency") ? (string)values["Currency"] : null;
var formatHint = values.ContainsKey("FormatHint") ? (string)values["FormatHint"] : null; var formatHint = values.ContainsKey("FormatHint") ? (string)values["FormatHint"] : null;
var editLabel = values.ContainsKey("EditLabel") ? (bool)values["EditLabel"] : true; var editLabel = values.ContainsKey("EditLabel") ? (bool)values["EditLabel"] : true;
var hasError = ViewData.ModelState[propertyName] != null && ViewData.ModelState[propertyName].Errors.Any();
var hasErrorClass = hasError ? "has-error" : string.Empty;
} }
<div class="form-group @lowerPropertyName"> <div class="form-group @lowerPropertyName @hasErrorClass">
@Html.LabelForModel(new { @class = "control-label" }) @Html.LabelForModel(new { @class = "control-label" })
@RenderBody() @RenderBody()
@Html.ValidationMessage("", new { @class = "help-block"}) @Html.ValidationMessage("", new { @class = "help-block"})
+2
View File
@@ -905,6 +905,7 @@
<Compile Include="Utility\Validation.cs" /> <Compile Include="Utility\Validation.cs" />
<Compile Include="ViewModels\LeafInput\ConfirmViewModel.cs" /> <Compile Include="ViewModels\LeafInput\ConfirmViewModel.cs" />
<Compile Include="ViewModels\LeafInput\CreateViewModel.cs" /> <Compile Include="ViewModels\LeafInput\CreateViewModel.cs" />
<Compile Include="ViewModels\SelectListViewModel.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Views\web.config" /> <Content Include="Views\web.config" />
@@ -940,6 +941,7 @@
<Content Include="Views\LeafInput\Confirm.cshtml" /> <Content Include="Views\LeafInput\Confirm.cshtml" />
<Content Include="Views\Shared\_StatusMessage.cshtml" /> <Content Include="Views\Shared\_StatusMessage.cshtml" />
<Content Include="Views\Shared\_ValidationField.cshtml" /> <Content Include="Views\Shared\_ValidationField.cshtml" />
<Content Include="Views\Shared\EditorTemplates\SelectListViewModel.cshtml" />
<None Include="Web.Debug.config"> <None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon> <DependentUpon>Web.config</DependentUpon>
</None> </None>