Add required city field

This commit is contained in:
2014-05-21 10:17:05 -04:00
parent b2f8baaca6
commit 365aa47328
10 changed files with 70 additions and 41 deletions
+30 -39
View File
@@ -1,6 +1,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Web.Mvc; using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models; using MileageTraker.Web.Models;
using MileageTraker.Web.Utility; using MileageTraker.Web.Utility;
@@ -8,13 +9,13 @@ namespace MileageTraker.Web.Controllers
{ {
[Authorize(Roles = "Administrator, Developer")] [Authorize(Roles = "Administrator, Developer")]
public class CityController : ControllerBase public class CityController : ControllerBase
{ {
[Authorize(Roles = "Administrator, Developer, Driver")] [Authorize(Roles = "Administrator, Developer, Driver")]
public JsonResult Autocomplete(string term) public JsonResult Autocomplete(string term)
{ {
var cities = DataService.GetCitiesAutocomplete(term); var cities = DataService.GetCitiesAutocomplete(term);
return Json(cities, JsonRequestBehavior.AllowGet); return Json(cities, JsonRequestBehavior.AllowGet);
} }
public ActionResult Index() public ActionResult Index()
{ {
@@ -26,46 +27,36 @@ namespace MileageTraker.Web.Controllers
public ActionResult Create(string county) public ActionResult Create(string county)
{ {
var city = new City { County = county }; var city = new City {County = county};
return View(city); return View(city);
} }
//[HttpPost] [ActionLog]
//public ActionResult Create([Bind(Exclude = "CityId")]City city) [HttpPost]
//{ public ActionResult Create([Bind(Exclude = "CityId")] City city)
// if (ModelState.IsValid) {
// { if (ModelState.IsValid)
// DataService.AddCity(city); {
DataService.AddCity(city);
// TempData["StatusMessage"] = "City " + city.Name + " created"; TempData["StatusMessage"] = "City " + city.Name + " created";
// return RedirectToAction("Index"); return RedirectToAction("Index");
// } }
// return View(city); return View(city);
//} }
//public ActionResult Edit(int id = 0) public JsonResult CityNameAvailable(string name)
//{ {
// var city = DataService.FindCity(id); var city = DataService.FindCityByName(name);
// if (city == null) return Json(city == null, JsonRequestBehavior.AllowGet);
// { }
// TempData["StatusMessage"] = "Purpose " + id + " not found";
// return RedirectToAction("Index");
// }
// return View(city);
//}
//[HttpPost] [Authorize(Roles = "Administrator, Developer, Driver")]
//public ActionResult Edit(City city) public JsonResult CityNameExists(string cityname)
//{ {
// if (ModelState.IsValid) var city = DataService.FindCityByName(cityname);
// { return Json(city != null, JsonRequestBehavior.AllowGet);
// DataService.UpdateCity(city); }
}
// TempData["StatusMessage"] = "Changes to Purpose " + city.Purpose + " saved";
// return RedirectToAction("Index");
// }
// return View(city);
//}
}
} }
+10
View File
@@ -270,6 +270,16 @@ namespace MileageTraker.Web.Controllers
Action = GetFixLink(viewModel) Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet); }, JsonRequestBehavior.AllowGet);
} }
// verify city exists
if (DataService.FindCityByName(log.CityName) == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "City must exist in system",
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// SAVE IT! // SAVE IT!
DataService.AddLog(log); DataService.AddLog(log);
+10
View File
@@ -350,6 +350,16 @@ namespace MileageTraker.Web.Controllers
Action = GetFixLink(viewModel) Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet); }, JsonRequestBehavior.AllowGet);
} }
// verify city exists
if (DataService.FindCityByName(log.CityName) == null)
{
return Json(new
{
Status = ImportStatus.Failure.ToString(),
Message = "City must exist in system",
Action = GetFixLink(viewModel)
}, JsonRequestBehavior.AllowGet);
}
// SAVE IT! // SAVE IT!
DataService.AddLog(log); DataService.AddLog(log);
+12
View File
@@ -492,6 +492,17 @@ namespace MileageTraker.Web.DAL
return _db.Cities; return _db.Cities;
} }
public City FindCityByName(string name)
{
return _db.Cities.FirstOrDefault(u => name.Equals(u.Name, StringComparison.InvariantCultureIgnoreCase));
}
public void AddCity(City city)
{
_db.Cities.Add(city);
_db.SaveChanges();
}
#endregion #endregion
#region Membership #region Membership
@@ -595,5 +606,6 @@ namespace MileageTraker.Web.DAL
} }
#endregion #endregion
} }
} }
+1
View File
@@ -12,6 +12,7 @@ namespace MileageTraker.Web.Models
[Display(Name = "City Name")] [Display(Name = "City Name")]
[InputSize("medium")] [InputSize("medium")]
[Required] [Required]
[Remote("CityNameAvailable", "City", ErrorMessage = "City name already in use")]
public string Name { get; set; } public string Name { get; set; }
[Required] [Required]
public string County { get; set; } public string County { get; set; }
@@ -37,6 +37,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog
[Display(Name = "Destination City")] [Display(Name = "Destination City")]
[InputSize("medium")] [InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; } public string CityName { get; set; }
[Display(Name = "Purpose")] [Display(Name = "Purpose")]
@@ -39,6 +39,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog
[Display(Name = "Destination City")] [Display(Name = "Destination City")]
[InputSize("medium")] [InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; } public string CityName { get; set; }
[Display(Name = "Purpose")] [Display(Name = "Purpose")]
+1
View File
@@ -47,6 +47,7 @@ namespace MileageTraker.Web.ViewModels.Log
[Display(Name = "City Name")] [Display(Name = "City Name")]
[InputSize("medium")] [InputSize("medium")]
[StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")]
[Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")]
public string CityName { get; set; } public string CityName { get; set; }
[Display(Name = "Purpose")] [Display(Name = "Purpose")]
+3 -1
View File
@@ -13,13 +13,15 @@
{ {
<div> <div>
<h3>@countyCities.Key</h3> <h3>@countyCities.Key</h3>
<p>
@foreach (var city in countyCities.Value) @foreach (var city in countyCities.Value)
{ {
<span class="label"> <span class="label">
@city @city
</span> </span>
} }
@Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn" }) @Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn btn-small" })
</p>
</div> </div>
} }
</div> </div>
+1 -1
View File
@@ -46,7 +46,7 @@
@RenderBody() @RenderBody()
</div> </div>
<footer> <footer>
Mileage Traker &copy; 2013 James Kolpack Mileage Traker &copy; 2014 Kolpack Software Consulting LLC
</footer> </footer>
<script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery-2.1.0.min.js")" type="text/javascript"></script>