/// <inheritdoc /> /// <exception cref="InvalidNameException">Throws if name is invalid.</exception> /// <exception cref="EntryAlreadyExistsException">Throws if file already exists.</exception> public void CreateDirectory(string name) { _transactionWrapper.BeginTransaction(); _storageOperationLocker.LockEntry(name, () => { if (!NameValid(name)) { throw new InvalidNameException(name); } if (ExistsInternal(name)) { throw new EntryAlreadyExistsException(name); } var parentDirectoryName = name.GetParentFullName(); if (!DirectoryExistsInternal(parentDirectoryName)) { throw new DirectoryNotFoundException(name); } var directoryEntry = CreateDirectoryEntry(name); _directoryRepository.Create(directoryEntry); }); _transactionWrapper.EndTransaction(); }
public async Task Create(params DirectoryModel[] directories) { foreach (var directory in directories) { if (directory.ParentId == null) { throw new InvalidDataException("Parent directory not specified."); } var parentDirectory = await _directoryRepository.GetById(directory.ParentId.Value); if (parentDirectory == null) { throw new NotFoundException("Parent directory not found."); } var dirToAdd = new Directory { ParentId = directory.ParentId, Name = directory.Name, Private = parentDirectory.Private || directory.Private, StaffOnly = directory.StaffOnly }; _directoryRepository.Create(dirToAdd); } await _directoryRepository.SaveChanges(); }
public IActionResult CreateDirectory([FromBody] DirectoryDto directoryDto) { var lang = Request.Headers["language"].ToString(); var errorMessages = new List <string>(); try { var webRootPath = _webHostEnvironment.WebRootPath; var dirPath = $"{webRootPath}\\appData\\{directoryDto.Path}\\{directoryDto.Name.Replace(" ", "_").Replace(".", "_").Replace("+","_")}"; if (!Directory.Exists(dirPath)) { DirectoryInfo createDir = Directory.CreateDirectory(dirPath); if (createDir != null) { var dir = new E_Learning.Models.Directory() { Name = directoryDto.Name.Replace(" ", "_"), Path = dirPath.Replace($"{webRootPath}\\appData\\", "").Replace(" ", "_"), CreatedAt = DateTime.Now }; var createdDir = _directoryRepository.Create(dir); return(Ok(new { directory = createdDir })); } else { errorMessages.Add(_translator.GetTranslation("ERROR", lang)); return(BadRequest(new { errors = errorMessages })); } } else { errorMessages.Add(_translator.GetTranslation("DIRECTORIES.DIRECTORY_EXISTS", lang)); return(BadRequest(new { errors = errorMessages })); } } catch { errorMessages.Add(_translator.GetTranslation("ERROR", lang)); return(BadRequest(new { errors = errorMessages })); } }