164 lines
4.7 KiB
C#
164 lines
4.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Security;
|
|
using System.Web.Mvc;
|
|
using MileageTraker.Web.Attributes;
|
|
using MileageTraker.Web.Models;
|
|
using MileageTraker.Web.ViewModels;
|
|
using MileageTraker.Web.ViewModels.CreateLog;
|
|
using MileageTraker.Web.ViewModels.Log;
|
|
|
|
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);
|
|
confirmCreateLogViewModel.Purpose.Available = GetPurposeTypesSelectList();
|
|
return View("Confirm", confirmCreateLogViewModel);
|
|
}
|
|
|
|
model.Purpose.Available = GetPurposeTypesSelectList();
|
|
return View(model);
|
|
}
|
|
|
|
[LogOwnerAuthorize]
|
|
public ActionResult EditPast(int id)
|
|
{
|
|
var log = DataService.GetLog(id);
|
|
var viewModel = new EditPastViewModel(log)
|
|
{
|
|
Purpose = { Available = GetPurposeTypesSelectList() }
|
|
};
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ActionLog]
|
|
public ActionResult EditPast(EditPastViewModel viewModel)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var log = DataService.GetLog(viewModel.LogId);
|
|
if (log.User.Username != User.Identity.Name)
|
|
throw new SecurityException();
|
|
viewModel.SetProperties(log);
|
|
|
|
if (viewModel.Purpose != null)
|
|
log.Purpose =
|
|
DataService.GetPurposeTypes()
|
|
.First(pt => pt.PurposeTypeId == viewModel.Purpose.Selected);
|
|
|
|
DataService.UpdateLog(log);
|
|
|
|
TempData["StatusMessage-Type"] = "alert-success";
|
|
TempData["StatusMessage"] =
|
|
@"You've successfully updated an entry
|
|
traveling to <strong>" + viewModel.CityName + @"</strong>
|
|
for <strong>" + log.Purpose.Purpose + @"</strong>
|
|
on <strong>" + viewModel.Date.ToShortDateString() + @"</strong>
|
|
in Vehicle Id <strong>" + viewModel.VehicleId + @"</strong>
|
|
ending in <strong>" + viewModel.EndOdometer + @"</strong>
|
|
miles on the odometer.";
|
|
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
viewModel.Purpose.Available = GetPurposeTypesSelectList();
|
|
return View(viewModel);
|
|
}
|
|
|
|
[HttpParamAction]
|
|
[HttpPost]
|
|
public ViewResult Edit(CreateLogViewModel model)
|
|
{
|
|
model.Purpose.Available = GetPurposeTypesSelectList();
|
|
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;
|
|
|
|
log.Purpose =
|
|
DataService.GetPurposeTypes()
|
|
.First(pt => pt.PurposeTypeId == model.Purpose.Selected);
|
|
|
|
DataService.AddLog(log);
|
|
var editLink = Url.Action("EditPast", "CreateLog", new {id = log.LogId});
|
|
|
|
TempData["StatusMessage-Type"] = "alert-success";
|
|
TempData["StatusMessage"] =
|
|
@"You've successfully created an entry
|
|
traveling to <strong>" + model.CityName + @"</strong>
|
|
for <strong>" + log.Purpose.Purpose + @"</strong>
|
|
on <strong>" + model.Date.ToShortDateString() + @"</strong>
|
|
in Vehicle Id <strong>" + model.VehicleId + @"</strong>
|
|
ending in <strong>" + model.EndOdometer + @"</strong>
|
|
miles on the odometer. " +
|
|
@"<a href='" + editLink + @"' class='btn btn-mini btn-success'>Edit</a>";
|
|
return RedirectToAction("Index");
|
|
}
|
|
|
|
model.Purpose.Available = GetPurposeTypesSelectList();
|
|
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,
|
|
Purpose =
|
|
new SelectListViewModel
|
|
{
|
|
Available =
|
|
GetPurposeTypesSelectList()
|
|
}
|
|
};
|
|
}
|
|
}
|
|
} |