diff --git a/Web/Controllers/CityController.cs b/Web/Controllers/CityController.cs index 6ed4f13..4469f52 100644 --- a/Web/Controllers/CityController.cs +++ b/Web/Controllers/CityController.cs @@ -1,13 +1,71 @@ -using System.Web.Mvc; +using System; +using System.Linq; +using System.Web.Mvc; +using MileageTraker.Web.Models; +using MileageTraker.Web.Utility; namespace MileageTraker.Web.Controllers { - public class CityController : ControllerBase + [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); } + + public ActionResult Index() + { + var cities = DataService.GetCities().ToList(); + var t = cities.Select(c => Tuple.Create(c.County, c.Name)); + var groupTuple = CustomExtensions.GroupTuple(t); + return View(groupTuple); + } + + public ActionResult Create(string 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); + + // TempData["StatusMessage"] = "City " + city.Name + " created"; + // return RedirectToAction("Index"); + // } + + // 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); + //} + + //[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); + //} } } diff --git a/Web/DAL/DataService.cs b/Web/DAL/DataService.cs index 9d246af..6ea2e9e 100644 --- a/Web/DAL/DataService.cs +++ b/Web/DAL/DataService.cs @@ -487,7 +487,7 @@ namespace MileageTraker.Web.DAL select city.Name; } - private IQueryable GetCities() + public IQueryable GetCities() { return _db.Cities; } diff --git a/Web/Models/City.cs b/Web/Models/City.cs index a54810c..4acc2dc 100644 --- a/Web/Models/City.cs +++ b/Web/Models/City.cs @@ -1,12 +1,19 @@ using System.ComponentModel.DataAnnotations; +using System.Web.Mvc; +using MileageTraker.Web.Attributes; namespace MileageTraker.Web.Models { public class City { [Key] + [HiddenInput(DisplayValue = false)] public int CityId { get; set; } + [Display(Name = "City Name")] + [InputSize("medium")] + [Required] public string Name { get; set; } + [Required] public string County { get; set; } } } \ No newline at end of file diff --git a/Web/Utility/CustomExtensions.cs b/Web/Utility/CustomExtensions.cs index 74eead0..0e98cff 100644 --- a/Web/Utility/CustomExtensions.cs +++ b/Web/Utility/CustomExtensions.cs @@ -238,33 +238,47 @@ namespace MileageTraker.Web.Utility } public static Dictionary> YearMonthList(IEnumerable dates) + { + var tuples = + from d in dates + orderby d descending + select Tuple.Create(d.Year.ToString(), d.Month.ToString()); + + return GroupTuple(tuples); + //return + // (from d in dates + // orderby d.Year descending + // group d by d.Year + // into yg + // select + // new + // { + // Year = yg.Key.ToString(), + // MonthList = + // (from o in yg + // orderby o.Month descending + // group o by o.Month + // into mg + // select mg.Key.ToString()).ToList() + // }).ToDictionary(arg => arg.Year, arg => arg.MonthList); + } + + public static Dictionary> GroupTuple(IEnumerable> tuples) { return - (from d in dates - orderby d.Year descending - group d by d.Year + (from t in tuples + group t by t.Item1 into yg select new { - Year = yg.Key.ToString(), - MonthList = + Item1 = yg.Key, + Item2List = (from o in yg - orderby o.Month descending - group o by o.Month + group o by o.Item2 into mg - select mg.Key.ToString()).ToList() - }).ToDictionary(arg => arg.Year, arg => arg.MonthList); - } - - public static RouteValueDictionary ToRouteValueDictionary(this NameValueCollection collection) - { - var routeValueDictionary = new RouteValueDictionary(); - foreach (var key in collection.AllKeys) - { - routeValueDictionary.Add(key, collection[key]); - } - return routeValueDictionary; + select mg.Key).ToList() + }).ToDictionary(arg => arg.Item1, arg => arg.Item2List); } //public static IEnumerable ToSelectList( diff --git a/Web/Views/City/Create.cshtml b/Web/Views/City/Create.cshtml new file mode 100644 index 0000000..a0883a8 --- /dev/null +++ b/Web/Views/City/Create.cshtml @@ -0,0 +1,30 @@ +@model MileageTraker.Web.Models.City + +@{ + ViewBag.Title = "Create City"; +} + +

@ViewBag.Title

+ +@using (Html.BeginForm("Create", "City", FormMethod.Post, new { @class = "form-horizontal well center-content" })) +{ + @Html.Partial("_ValidationSummary") +
+ + @Html.DisplayFor(m => m.County) +@*
+
+ @Html.DisplayNameFor(m => m.County) +
+
+ @Html.DisplayTextFor(m => m.County) +
+
*@ + @Html.HiddenFor(m => m.County) + + @Html.EditorFor(m => m.Name) +
+ +
+
+} diff --git a/Web/Views/City/Index.cshtml b/Web/Views/City/Index.cshtml new file mode 100644 index 0000000..b0bba4d --- /dev/null +++ b/Web/Views/City/Index.cshtml @@ -0,0 +1,25 @@ +@model Dictionary> + +@{ + ViewBag.Title = "Cities by County"; +} + +@Html.Partial("_StatusMessage") + +

@ViewBag.Title

+ +
+ @foreach (var countyCities in Model) + { +
+

@countyCities.Key

+ @foreach (var city in countyCities.Value) + { + + @city + + } + @Html.ActionLink("Add New City", "Create", new {County=countyCities.Key}, new { @class = "btn" }) +
+ } +
\ No newline at end of file diff --git a/Web/Views/Log/Index.cshtml b/Web/Views/Log/Index.cshtml index 0657d58..ae44678 100644 --- a/Web/Views/Log/Index.cshtml +++ b/Web/Views/Log/Index.cshtml @@ -36,6 +36,7 @@ @Html.ActionLink("Add New Log", "Create", null, new { @class = "btn" }) @Html.ActionLink("Export", "Export", new { Model.Year, Model.Month, Model.LogType, Model.MonthRange, Model.VehicleId, Model.EmployeeName }, new { @class = "btn" }) @Html.ActionLink("Purposes", "Index", "Purpose", null, new { @class = "btn" }) + @Html.ActionLink("Cities", "Index", "City", null, new { @class = "btn" }) @Html.ActionLink("Import", "ImportUpload", null, new { @class = "btn"})
diff --git a/Web/Web.csproj b/Web/Web.csproj index 71b391f..415d217 100644 --- a/Web/Web.csproj +++ b/Web/Web.csproj @@ -257,6 +257,8 @@ + +