107 lines
2.7 KiB
C#
107 lines
2.7 KiB
C#
using System;
|
|
using System.Web.Mvc;
|
|
using MileageTraker.Web.Attributes;
|
|
using MileageTraker.Web.Models;
|
|
using MileageTraker.Web.Utility;
|
|
using MileageTraker.Web.ViewModels.CreateLog;
|
|
|
|
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";
|
|
|
|
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
|
|
};
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |