public ActionResult Delete(int id)
        {
            DbChangeLog log = new DbChangeLog();
            LocHourCats deletedCat = context.DeleteLocHourCat(id);
            log.UserName = User.Identity.Name;
            log.Controller = ControllerContext.RouteData.Values["controller"].ToString();
            log.Action = ControllerContext.RouteData.Values["action"].ToString();
            log.ItemId = id;

            log.BeforeChange = Domain.Extensions.DbLogExtensions.LocCatToString(deletedCat);
            if (deletedCat != null)
            {
                log.Success = true;

                TempData["message"] = string.Format("{0} was deleted", deletedCat.Name);
            }
            else
            {
                log.Success = false;
                log.Error = "Unable to delete location";
                TempData["alert"] = "Sorry, there was an error, that location has not been deleted";
            }
            context.SaveLog(log);
            return RedirectToAction("Edit","Locations", new { id = deletedCat.LocationId });
        }
        public ActionResult Edit(LocHours locHours)
        {
            /***** Logging initial settings *****/
            DbChangeLog log = new DbChangeLog();
            log.UserName = User.Identity.Name;
            log.Controller = "LocHours";
            log.Action = (locHours.Id != 0) ? "Edit" : "Create";

            if (log.Action == "Edit")
            {
                LocHours oldHours = context.LocHours.FirstOrDefault(m => m.Id == locHours.Id);
                log.BeforeChange = Domain.Extensions.DbLogExtensions.LocHoursToString(oldHours);
            }
            log.AfterChange = Domain.Extensions.DbLogExtensions.LocHoursToString(locHours);
            /***** end Logging initial settings *****/

            if (ModelState.IsValid)
            {
                try
                {
                    context.SaveLocHours(locHours);
                    log.ItemId = locHours.Id;

                    log.Success = true;
                    TempData["message"] = string.Format("{0} : {1} has been saved", locHours.Days, locHours.Hours);
                }
                catch (Exception e)
                {
                    log.Error = e.ToString();
                    log.Success = false;
                    TempData["alert"] = string.Format("There has been an error. {0} : {1} has not been saved", locHours.Days, locHours.Hours);
                }

            }
            else
            {

                log.Error = "Errors: ";
                NLogLogger logger = new NLogLogger();
                logger.Info("There has been a validation error, this record has not been saved");
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        log.Error += error + "<br />";

                    }
                }
                TempData["alert"] = "There has been a validation error, this record has not been saved";

            }
            log.AfterChange = Domain.Extensions.DbLogExtensions.LocHoursToString(locHours);
            context.SaveLog(log);
            return RedirectToAction("Edit",new { id = locHours.Id });
        }
        public ActionResult Edit(LocHourCats locHourCats)
        {
            /***** Logging initial settings *****/

                DbChangeLog log = new DbChangeLog();
                log.UserName = User.Identity.Name;
                log.Controller = "LocHourCats";
                log.Action = (locHourCats.Id != 0) ? "Edit" : "Create";
                log.ItemId = locHourCats.Id;
                // if this is an edit to an exhisting item, record the old item to the log
                if (log.Action == "Edit")
                {
                    LocHourCats oldCat = context.LocHourCats.FirstOrDefault(c => c.Id == locHourCats.Id);
                    log.BeforeChange = Domain.Extensions.DbLogExtensions.LocCatToString(oldCat);
                }

                // record the newly attempted change
                log.AfterChange = Domain.Extensions.DbLogExtensions.LocCatToString(locHourCats);

            /***** end Logging initial settings *****/

            if (ModelState.IsValid)
            {

                try
                {
                    context.SaveLocHourCat(locHourCats);

                    // need to record the id here, if this item has just been created it will not have an ID until it has been recorded to the DB
                    log.ItemId = locHourCats.Id;
                    log.Success = true;
                    TempData["message"] = string.Format("{0} has been saved", locHourCats.Name);
                }

                catch (Exception e)
                {
                    log.Error = e.ToString();
                    log.Success = false;
                    TempData["alert"] = "There has been an error. That item has not been saved";
                }

            }
            else
            {

                TempData["alert"] = string.Format("{0} has not been saved", locHourCats.Name);

                // record the errors and error status to the log
                log.Success = false;
                log.Error = "Errors: ";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        log.Error += error + "<br />";

                    }
                }
            }

            context.SaveLog(log);
            return RedirectToAction("Edit",new { Id = locHourCats.Id });
        }
        public ActionResult Edit(Location loc)
        {
            /***** Logging initial settings *****/
            DbChangeLog log = new DbChangeLog();
            log.UserName = User.Identity.Name;
            log.Controller = "Locations";
            log.Action = (loc.Id != 0) ? "Edit" : "Create";

            if (log.Action == "Edit")
            {
                Location oldLoc = repo.Locations.FirstOrDefault(m => m.Id == loc.Id);
                log.BeforeChange = Domain.Extensions.DbLogExtensions.LocationToString(oldLoc);
            }
            /***** end Logging initial settings *****/

            if (ModelState.IsValid)
            {
                try {
                    repo.SaveLocation(loc);
                    log.AfterChange = Domain.Extensions.DbLogExtensions.LocationToString(loc);
                    log.Success = true;
                    TempData["message"] = string.Format("{0} has been saved", loc.Name);
                }

                catch(Exception e)
                {
                    log.Error = e.ToString();
                    log.Success = false;
                    TempData["alert"] = string.Format("There has been an error. {0} has not been saved", loc.Name);
                }
                log.ItemId = loc.Id;

            }
            else
            {
                log.Error = "Errors: ";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        log.Error += error + "<br />";

                    }
                }
                TempData["alert"] = string.Format("{0} has not been saved");

            }
            repo.SaveLog(log);
            Location retLoc = repo.Locations.FirstOrDefault(p => p.Id == loc.Id);
            return View(retLoc);
        }
        public ActionResult Manage([Bind(Include = "Id, LocationId, FeaturedImg")]LocImage LocImage, HttpPostedFileBase image)
        {
            /***** Logging initial settings *****/

            DbChangeLog log = new DbChangeLog();
            log.UserName = User.Identity.Name;
            log.Controller = "LocImage";
            Console.WriteLine("LocImage ID = " + LocImage.Id);
            log.Action = (LocImage.Id == 0) ? "Create" : "Edit";
            log.ItemId = LocImage.Id;
            // if this is an edit to an exhisting item, record the old item to the log
            if (log.Action == "Edit")
            {
                LocImage oldImage = context.ImageByLocationID(LocImage.LocationId);
                log.BeforeChange = Domain.Extensions.DbLogExtensions.LocImageToString(oldImage, "Image is being replaced");
            }
            if(image != null)
            {
                LocImage.ImageMimeType = image.ContentType;
                LocImage.ImageData = new byte[image.ContentLength];
                image.InputStream.Read(LocImage.ImageData, 0, image.ContentLength);
            }
            // record the newly attempted change

            /***** end Logging initial settings *****/

            if (ModelState.IsValid)
            {
                try
                {
                    context.SaveLocImage(LocImage);

                    // need to record the id here, if this item has just been created it will not have an ID until it has been recorded to the DB
                    log.ItemId = LocImage.Id;
                    log.Success = true;
                    log.AfterChange = Domain.Extensions.DbLogExtensions.LocImageToString(LocImage, "");
                    TempData["message"] = string.Format("The image has been saved");
                }

                catch (Exception e)
                {
                    log.Error = e.ToString();
                    log.Success = false;
                    TempData["alert"] = "There has been an error. That item has not been saved";
                }

            }
            else
            {

                TempData["alert"] = string.Format("The image has not been saved. Please try again.");

                // record the errors and error status to the log
                log.Success = false;
                log.Error = "Errors: ";
                foreach (ModelState modelState in ViewData.ModelState.Values)
                {
                    foreach (ModelError error in modelState.Errors)
                    {
                        log.Error += error + "<br />";

                    }
                }
            }

            context.SaveLog(log);

            return RedirectToAction("Edit","Locations",new { id = LocImage.LocationId });
        }
        public async Task<ActionResult> Manage(ManageUserViewModel model)
        { 
                /***** Logging initial settings *****/
                DbChangeLog log = new DbChangeLog();
                log.UserName = User.Identity.Name;
                log.Controller = "Account";
                log.Action = "Password Change";

                log.BeforeChange = "Old Password";
                
                /***** end Logging initial settings *****/
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasPassword)
            {

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
                    if (result.Succeeded)
                    {
                        log.AfterChange = "New Password";
                        context.SaveLog(log);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        log.AfterChange = "Log in error";
                        log.Error = result.ToString();
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
                    if (result.Succeeded)
                    {
                        context.SaveLog(log);
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            context.SaveLog(log);
            // If we got this far, something failed, redisplay form
            return View(model);
        }
 public void SaveLog(DbChangeLog log)
 {
     context.DbChangeLogs.Add(log);
     context.SaveChanges();
 }