Files
MileageTraker/Web/Controllers/CreateLogController.cs
T
2013-01-12 14:58:33 -05:00

95 lines
2.6 KiB
C#

using System;
using System.Web.Mvc;
using MileageTraker.Web.Attributes;
using MileageTraker.Web.Models;
using MileageTraker.Web.ViewModels.CreateLog;
namespace MileageTraker.Web.Controllers
{
[Authorize(Roles = "Driver, Administrator, Developer")]
public class CreateLogController : ControllerBase
{
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)
{
var confirmCreateLogViewModel = new ConfirmCreateLogViewModel(model);
return View("Confirm", confirmCreateLogViewModel);
}
return View(model);
}
[HttpParamAction]
[HttpPost]
public ViewResult Edit(CreateLogViewModel model)
{
return View("Index", model);
}
[HttpParamAction]
[HttpPost]
[ActionLog]
public ActionResult Confirm(CreateLogViewModel model)
{
if (ModelState.IsValid)
{
SetCookieValue(CookieKeyLogtype, model.LogType.Enum.ToString());
SetCookieValue(CookeNameVehicleid, model.VehicleId);
// Save the new log
var log = model.GetLog();
log.User = DataService.FindUserByUsername(User.Identity.Name);
log.Source = HttpContext.Request.Url.AbsolutePath;
log.UserHostAddress = HttpContext.Request.UserHostAddress;
log.UserAgent = HttpContext.Request.UserAgent;
DataService.AddLog(log);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] =
@"You've successfully created an entry
traveling to <strong>" + model.CityName + @"</strong>
on <strong>" + model.Date.ToShortDateString() + @"</strong>
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
ending in <strong>" + model.EndOdometer + @"</strong>
miles on the odometer.";
return RedirectToAction("Index");
}
return View("Index", model);
}
public PartialViewResult RecentLogs()
{
var username = User.Identity.Name;
var user = DataService.FindUserByUsername(username);
var logs = DataService.GetRecentLogsByUsername(user.Username);
ViewData["name"] = user.FullName;
return PartialView(logs);
}
private CreateLogViewModel NewCreateLogModel()
{
MileageLogType logType;
Enum.TryParse(GetCookieValue(CookieKeyLogtype), true, out logType);
var vehicleId = GetCookieValue(CookeNameVehicleid);
return new CreateLogViewModel
{
LogType = logType,
VehicleId = vehicleId,
Date = DateTime.Today
};
}
}
}