Example #1
0
        public IHttpActionResult CreateFolder(string folderName, string subfolderName = null)
        {
            var folderFromDB = _context.Folders.SingleOrDefault(x => x.Name == folderName);

            Folder subfolderFromDB = null;

            if (subfolderName != null)
            {
                subfolderFromDB = _context.Folders.SingleOrDefault(x => x.Name == subfolderName);
            }

            // subfolder doesn't exists, create one
            if (folderFromDB != null && subfolderFromDB == null && subfolderName != null)
            {
                var newSubFolder = FolderService.CreateSubFolder(_context, subfolderName, folderFromDB);
                return(Created("", newSubFolder));
            }

            // folder doesn't exist
            if (folderFromDB == null && subfolderFromDB != null)
            {
                return(BadRequest("Parent folder doesn't exist."));
            }

            // folder and subfolder already exist
            if (folderFromDB != null && subfolderFromDB != null)
            {
                return(BadRequest("Parent folder and subfolder already exists."));
            }

            // folder and subfolder doesn't exist
            else
            {
                var newFolder    = FolderService.CreateNewFolder(_context, folderName);
                var newSubFolder = FolderService.CreateSubFolder(_context, subfolderName, newFolder);

                var listOfFolders = new List <Folder>();
                listOfFolders.Add(newFolder);
                listOfFolders.Add(newSubFolder);

                return(Created("", listOfFolders));
            }
        }