public ActionResult Edit(int id, ViewModels.Forms.FormViewModel viewModel) { // TODO : Cleanup - should probably wrap this in a transaction with rollback on // error from the SaveAs method of FileUpload Common.Models.Account.Users currentUser; Common.Models.Forms.Form model, currentModel; currentUser = Data.Account.Users.Get(User.Identity.Name); currentModel = Data.Forms.Form.Get(viewModel.Id.Value); model = Mapper.Map <Common.Models.Forms.Form>(viewModel); // Path is based on base path + id, so, during an edit, it should NEVER change under // the current model - May change in the future if versioning or something similar // is supported model.Path = currentModel.Path; model = Data.Forms.Form.Edit(model, currentUser); // Only overwrite is the user gave us a file to use, otherwise, keep // the existing file - user just wants to update the matter type if (viewModel.FileUpload != null && viewModel.FileUpload.ContentLength > 0) { // Posted file - overwrites existing viewModel.FileUpload.SaveAs(model.Path); } return(RedirectToAction("Index")); }
public ActionResult Create(ViewModels.Forms.FormViewModel viewModel) { Common.Models.Account.Users currentUser; Common.Models.Forms.Form model; currentUser = Data.Account.Users.Get(User.Identity.Name); model = Mapper.Map <Common.Models.Forms.Form>(viewModel); // File to upload is mandatory in creation, no file -> Fail if (viewModel.FileUpload == null || viewModel.FileUpload.ContentLength <= 0) { // NO FILE - FAIL ModelState.AddModelError("FileUpload", "File to upload is required"); } if (ModelState.IsValid) { // Determine path // Create path if it does not exist - IIS may not like this if it does not have // the appropriate permissions // TODO : Error handling if (!Directory.Exists(Common.Settings.Manager.Instance.FileStorage.FormsPath)) { Directory.CreateDirectory(Common.Settings.Manager.Instance.FileStorage.FormsPath); } // Make sure the path ends with a directory separator character model.Path = Common.Settings.Manager.Instance.FileStorage.FormsPath; if (!model.Path.EndsWith(Path.DirectorySeparatorChar.ToString()) && !model.Path.EndsWith(Path.AltDirectorySeparatorChar.ToString())) { model.Path += Path.DirectorySeparatorChar; } // Must create in DB so that ID is known. // TODO : error checking, transaction rollback, etc. model = Data.Forms.Form.Create(model, currentUser); // Append ID then the extension (if present) model.Path += model.Id.Value.ToString(); if (Path.HasExtension(viewModel.FileUpload.FileName)) { model.Path += Path.GetExtension(viewModel.FileUpload.FileName); } // Must update to update the path to pickup the file name model = Data.Forms.Form.Edit(model, currentUser); viewModel.FileUpload.SaveAs(model.Path); return(RedirectToAction("Index")); } return(View(viewModel)); }
public ActionResult Delete(int id, ViewModels.Forms.FormViewModel viewModel) { Common.Models.Account.Users currentUser; Common.Models.Forms.Form model; currentUser = Data.Account.Users.Get(User.Identity.Name); model = Mapper.Map <Common.Models.Forms.Form>(viewModel); model = Data.Forms.Form.Disable(model, currentUser); // Don't delete the file - in theory a system admin could enable this again // deleting file would leave a form in the DB without a corresponding file return(RedirectToAction("Index")); }