public async Task <ActionResult> SalvaDados() { String status = "1"; try { FileDelete fileDelete = new FileDelete(); String name = Request.Form["fileDeleteName"]; String applicationId = Request.Form["fileDeleteApplicationId"]; String filesDirectory = Request.Form["filesDeleteDirectory"]; String folders = Request.Form["fileDeleteFolders"]; fileDelete.Name = name; fileDelete.ApplicationId = Convert.ToInt32(applicationId); fileDelete.Date = DateTime.Now; fileDelete.User = User.Identity.Name; fileDelete.Enable = true; fileDelete.FilesDirectory = Regex.Replace(filesDirectory, "(?<!\r)\n", "\r\n"); if (ModelState.IsValid) { _context.Add(fileDelete); await _context.SaveChangesAsync(); _context.Entry(fileDelete).GetDatabaseValues(); List <FileDeleteFolder> fileDeleteFolderList = new List <FileDeleteFolder>(); foreach (String folder in folders.Trim().Split(" ")) { FileDeleteFolder fileDeleteFolder = new FileDeleteFolder(); fileDeleteFolder.ApplicationId = fileDelete.ApplicationId; fileDeleteFolder.FolderId = Convert.ToInt32(folder); fileDeleteFolder.FileDeleteId = fileDelete.Id; fileDeleteFolderList.Add(fileDeleteFolder); } _context.FileDeleteFolder.AddRange(fileDeleteFolderList); await _context.SaveChangesAsync(); GALibrary.GALogs.SaveLog("FileDelete", "Fim do cadastro de arquivos para remover" + fileDelete.Name + " pelo usuario " + User.Identity.Name, 2, _context.Parameter.FirstOrDefault()); } } catch (Exception erro) { GALibrary.GALogs.SaveLog("FileDelete", "Erro ao cadastrar arquivo para remover pelo usuario " + User.Identity.Name + ": " + erro.ToString(), 1, _context.Parameter.FirstOrDefault()); status = erro.ToString(); } return(Json(new { status = status })); }
public async Task <IActionResult> Edit(int id, [Bind("Id,Name,ApplicationId,FilesDirectory,User,Date,Enable")] FileDelete fileDelete, int[] FileDeleteFolderRight) { Boolean valida = true; if (id != fileDelete.Id) { return(NotFound()); } try { fileDelete.Date = DateTime.Now; fileDelete.User = User.Identity.Name; if (FileDeleteFolderRight.Length == 0) { valida = false; ViewBag.ErroPastas = "É necessário selecionar ao menos uma pasta, o status original foi restaurado"; } if (ModelState.IsValid && valida) { fileDelete.FilesDirectory = Regex.Replace(fileDelete.FilesDirectory, "(?<!\r)\n", "\r\n"); _context.Update(fileDelete); await _context.SaveChangesAsync(); List <FileDeleteFolder> folderSavedList = _context.FileDeleteFolder.Where(x => x.FileDeleteId == fileDelete.Id).ToList(); if (FileDeleteFolderRight != null) { List <FileDeleteFolder> lista = new List <FileDeleteFolder>(); _context.FileDelete.Attach(fileDelete); _context.Entry(fileDelete).Collection(x => x.FileDeleteFolder).Load(); foreach (int idFolder in FileDeleteFolderRight) { FileDeleteFolder fileDeletefolders = folderSavedList.Where(x => x.FolderId == idFolder).FirstOrDefault(); if (fileDeletefolders == null) { fileDeletefolders = new FileDeleteFolder(); fileDeletefolders.ApplicationId = fileDelete.ApplicationId; fileDeletefolders.FileDeleteId = fileDelete.Id; fileDeletefolders.FolderId = idFolder; } lista.Add(fileDeletefolders); } var apagar = fileDelete.FileDeleteFolder.Except(lista).ToList(); var novos = lista.Except(fileDelete.FileDeleteFolder).ToList(); if (novos.Count != 0) { foreach (FileDeleteFolder fileDeleteFolders in novos) { _context.FileDeleteFolder.Add(fileDeleteFolders); } } if (apagar.Count != 0) { foreach (FileDeleteFolder fileDeleteFolders in apagar) { _context.FileDeleteFolder.Remove(fileDeleteFolders); } } _context.SaveChanges(); } GALibrary.GALogs.SaveLog("FileDelete", "Fim da edicao do arquivo para apagar " + fileDelete.Name + " realizada pelo usuario " + User.Identity.Name, 2, _context.Parameter.FirstOrDefault()); return(RedirectToAction(nameof(Index))); } var folderIDs = _context.FileDeleteFolder.Where(x => x.FileDeleteId == id).Select(x => x.FolderId); ViewBag.FileDeleteFolderLeft = new MultiSelectList(_context.Folder.Where(x => x.ApplicationId == fileDelete.ApplicationId && !folderIDs.Contains(x.Id)), "Id", "Path"); ViewBag.FileDeleteFolderRight = new MultiSelectList(_context.Folder.Where(x => folderIDs.Contains(x.Id)), "Id", "Path"); ViewBag.ApplicationIdFileDelete = new SelectList(_context.Application.Where(x => x.Enable).OrderBy(x => x.Name), "Id", "Name", fileDelete.ApplicationId); ViewBag.EnabledFileDelete = new SelectList(new[] { new { ID = true, Name = "Sim" }, new { ID = false, Name = "Não" }, }, "ID", "Name", fileDelete.Enable); return(View(fileDelete)); } catch (Exception erro) { GALibrary.GALogs.SaveLog("FileDelete", "Erro ao editar arquivo para apagar com id " + id + " pelo usuario " + User.Identity.Name + ": " + erro.ToString(), 1, _context.Parameter.FirstOrDefault()); return(View("~/Views/Shared/Error.cshtml")); } }