public ServiceResult DeleteProject(int projectId, string projectTitle) { ServiceResult sResult = new ServiceResult(); try { DashBoardModel dbModel = new DashBoardModel(); var user = HttpContext.Current.User; if (user.Identity.IsAuthenticated) { ApplicationDbContext db = new ApplicationDbContext(); List <Role> lstUserRoles = db.GetUserRoleId(SessionUser.GetUserId()); if (lstUserRoles.Any(r => r.Id == (int)UserRoles.Admin) || dbModel.CheckUserPermissionToDelete(projectId)) { if (dbModel.DeleteProject(projectId)) { //Delete all files of this project from server sResult.Value = true; var projectFolderPath = Utility.GetRootPath() + "Projects\\" + projectTitle; System.IO.DirectoryInfo di = new DirectoryInfo(projectFolderPath); try { foreach (FileInfo file in di.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in di.GetDirectories()) { dir.Delete(true); } Directory.Delete(projectFolderPath, true); } catch (Exception e) { Debug.WriteLine("DELETE FILES ERROR: " + e.ToString()); //sResult.IsSuccess = false; //sResult.Message = e.ToString(); } } } } } catch (Exception ex) { Debug.WriteLine("Error: " + ex.ToString()); sResult.IsSuccess = false; sResult.Message = ex.ToString(); } return(sResult); }
public ServiceResult DeleteFileProject(int projectId, string projectName, int fileId, string fileName) { ServiceResult sResult = new ServiceResult(); try { var user = HttpContext.Current.User; if (user.Identity.IsAuthenticated) { DashBoardModel dbModel = new DashBoardModel(); ApplicationDbContext db = new ApplicationDbContext(); List <Role> lstUserRoles = db.GetUserRoleId(SessionUser.GetUserId()); if (lstUserRoles.Any(r => r.Id == (int)UserRoles.Admin) || dbModel.CheckUserPermissionToDelete(projectId)) { sResult.IsSuccess = dbModel.DeleteFileProject(projectId, fileId); } else { sResult.IsSuccess = false; sResult.Message = "Just admin or leader have permisstion to delete file!"; } //delete File if (sResult.IsSuccess) { try { string filePath = Utility.GetRootPath() + "Projects\\" + projectName + "\\Imports\\" + fileName; if (File.Exists(filePath)) { File.Delete(filePath); } } catch (Exception ex) { sResult.IsSuccess = false; sResult.Message = "Can't delete physical file!\n" + ex.Message; } } } } catch (Exception ex) { sResult.IsSuccess = false; sResult.Message = ex.Message; } return(sResult); }