Methodize status message handling

This commit is contained in:
2015-10-10 22:42:38 -04:00
parent 6482955b3c
commit 22e4089fba
10 changed files with 71 additions and 66 deletions
+7 -9
View File
@@ -37,7 +37,7 @@ namespace MileageTraker.Web.Controllers
if (success)
{
FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
TempData["StatusMessage"] = "Logged in as " + model.Username;
SetStatusMessage("Logged in as " + model.Username);
return RedirectToLocal(returnUrl);
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
@@ -70,7 +70,7 @@ namespace MileageTraker.Web.Controllers
[ValidateAntiForgeryToken]
public ActionResult LogOff()
{
TempData["StatusMessage"] = User.Identity.Name + " logged off";
SetStatusMessage(User.Identity.Name + " logged off");
FormsAuthentication.SignOut();
@@ -117,7 +117,7 @@ namespace MileageTraker.Web.Controllers
if (changePasswordSucceeded)
{
TempData["StatusMessage"] = "Your password has been changed.";
SetStatusMessage("Your password has been changed.");
return RedirectToAction("Manage");
}
ModelState.AddModelError("", "The current password is incorrect or the new password is invalid.");
@@ -169,18 +169,16 @@ namespace MileageTraker.Web.Controllers
var email = new EmailNotificationService();
var resetPasswordUrl = ResetPassword(user);
email.SendResetPassword(user, resetPasswordUrl);
TempData["StatusMessage"] = "Please check your email - we have sent a request for you to reset the password.";
SetStatusMessage("Please check your email - we have sent a request for you to reset the password.");
}
else if (user == null)
{
TempData["StatusMessage"] = "Could not find username " + viewModel.Username + ".";
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("Could not find username " + viewModel.Username + ".", StatusType.Error);
return RedirectToAction("ResetPassword");
}
else if (!user.IsApproved)
{
TempData["StatusMessage"] = "Account is disabled for " + viewModel.Username + ".";
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("Account is disabled for " + viewModel.Username + ".", StatusType.Error);
}
// even when if not successful, let the user think they're getting a cookie
@@ -232,7 +230,7 @@ namespace MileageTraker.Web.Controllers
if (success)
{
FormsAuthentication.SetAuthCookie(viewModel.Username, false);
TempData["StatusMessage"] = "Password set for " + viewModel.Username + ", logged in";
SetStatusMessage("Password set for " + viewModel.Username + ", logged in");
return RedirectToAction("Index", "CreateLog");
}
}
+1 -2
View File
@@ -47,8 +47,7 @@ namespace MileageTraker.Web.Controllers
{
DataService.AddCity(city);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] = "City " + city.Name + " created";
SetStatusMessage("City " + city.Name + " created", StatusType.Success);
return RedirectToAction("Index");
}
+21
View File
@@ -108,5 +108,26 @@ namespace MileageTraker.Web.Controllers
select msg;
return errorList;
}
protected enum StatusType
{
Info,
Success,
Error
}
protected void SetStatusMessage(string msg, StatusType statusType = StatusType.Info)
{
TempData["StatusMessage"] = msg;
switch (statusType)
{
case StatusType.Success:
TempData["StatusMessage-Type"] = "alert-success";
break;
case StatusType.Error:
TempData["StatusMessage-Type"] = "alert-error";
break;
}
}
}
}
+8 -11
View File
@@ -71,15 +71,13 @@ namespace MileageTraker.Web.Controllers
DataService.UpdateLog(log);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] =
@"You've successfully updated an entry
SetStatusMessage(@"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.";
miles on the odometer.", StatusType.Success);
return RedirectToAction("Index");
}
@@ -121,8 +119,7 @@ namespace MileageTraker.Web.Controllers
DataService.AddLog(log);
var editLink = Url.Action("EditPast", "CreateLog", new {id = log.LogId});
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] =
SetStatusMessage(
@"You've successfully created an entry
traveling to <strong>" + model.CityName + @"</strong>
for <strong>" + log.Purpose.Purpose + @"</strong>
@@ -130,7 +127,8 @@ namespace MileageTraker.Web.Controllers
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>";
@"<a href='" + editLink + @"' class='btn btn-mini btn-success'>Edit</a>",
StatusType.Success);
return RedirectToAction("Index");
}
@@ -191,13 +189,12 @@ namespace MileageTraker.Web.Controllers
}
catch (OutOfMemoryException)
{
TempData["StatusMessage"] = "Problem reading excel document - please verify that the document is in Excel 97-2003 Workbook (.xls) format";
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage(
"Problem reading excel document - please verify that the document is in Excel 97-2003 Workbook (.xls) format", StatusType.Error);
}
catch (Exception ex)
{
TempData["StatusMessage"] = "Problem reading excel document: " + ex.Message;
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("Problem reading excel document: " + ex.Message, StatusType.Error);
}
}
return RedirectToAction("ImportUpload");
+1 -2
View File
@@ -150,8 +150,7 @@ namespace MileageTraker.Web.Controllers
}
catch (Exception ex)
{
TempData["StatusMessage"] = "Problem reading document: " + ex.Message;
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("Problem reading document: " + ex.Message, StatusType.Error);
}
finally
{
+10 -14
View File
@@ -107,7 +107,7 @@ namespace MileageTraker.Web.Controllers
else
{
logId = id;
TempData["StatusMessage"] = "This is the first log for this vehicle";
SetStatusMessage("This is the first log for this vehicle");
}
return RedirectToAction("Details", new {id = logId});
}
@@ -123,7 +123,7 @@ namespace MileageTraker.Web.Controllers
else
{
logId = id;
TempData["StatusMessage"] = "This is the most recent log for this vehicle";
SetStatusMessage("This is the most recent log for this vehicle");
}
return RedirectToAction("Details", new { id = logId });
}
@@ -161,8 +161,7 @@ namespace MileageTraker.Web.Controllers
DataService.AddLog(log);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] = "Log created";
SetStatusMessage("Log created", StatusType.Success);
if (TempData.ContainsKey("FuelLogId"))
{
@@ -173,8 +172,7 @@ namespace MileageTraker.Web.Controllers
DataService.UpdateFuelLog(fuelLog);
// return to the fuel log match page
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] = "Log created and matched to the fuel log";
SetStatusMessage("Log created and matched to the fuel log", StatusType.Success);
return RedirectToAction("Match", "FuelLog", new {id = fuelLogId});
}
@@ -213,7 +211,7 @@ namespace MileageTraker.Web.Controllers
DataService.UpdateLog(log);
TempData["StatusMessage"] = "Log updated";
SetStatusMessage("Log updated");
return RedirectToAction("Details", new{id = log.LogId});
}
@@ -237,7 +235,7 @@ namespace MileageTraker.Web.Controllers
{
DataService.DeleteLog(id);
TempData["StatusMessage"] = "Log deleted";
SetStatusMessage("Log deleted");
if (Session["LogPage"] != null)
return Redirect((string) Session["LogPage"]);
return RedirectToAction("Index");
@@ -265,8 +263,7 @@ namespace MileageTraker.Web.Controllers
DataService.FindUserByUsername(viewModel.UserName);
if (user == null)
{
TempData["StatusMessage"] = "User " + viewModel.UserName + " not found";
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("User " + viewModel.UserName + " not found", StatusType.Error);
}
else
{
@@ -284,13 +281,12 @@ namespace MileageTraker.Web.Controllers
}
catch (OutOfMemoryException)
{
TempData["StatusMessage"] = "Problem reading excel document - please verify that the document is in Excel 97-2003 Workbook (.xls) format";
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage(
"Problem reading excel document - please verify that the document is in Excel 97-2003 Workbook (.xls) format", StatusType.Error);
}
catch (Exception ex)
{
TempData["StatusMessage"] = "Problem reading excel document: " + ex.Message;
TempData["StatusMessage-Type"] = "alert-error";
SetStatusMessage("Problem reading excel document: " + ex.Message, StatusType.Error);
}
}
}
+4 -4
View File
@@ -17,7 +17,7 @@ namespace MileageTraker.Web.Controllers
var purposetype = DataService.FindPurposeType(id);
if (purposetype == null)
{
TempData["StatusMessage"] = "Purpose " + id + " not found";
SetStatusMessage("Purpose " + id + " not found");
return RedirectToAction("Index");
}
return View(purposetype);
@@ -37,7 +37,7 @@ namespace MileageTraker.Web.Controllers
{
DataService.AddPurposeType(purposetype);
TempData["StatusMessage"] = "Purpose " + purposetype.Purpose + " created";
SetStatusMessage("Purpose " + purposetype.Purpose + " created");
return RedirectToAction("Index");
}
@@ -49,7 +49,7 @@ namespace MileageTraker.Web.Controllers
var purposeType = DataService.FindPurposeType(id);
if (purposeType == null)
{
TempData["StatusMessage"] = "Purpose " + id + " not found";
SetStatusMessage("Purpose " + id + " not found");
return RedirectToAction("Index");
}
return View(purposeType);
@@ -62,7 +62,7 @@ namespace MileageTraker.Web.Controllers
{
DataService.UpdatePurposeType(purposeType);
TempData["StatusMessage"] = "Changes to Purpose " + purposeType.Purpose + " saved";
SetStatusMessage("Changes to Purpose " + purposeType.Purpose + " saved");
return RedirectToAction("Index");
}
return View(purposeType);
+10 -13
View File
@@ -60,7 +60,7 @@ namespace MileageTraker.Web.Controllers
DataService.FindUserByUsername(employeeName);
if (user == null)
{
TempData["StatusMessage"] = "User " + employeeName + " not found";
SetStatusMessage("User " + employeeName + " not found");
return RedirectToAction("Index");
}
return View("Details", user);
@@ -140,8 +140,7 @@ namespace MileageTraker.Web.Controllers
if (viewModel.SetPassword)
{
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] = "User " + user.Username + " created";
SetStatusMessage("User " + user.Username + " created", StatusType.Success);
return RedirectToAction("SetPassword", new { id = user.UserId });
}
@@ -149,8 +148,7 @@ namespace MileageTraker.Web.Controllers
var resetPasswordUrl = ResetPassword(user);
email.SendInitializePassword(user, resetPasswordUrl);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] = "User " + user.Username + " created, invitation sent to " + user.Email;
SetStatusMessage("User " + user.Username + " created, invitation sent to " + user.Email, StatusType.Success);
return RedirectToAction("Index");
}
@@ -175,7 +173,7 @@ namespace MileageTraker.Web.Controllers
var resetPasswordUrl = ResetPassword(user);
var email = new EmailNotificationService();
email.SendResetPassword(user, resetPasswordUrl);
TempData["StatusMessage"] = "Reset password sent to User " + user.Username + " at " + user.Email;
SetStatusMessage("Reset password sent to User " + user.Username + " at " + user.Email);
return RedirectToAction("Details", new { id = userId});
}
@@ -231,7 +229,7 @@ namespace MileageTraker.Web.Controllers
viewModel.Roles.Selected);
}
TempData["StatusMessage"] = "Changes saved for " + user.Username;
SetStatusMessage("Changes saved for " + user.Username);
return RedirectToAction("Details", new { id = viewModel.UserId});
}
return View(viewModel);
@@ -253,7 +251,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.UpdateUserPassword(viewModel.UserId, viewModel.NewPassword);
TempData["StatusMessage"] = "Password set for " + viewModel.Username;
SetStatusMessage("Password set for " + viewModel.Username);
return RedirectToAction("Details", new { id = viewModel.UserId});
}
@@ -271,15 +269,14 @@ namespace MileageTraker.Web.Controllers
}
if (user.Username == User.Identity.Name)
{
TempData["StatusMessage-Type"] = "alert-error";
TempData["StatusMessage"] = "Cannot disable yourself!";
SetStatusMessage("Cannot disable yourself!", StatusType.Error);
}
else
{
user.IsApproved = false;
DataService.UpdateUser(user);
TempData["StatusMessage"] = user.Username + " disabled";
SetStatusMessage(user.Username + " disabled");
}
if (Request.UrlReferrer != null)
@@ -298,7 +295,7 @@ namespace MileageTraker.Web.Controllers
user.IsApproved = true;
DataService.UpdateUser(user);
TempData["StatusMessage"] = user.Username + " enabled";
SetStatusMessage(user.Username + " enabled");
if (Request.UrlReferrer != null)
return Redirect(Request.UrlReferrer.AbsolutePath);
@@ -317,7 +314,7 @@ namespace MileageTraker.Web.Controllers
user.PasswordFailuresSinceLastSuccess = 0;
DataService.UpdateUser(user);
TempData["StatusMessage"] = user.Username + " unlocked";
SetStatusMessage(user.Username + " unlocked");
if (Request.UrlReferrer != null)
return Redirect(Request.UrlReferrer.AbsolutePath);
+6 -7
View File
@@ -42,8 +42,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.AddVehicle(vehicle);
TempData["StatusMessage"] =
string.Format("Vehicle {0} created", vehicle.VehicleId);
SetStatusMessage(string.Format("Vehicle {0} created", vehicle.VehicleId));
return RedirectToAction("Index");
}
@@ -63,7 +62,7 @@ namespace MileageTraker.Web.Controllers
if (ModelState.IsValid)
{
DataService.UpdateVehicle(vehicle);
TempData["StatusMessage"] = "Changes saved for vehicle " + vehicle.VehicleId;
SetStatusMessage("Changes saved for vehicle " + vehicle.VehicleId);
return RedirectToAction("Details", new { id = vehicle.VehicleId });
}
return View(vehicle);
@@ -90,13 +89,13 @@ namespace MileageTraker.Web.Controllers
var vehicle = DataService.GetVehicle(id);
if (vehicle.InactiveDate.HasValue)
{
TempData["StatusMessage"] = "Vehicle already inactive";
SetStatusMessage("Vehicle already inactive");
}
else
{
vehicle.InactiveDate = DateTime.Now;
DataService.UpdateVehicle(vehicle);
TempData["StatusMessage"] = string.Format("Vehicle set inactive on {0:MM/dd/yyyy}", vehicle.InactiveDate.Value);
SetStatusMessage(string.Format("Vehicle set inactive on {0:MM/dd/yyyy}", vehicle.InactiveDate.Value));
}
return RedirectToAction("Details", new {id});
}
@@ -107,13 +106,13 @@ namespace MileageTraker.Web.Controllers
var vehicle = DataService.GetVehicle(id);
if (!vehicle.InactiveDate.HasValue)
{
TempData["StatusMessage"] = "Vehicle is already active";
SetStatusMessage("Vehicle is already active");
}
else
{
vehicle.InactiveDate = null;
DataService.UpdateVehicle(vehicle);
TempData["StatusMessage"] = string.Format("Vehicle reactivated");
SetStatusMessage(string.Format("Vehicle reactivated"));
}
return RedirectToAction("Details", new { id });
}
+3 -4
View File
@@ -43,9 +43,8 @@ namespace MileageTraker.Web.Controllers
vehicleService.Vehicle = DataService.GetVehicle(viewModel.VehicleId);
DataService.AddVehicleService(vehicleService);
TempData["StatusMessage-Type"] = "alert-success";
TempData["StatusMessage"] =
string.Format("Vehicle Service for vehicle {0} created", viewModel.VehicleId);
SetStatusMessage(
string.Format("Vehicle Service for vehicle {0} created", viewModel.VehicleId), StatusType.Success);
return RedirectToAction("Index");
}
@@ -70,7 +69,7 @@ namespace MileageTraker.Web.Controllers
vehicleService.Vehicle = DataService.GetVehicle(viewModel.VehicleId);
DataService.UpdateVehicleService(vehicleService);
TempData["StatusMessage"] = "Changes saved for vehicle service " + vehicleService.Vehicle.VehicleId;
SetStatusMessage("Changes saved for vehicle service " + vehicleService.Vehicle.VehicleId);
return RedirectToAction("Details", new { id = vehicleService.VehicleServiceId });
}
return View(viewModel);