public IActionResult Create([FromBody] DirectoryCreate data)
 {
     try
     {
         var            userData = jwtService.ParseData(this.User);
         DirectoryIndex result   = dirService.Create(data, userData.UserId);
         return(Ok(result));
     }
     catch (ServiceException e)
     {
         return(BadRequest(e.Message));
     }
 }
Exemplo n.º 2
0
        public DirectoryIndex Create(DirectoryCreate create, int userId, bool isAdmin = false)
        {
            var user = this.context.Users.SingleOrDefault(x => x.Id == userId);

            if (user == null)
            {
                throw new ServiceException("User Not Found!");
            }

            var parentDir = context.Directories.SingleOrDefault(x => x.Id == create.ParentDirectoryId);

            if (parentDir == null)
            {
                throw new ServiceException("The parent directory of the directory you are trying to create does not exist!");
            }

            if (parentDir.UserId != user.Id && isAdmin == false)
            {
                throw new ServiceException("The parent directory does not belong to you!");
            }

            if (string.IsNullOrWhiteSpace(create.Name) || create.Name.Length == 0 || create.Name.ToLower() == "root")
            {
                throw new ServiceException("The directory name is not valid!");
            }

            var order = 0;

            if (parentDir.Subdirectories.Any())
            {
                order = parentDir.Subdirectories.Select(x => x.Order).Max() + 1;
            }

            var dir = new Directory
            {
                Name            = create.Name,
                UserId          = parentDir.UserId,
                ParentDirectory = parentDir,
                Order           = order,
            };

            context.Directories.Add(dir);
            context.SaveChanges();

            var index = Mapper.Instance.Map <DirectoryIndex>(dir);

            return(index);
        }
        public void DirectoryCreate_Exists_Succeeds()
        {
            var cwd         = Environment.CurrentDirectory;
            var tempDirName = Guid.NewGuid().ToString();
            var tempDirPath = Path.Combine(cwd, tempDirName);
            var mockReader  = Helper.GetMockReader(tempDirPath, Guid.NewGuid().ToString(), 1);
            var mkDir       = new DirectoryCreate();

            try
            {
                mkDir.ReadXml(mockReader.Object);
                Directory.CreateDirectory(tempDirPath);

                Assert.True(mkDir.Run());
            }
            finally
            {
                Directory.Delete(tempDirPath);
            }
        }