private async Task <bool> _deleteDirectory(Account ownerAccount, string directoryName, string pathToDirectory, string directoryPath) { DSDirectory dsDirectory = await dbContext.DSDirectories .Where(directory => directory.Node.OwnerId == ownerAccount.Id && directory.Node.Name == directoryName && directory.Node.Path == pathToDirectory) .SingleOrDefaultAsync(); if (dsDirectory == null) { return(false); } // Delete all subdirectories and files that this directory contains var childFiles = dbContext.DSFiles .Where(f => f.Node.Path.Contains(directoryPath)) .ToList(); var childDirectories = dbContext.DSDirectories .Where(d => d.Node.Path.Contains(directoryPath)) .ToList(); dbContext.DSNodes.RemoveRange(childFiles.Select(f => f.Node).ToList()); dbContext.DSFiles.RemoveRange(childFiles); dbContext.DSNodes.RemoveRange(childDirectories.Select(f => f.Node).ToList()); dbContext.DSDirectories.RemoveRange(childDirectories); dbContext.DSNodes.Remove(dsDirectory.Node); dbContext.DSDirectories.Remove(dsDirectory); await dbContext.SaveChangesAsync(); return(true); }
// Map multiple lists (files and dirs) into one single node list public static ICollection <DSFile> AllNodes(this DSDirectory source) { return(source.Files); }
public NodeDto FileUpload(string ownerPhoneNumber, FileDto fileDto) { var ownerAccount = dbContext.Accounts .Where(a => a.PhoneNumber == ownerPhoneNumber) .SingleOrDefault(); if (ownerAccount == null) { return(null); } DSFile dsFile = dbContext.DSFiles .Where(f => f.Node.Name == fileDto.Name && f.Node.Path == fileDto.Path && f.Node.OwnerId == ownerAccount.Id) .SingleOrDefault(); if (dsFile == null) { DSNode dsNode = new DSNode(); dsFile = new DSFile(); dbContext.DSNodes.Add(dsNode); dsFile.Node = dsNode; dbContext.DSFiles.Add(dsFile); } // Fetch parent directory by both path and name int lastSegmentPos = fileDto.Path.LastIndexOf('/'); // Get the positon of the last slash string parentDirectoryName = fileDto.Path; string pathToParentDirectory = ""; if (lastSegmentPos > -1) { parentDirectoryName = fileDto.Path?.Substring(lastSegmentPos + 1); // Extract last segment (dir name) pathToParentDirectory = fileDto.Path?.Remove(lastSegmentPos); // Extract the path up to the last segment } if (!String.IsNullOrEmpty(parentDirectoryName)) { DSDirectory parentDir = dbContext.DSDirectories .Where(d => d.Node.Name == parentDirectoryName && d.Node.Path == pathToParentDirectory) .SingleOrDefault(); dsFile.ParentDirectoryId = parentDir.Id; } //TODO Add this to automapper aswell dsFile.Node.Name = fileDto.Name; dsFile.Node.Path = fileDto.Path; dsFile.Node.Url = fileDto.Url; dsFile.Node.Private = fileDto.Private; //introduce private prop on Dto dsFile.Node.NodeType = NodeType.File; dsFile.Node.Owner = ownerAccount; dsFile.MimeType = fileDto.MimeType; dsFile.FileSizeInKB = fileDto.FileSizeInKB; dbContext.SaveChanges(); return(autoMapper.Map <NodeDto>(dsFile)); }
// Check how we'll create new dirs based on path not on parentDirName public async Task <NodeDto> NewDirectory(string ownerPhoneNumber, DirectoryDto directoryDto) { var ownerAccount = await dbContext.Accounts .Where(a => a.PhoneNumber == ownerPhoneNumber) .SingleOrDefaultAsync(); if (ownerAccount == null) { return(null); } DSDirectory dsDirectory = await dbContext.DSDirectories .Where(d => d.Node.Name == directoryDto.Name && d.Node.Path == directoryDto.Path && d.Node.OwnerId == ownerAccount.Id) .SingleOrDefaultAsync(); if (dsDirectory == null) { DSNode dsNode = new DSNode(); dsDirectory = new DSDirectory(); await dbContext.DSNodes.AddAsync(dsNode); dsDirectory.Node = dsNode; await dbContext.DSDirectories.AddAsync(dsDirectory); } // Fetch parent directory by both path and name int lastSegmentPos = directoryDto.Path.LastIndexOf('/'); // Get the positon of the last slash string parentDirectoryName = directoryDto.Path; string pathToParentDirectory = ""; if (lastSegmentPos > -1) { parentDirectoryName = directoryDto.Path.Substring(lastSegmentPos + 1); // Extract last segment (dir name) pathToParentDirectory = directoryDto.Path.Remove(lastSegmentPos); // Extract the path up to the last segment } if (!String.IsNullOrEmpty(parentDirectoryName)) { DSDirectory parentDir = await dbContext.DSDirectories .Where(d => d.Node.Name == parentDirectoryName && d.Node.Path == pathToParentDirectory) .SingleOrDefaultAsync(); dsDirectory.ParentDirectoryId = parentDir.Id; } dsDirectory.Node.Name = directoryDto.Name; dsDirectory.Node.Path = directoryDto.Path; dsDirectory.Node.Url = directoryDto.Url; dsDirectory.Node.Private = directoryDto.Private; dsDirectory.Node.NodeType = NodeType.Directory; dsDirectory.Node.Owner = ownerAccount; await dbContext.SaveChangesAsync(); return(autoMapper.Map <NodeDto>(dsDirectory)); }