diff --git a/Web/Controllers/CityController.cs b/Web/Controllers/CityController.cs index 4469f52..7e664f5 100644 --- a/Web/Controllers/CityController.cs +++ b/Web/Controllers/CityController.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Web.Mvc; +using MileageTraker.Web.Attributes; using MileageTraker.Web.Models; using MileageTraker.Web.Utility; @@ -8,13 +9,13 @@ namespace MileageTraker.Web.Controllers { [Authorize(Roles = "Administrator, Developer")] public class CityController : ControllerBase - { + { [Authorize(Roles = "Administrator, Developer, Driver")] public JsonResult Autocomplete(string term) - { + { var cities = DataService.GetCitiesAutocomplete(term); - return Json(cities, JsonRequestBehavior.AllowGet); - } + return Json(cities, JsonRequestBehavior.AllowGet); + } public ActionResult Index() { @@ -26,46 +27,36 @@ namespace MileageTraker.Web.Controllers public ActionResult Create(string county) { - var city = new City { County = county }; + var city = new City {County = county}; return View(city); } - //[HttpPost] - //public ActionResult Create([Bind(Exclude = "CityId")]City city) - //{ - // if (ModelState.IsValid) - // { - // DataService.AddCity(city); + [ActionLog] + [HttpPost] + public ActionResult Create([Bind(Exclude = "CityId")] City city) + { + if (ModelState.IsValid) + { + DataService.AddCity(city); - // TempData["StatusMessage"] = "City " + city.Name + " created"; - // return RedirectToAction("Index"); - // } + TempData["StatusMessage"] = "City " + city.Name + " created"; + return RedirectToAction("Index"); + } - // return View(city); - //} + return View(city); + } - //public ActionResult Edit(int id = 0) - //{ - // var city = DataService.FindCity(id); - // if (city == null) - // { - // TempData["StatusMessage"] = "Purpose " + id + " not found"; - // return RedirectToAction("Index"); - // } - // return View(city); - //} + public JsonResult CityNameAvailable(string name) + { + var city = DataService.FindCityByName(name); + return Json(city == null, JsonRequestBehavior.AllowGet); + } - //[HttpPost] - //public ActionResult Edit(City city) - //{ - // if (ModelState.IsValid) - // { - // DataService.UpdateCity(city); - - // TempData["StatusMessage"] = "Changes to Purpose " + city.Purpose + " saved"; - // return RedirectToAction("Index"); - // } - // return View(city); - //} - } + [Authorize(Roles = "Administrator, Developer, Driver")] + public JsonResult CityNameExists(string cityname) + { + var city = DataService.FindCityByName(cityname); + return Json(city != null, JsonRequestBehavior.AllowGet); + } + } } diff --git a/Web/Controllers/CreateLogController.cs b/Web/Controllers/CreateLogController.cs index 3c3b915..035f20a 100644 --- a/Web/Controllers/CreateLogController.cs +++ b/Web/Controllers/CreateLogController.cs @@ -270,6 +270,16 @@ namespace MileageTraker.Web.Controllers Action = GetFixLink(viewModel) }, 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! DataService.AddLog(log); diff --git a/Web/Controllers/LogController.cs b/Web/Controllers/LogController.cs index 458478d..94a6158 100644 --- a/Web/Controllers/LogController.cs +++ b/Web/Controllers/LogController.cs @@ -350,6 +350,16 @@ namespace MileageTraker.Web.Controllers Action = GetFixLink(viewModel) }, 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! DataService.AddLog(log); diff --git a/Web/DAL/DataService.cs b/Web/DAL/DataService.cs index 6ea2e9e..d15012c 100644 --- a/Web/DAL/DataService.cs +++ b/Web/DAL/DataService.cs @@ -492,6 +492,17 @@ namespace MileageTraker.Web.DAL 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 #region Membership @@ -595,5 +606,6 @@ namespace MileageTraker.Web.DAL } #endregion + } } \ No newline at end of file diff --git a/Web/Models/City.cs b/Web/Models/City.cs index 4acc2dc..5f4a0da 100644 --- a/Web/Models/City.cs +++ b/Web/Models/City.cs @@ -12,6 +12,7 @@ namespace MileageTraker.Web.Models [Display(Name = "City Name")] [InputSize("medium")] [Required] + [Remote("CityNameAvailable", "City", ErrorMessage = "City name already in use")] public string Name { get; set; } [Required] public string County { get; set; } diff --git a/Web/ViewModels/CreateLog/CreateLogViewModel.cs b/Web/ViewModels/CreateLog/CreateLogViewModel.cs index 0ae4348..a10b004 100644 --- a/Web/ViewModels/CreateLog/CreateLogViewModel.cs +++ b/Web/ViewModels/CreateLog/CreateLogViewModel.cs @@ -37,6 +37,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog [Display(Name = "Destination City")] [InputSize("medium")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] + [Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")] public string CityName { get; set; } [Display(Name = "Purpose")] diff --git a/Web/ViewModels/CreateLog/EditPastViewModel.cs b/Web/ViewModels/CreateLog/EditPastViewModel.cs index 744d5ff..006b0a3 100644 --- a/Web/ViewModels/CreateLog/EditPastViewModel.cs +++ b/Web/ViewModels/CreateLog/EditPastViewModel.cs @@ -39,6 +39,7 @@ namespace MileageTraker.Web.ViewModels.CreateLog [Display(Name = "Destination City")] [InputSize("medium")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] + [Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")] public string CityName { get; set; } [Display(Name = "Purpose")] diff --git a/Web/ViewModels/Log/LogViewModel.cs b/Web/ViewModels/Log/LogViewModel.cs index 01446b5..187d5b6 100644 --- a/Web/ViewModels/Log/LogViewModel.cs +++ b/Web/ViewModels/Log/LogViewModel.cs @@ -47,6 +47,7 @@ namespace MileageTraker.Web.ViewModels.Log [Display(Name = "City Name")] [InputSize("medium")] [StringLength(64, MinimumLength = 3, ErrorMessage = "Minimum 3 characters")] + [Remote("CityNameExists", "City", ErrorMessage = "City must exist in system")] public string CityName { get; set; } [Display(Name = "Purpose")] diff --git a/Web/Views/City/Index.cshtml b/Web/Views/City/Index.cshtml index b0bba4d..e96a520 100644 --- a/Web/Views/City/Index.cshtml +++ b/Web/Views/City/Index.cshtml @@ -13,13 +13,15 @@ {

@countyCities.Key

+

@foreach (var city in countyCities.Value) { @city } - @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" }) +

} \ No newline at end of file diff --git a/Web/Views/Shared/_Layout.cshtml b/Web/Views/Shared/_Layout.cshtml index ecbd4a6..24293db 100644 --- a/Web/Views/Shared/_Layout.cshtml +++ b/Web/Views/Shared/_Layout.cshtml @@ -46,7 +46,7 @@ @RenderBody()