Initial
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user