using System; 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.Any()) 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() }; Session.Add("LogPage", Request.Url.PathAndQuery); 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); int logId; if (log.VehiclePreviousLog != null) { logId = log.VehiclePreviousLog.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 nextLog = _dataService.GetNextLog(id); 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"); ModelState.Remove("VehiclePreviousLogId"); ModelState.Remove("VehiclePreviousLog"); } 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); if (Session["LogPage"] != null) return Redirect((string) Session["LogPage"]); 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); } } }