This commit is contained in:
2012-11-30 21:35:06 -05:00
commit 3963d6363a
346 changed files with 351799 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
using System.Web.Mvc;
using MileageTraker.Web.DAL;
namespace MileageTraker.Web.Controllers
{
public class CityController : ControllerBase
{
private readonly DataService _dataService = new DataService();
public JsonResult Autocomplete(string term)
{
var cities = _dataService.GetCitiesAutocomplete(term);
return Json(cities, JsonRequestBehavior.AllowGet);
}
}
}
+21
View File
@@ -0,0 +1,21 @@
using System.Web.Mvc;
namespace MileageTraker.Web.Controllers
{
public class ControllerBase : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext != null && filterContext.Exception != null)
{
var controller = filterContext.RouteData.Values["controller"].ToString();
var action = filterContext.RouteData.Values["action"].ToString();
var loggerName = string.Format("{0}Controller.{1}", controller, action);
log4net.LogManager.GetLogger(loggerName).Error(string.Empty, filterContext.Exception);
}
base.OnException(filterContext);
}
}
}
+108
View File
@@ -0,0 +1,108 @@
using System;
using System.Web.Mvc;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels;
namespace MileageTraker.Web.Controllers
{
public class CreateLogController : ControllerBase
{
private const string CookieKeyEmployeename = "mr_employeeName";
private const string CookeNameVehicleid = "mr_vehicleId";
private const string CookieKeyLogtype = "mr_logType";
private readonly DataService _dataService = new DataService();
public ViewResult Index()
{
var createLogModel = NewCreateLogModel();
return View(createLogModel);
}
[HttpPost]
public ActionResult Index(CreateLogViewModel model)
{
if (!ModelState.IsValid)
{
ViewBag.updateError = "Create Failure";
if (model.LogType == null) // WTF why doesn't the model binder get this
model.LogType = new MileageLogTypeWrapper();
return View(model);
}
var confirmCreateLogViewModel = new ConfirmCreateLogViewModel(model);
return View("Confirm", confirmCreateLogViewModel);
}
[HttpParamAction]
[HttpPost]
public ViewResult Edit(CreateLogViewModel model)
{
return View("Index", model);
}
[HttpParamAction]
[HttpPost]
[ActionLog]
public ViewResult Confirm(CreateLogViewModel model)
{
if (ModelState.IsValid)
{
SetCookieValue(CookieKeyLogtype, model.LogType.Enum.ToString());
SetCookieValue(CookieKeyEmployeename, model.EmployeeName);
SetCookieValue(CookeNameVehicleid, model.VehicleId);
// Save the new log
var log = model.GetLog();
log.Source = HttpContext.Request.Url.AbsolutePath;
log.UserHostAddress = HttpContext.Request.UserHostAddress;
log.UserAgent = HttpContext.Request.UserAgent;
_dataService.AddLog(log);
return View("Success", model);
}
return View("Index", model);
}
public PartialViewResult RecentLogs(string employeeName)
{
var logs = _dataService.GetRecentLogsByEmployee(employeeName);
ViewData["employeeName"] = employeeName;
return PartialView(logs);
}
private CreateLogViewModel NewCreateLogModel()
{
MileageLogType logType;
Enum.TryParse(GetCookieValue(CookieKeyLogtype), true, out logType);
var name = GetCookieValue(CookieKeyEmployeename);
var vehicleId = GetCookieValue(CookeNameVehicleid);
return new CreateLogViewModel
{
LogType = logType,
EmployeeName = name,
VehicleId = vehicleId,
Date = DateTime.Today.ToShortDateString()
};
}
private string GetCookieValue(string key)
{
return
HttpContext.Request.Cookies[key] != null
? HttpContext.Request.Cookies[key].Value
: null;
}
private void SetCookieValue(string key, string value)
{
var cookies = HttpContext.Response.Cookies;
cookies[key].Value = value;
cookies[key].Expires = DateTime.MaxValue;
}
}
}
+16
View File
@@ -0,0 +1,16 @@
using System.Web.Mvc;
using MileageTraker.Web.DAL;
namespace MileageTraker.Web.Controllers
{
public class EmployeeController : Controller
{
private readonly DataService _dataService = new DataService();
public JsonResult Autocomplete(string term)
{
var employees = _dataService.GetEmployeeNamesAutocomplete(term);
return Json(employees, JsonRequestBehavior.AllowGet);
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using System.Web.Mvc;
namespace MileageTraker.Web.Controllers
{
public class HomeController : ControllerBase
{
public ActionResult About()
{
return View();
}
}
}
+207
View File
@@ -0,0 +1,207 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.Utility;
using MileageTraker.Web.ViewModels;
namespace MileageTraker.Web.Controllers
{
public class LogController : ControllerBase
{
private readonly DataService _dataService = new DataService();
public ViewResult Index(LogQueryViewModel query)
{
var logs = _dataService.GetLogs();
var validLogYears = _dataService.GetValidLogYears().ToList();
if (validLogYears.Count() == 0)
return View("Empty");
if (!query.Year.HasValue)
query.Year = validLogYears.FirstOrDefault();
var validLogMonths = _dataService.GetValidLogMonths(query.Year.Value).ToList();
if (!query.Month.HasValue)
query.Month = validLogMonths.FirstOrDefault();
var filteredLogs =
from log in _dataService.GetLogIndexViewModels(DataService.FilterLogs(logs, query))
orderby log.Created descending
select log;
var viewModel =
new LogResultsViewModel
{
Logs = filteredLogs,
Years = validLogYears,
Months = validLogMonths,
SelectedYear = query.Year.Value,
SelectedMonth = query.Month.Value,
SelectedLogType = query.LogType.ToString()
};
return View(viewModel);
}
[ActionLog]
public ActionResult Export(LogQueryViewModel query)
{
var logs = _dataService.GetLogs();
var name = string.Format(
"MileageLogs{0}-{1}{2}",
query.Year,
query.Month,
query.LogType.HasValue ? "-" + query.LogType.Value.GetDisplayShortName() : "");
var export = ExcelWriter.WriteXls(DataService.FilterLogs(logs, query), name, name);
return File(export, "application/ms-excel", name + ".xls");
}
public ViewResult MonthlyVehicleMileage(LogQueryViewModel query)
{
var items = _dataService.GetMonthlyVehicleMileageItems(query);
var report = new VehicleMileageViewModel (items, query);
return View(report);
}
public ActionResult MonthlyEmployeeMileage(LogQueryViewModel query)
{
var items = _dataService.GetMonthlyEmployeeMileageItems(query);
var report = new EmployeeMileageViewModel(items, query);
return View(report);
}
public ViewResult Details(int id)
{
var log = _dataService.GetLog(id);
return View(log);
}
public ActionResult PreviousDetails(int id)
{
var log = _dataService.GetLog(id);
var previousLog = _dataService.GetPreviousLog(log);
int logId;
if (previousLog != null)
{
logId = previousLog.LogId;
}
else
{
logId = id;
TempData["Message"] = "This is the first log for this vehicle";
}
return RedirectToAction("Details", new {id = logId});
}
public ActionResult NextDetails(int id)
{
var log = _dataService.GetLog(id);
var nextLog = _dataService.GetNextLog(log);
int logId;
if (nextLog != null)
{
logId = nextLog.LogId;
}
else
{
logId = id;
TempData["Message"] = "This is the most recent log for this vehicle";
}
return RedirectToAction("Details", new { id = logId });
}
public ActionResult Create()
{
var log = new Log { Date = DateTime.Today };
var vehicleId = Request["vehicleId"];
if (vehicleId != null)
{
log.VehicleId = vehicleId;
}
return View(log);
}
[HttpPost]
[ActionLog]
public ActionResult Create(Log log)
{
if (ModelState.IsValid)
{
log.Source = HttpContext.Request.Url.AbsolutePath;
log.UserHostAddress = HttpContext.Request.UserHostAddress;
log.UserAgent = HttpContext.Request.UserAgent;
_dataService.AddLog(log);
return RedirectToAction("Index");
}
return View(log);
}
public ActionResult Edit(int id)
{
var log = _dataService.GetLog(id);
return View(log);
}
[HttpPost]
[ActionLog]
public ActionResult Edit(Log log)
{
RemoveModelStateErrors();
if (ModelState.IsValid)
{
_dataService.UpdateLog(log);
return RedirectToAction("Details", new{id = log.LogId});
}
return View(log);
}
private void RemoveModelStateErrors()
{
ModelState.Remove("Source");
ModelState.Remove("UserAgent");
ModelState.Remove("UserHostAddress");
}
public ActionResult Delete(int id)
{
var log = _dataService.GetLog(id);
return View(log);
}
[HttpPost, ActionName("Delete")]
[ActionLog]
public ActionResult DeleteConfirmed(int id)
{
_dataService.DeleteLog(id);
return RedirectToAction("Index");
}
public JsonResult GetValidLogMonths(int year)
{
var validLogMonths = _dataService.GetValidLogMonths(year);
return Json(validLogMonths, JsonRequestBehavior.AllowGet);
}
public PartialViewResult DetailsPartial(int id)
{
var log = _dataService.GetLog(id);
return PartialView(new LogPartialDetails(log));
}
protected override void Dispose(bool disposing)
{
_dataService.Dispose();
base.Dispose(disposing);
}
}
}
+84
View File
@@ -0,0 +1,84 @@
using System;
using System.Web.Mvc;
using MileageTraker.Web.DAL;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels;
namespace MileageTraker.Web.Controllers
{
public class VehicleController : ControllerBase
{
private readonly DataService _ds = new DataService();
public ViewResult Index()
{
var vehicles = _ds.GetVehicles();
return View(vehicles);
}
public ViewResult Details(string id)
{
var vehicle = _ds.GetVehicle(id);
return View(vehicle);
}
public PartialViewResult DetailsPartial(string id)
{
var vehicle = _ds.GetVehicle(id);
return PartialView(new VehiclePartialDetails(vehicle));
}
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Vehicle vehicle)
{
if (ModelState.IsValid)
{
_ds.AddVehicle(vehicle);
return RedirectToAction("Index");
}
return View(vehicle);
}
public ActionResult Edit(string id)
{
var vehicle = _ds.GetVehicle(id);
return View(vehicle);
}
[HttpPost]
public ActionResult Edit(Vehicle vehicle)
{
if (ModelState.IsValid)
{
_ds.UpdateVehicle(vehicle);
return RedirectToAction("Details", new { id = vehicle.VehicleId });
}
return View(vehicle);
}
public JsonResult Exists(string vehicleId)
{
var vehicle = _ds.GetVehicle(vehicleId);
return Json(vehicle != null ? true : false, JsonRequestBehavior.AllowGet);
}
public FileResult Export()
{
var vehicles = _ds.GetVehicles();
var export = VehicleImporter.Export(vehicles);
return File(export, "application/ms-excel", string.Format("ETHRAVehicles{0:yyyy-MM-dd}.xls", DateTime.Today));
}
protected override void Dispose(bool disposing)
{
_ds.Dispose();
base.Dispose(disposing);
}
}
}