private async void BtnImportFromDocLibTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
        {
            CloseAppBar();

            var fromFolder = await FileSystemUtils.PromptForFolder("Select folder to import from");

            if (fromFolder == null)
            {
                return;
            }

            var repository = new SnippetRepository();

            repository.LengthyOpStarting += (x, args) => { ProgressRingSnippets.IsActive = true; };
            repository.LengthyOpHasEnded += (x, args) => { ProgressRingSnippets.IsActive = false; };

            var nFilesImported = await repository.ImportAllSnippetsFromDocumentsLibrary(fromFolder);

            var msg = nFilesImported == -1 ?
                      "An error occurred during snippet import. The process did not complete successfully" :
                      nFilesImported.ToString() + " snippets imported into the local snippet data store";
            var dlg = new MessageDialog(msg);
            await dlg.ShowAsync();

            // Goto the root of the library and refresh
            FileSystemUserControl.GotoRootFolder();
        }
예제 #2
0
        public void CRUD_One_Snippet_And_Fetch_Runs_Successfully()
        {
            var connectionManager = new ConnectionManager(DbFile.GetConnectionString("testdb.db"));
            var repository        = new SnippetRepository(connectionManager);
            var entry             = new Snippet
            {
                Title             = "New Hyperlink",
                Content           = "Here is some content for you.",
                SyntaxHighlightId = 1
            };

            repository.InsertOne(entry);
            var insertedEntity = repository.GetOne(1);

            Assert.IsNotNull(entry);

            insertedEntity.Title = "Modified Snippet";
            repository.UpdateOne(insertedEntity);

            var updatedEntity = repository.GetOne(1);

            Assert.That(updatedEntity.Title, Is.EqualTo("Modified Snippet"));

            repository.DeleteOne(updatedEntity);

            var deletedEntity = repository.GetOne(insertedEntity.Id);

            Assert.IsNull(deletedEntity);
        }
예제 #3
0
        public void InsertSnippet_WhenSnippetToInsert_ShouldInsertOnce()
        {
            IDataGateway gateway    = Substitute.For <IDataGateway>();
            var          repository = new SnippetRepository(gateway);

            repository.InsertSnippet(new Snippet());
            gateway.Received(1).InsertSnippet(Arg.Any <Snippet>());
        }
예제 #4
0
        public void DeleteSnippet_WhenSnippetExists_ShouldDeleteSnippet()
        {
            IDataGateway gateway = Substitute.For <IDataGateway>();

            var repository = new SnippetRepository(gateway);

            repository.DeleteSnippet(2);
            gateway.Received().RemoveSnippet(Arg.Any <int>());
        }
예제 #5
0
        public void UpdateSnippet_WhenSnippetsAreNotAvailable_ReturnsFalse()
        {
            IDataGateway gateway = Substitute.For <IDataGateway>();

            gateway.UpdateSnippet(Arg.Any <Snippet>()).Returns(false);
            var repository = new SnippetRepository(gateway);

            Assert.IsFalse(repository.UpdateSnippet(new Snippet()));
        }
예제 #6
0
        public void GetSnippets_WhenNoSnippets_ReturnsEmptyCollection()
        {
            IDataGateway gateway = Substitute.For <IDataGateway>();

            var repository = new SnippetRepository(gateway);
            var snippets   = repository.GetSnippets();

            Assert.AreEqual(new List <Snippet>(), snippets);
        }
예제 #7
0
        public void GetSnippets_WhenSnippetsAreAvailable_ReturnsSnippets()
        {
            IDataGateway gateway = Substitute.For <IDataGateway>();

            gateway.GetAllSnippets().Returns(new List <Snippet>
            {
                new Snippet()
            });

            var repository = new SnippetRepository(gateway);
            var snippets   = repository.GetSnippets();

            Assert.AreEqual(1, snippets.Count());
        }
        private async Task <bool> InstallSampleSnippets()
        {
            // Install some sample snippets
            MessageDialog dlg;

            try
            {
                if (await FileSystemUtils.FolderExistsAsync(RootSnippetPath + "\\Samples"))
                {
                    return(true);  // Samples have been previously installed
                }
                var folder = await FileSystemUtils.CreateFolderAsync(RootSnippetPath, "Samples");

                if (folder == null)
                {
                    throw new Exception();
                }

                var repository = new SnippetRepository();

                // /////////////
                // Snippet #1...
                var sampleSnippet = await CodeSnippet.CreateNew();

                sampleSnippet.Filename        = "ReadTextFile.snippet";
                sampleSnippet.Title           = "Read a text file";
                sampleSnippet.Description     = "Read a text file from the Documents library";
                sampleSnippet.Author          = "Russell Archer";
                sampleSnippet.Code            = "// Get a file in the Documents library by specifying a relative path\r\nvar file = await KnownFolders.DocumentsLibrary.GetFileAsync($relativePath$\\$filename$);\r\n\r\n// Read the contents of the as one long string\r\nvar txt = await FileIO.ReadTextAsync(file);\r\n\r\n// Display the text\r\ntbTextBlock.Text = txt;\r\n";
                sampleSnippet.DeclarationList = new ObservableCollection <Declaration>();
                sampleSnippet.DeclarationList.Add(new Declaration
                {
                    DeclarationType = DeclarationTypes.Literal,
                    Editable        = true,
                    ID      = "relativePath",
                    ToolTip = "Relative path",
                    Default = "relativePath"
                });

                sampleSnippet.DeclarationList.Add(new Declaration
                {
                    DeclarationType = DeclarationTypes.Literal,
                    Editable        = true,
                    ID      = "filename",
                    ToolTip = "Filename",
                    Default = "filename"
                });

                if (!await repository.WriteSnippetAsync(folder, sampleSnippet))
                {
                    throw new Exception();
                }

                // /////////////
                // Snippet #2...
                sampleSnippet = await CodeSnippet.CreateNew();

                sampleSnippet.Filename        = "ReadJsonFile.snippet";
                sampleSnippet.Title           = "Read a JSON file";
                sampleSnippet.Description     = "Read a static text file containing JSON from a subdirectory in the app's location";
                sampleSnippet.Author          = "Russell Archer";
                sampleSnippet.Code            = "var file = await Package.Current.InstalledLocation.GetFileAsync($appFolder$\\$filename$);\r\nvar txt = await FileIO.ReadTextAsync(file);\r\n\r\n// Here we use Newtonsoft Json.net to deserialize the json\r\nvar results = await JsonConvert.DeserializeObjectAsync<ObservableCollection<t>>(txt);";
                sampleSnippet.DeclarationList = new ObservableCollection <Declaration>();
                sampleSnippet.DeclarationList.Add(new Declaration
                {
                    DeclarationType = DeclarationTypes.Literal,
                    Editable        = true,
                    ID      = "appFolder",
                    ToolTip = "App Folder",
                    Default = "appFolder"
                });

                sampleSnippet.DeclarationList.Add(new Declaration
                {
                    DeclarationType = DeclarationTypes.Literal,
                    Editable        = true,
                    ID      = "filename",
                    ToolTip = "Filename",
                    Default = "filename"
                });

                if (!await repository.WriteSnippetAsync(folder, sampleSnippet))
                {
                    throw new Exception();
                }

                // /////////////
                // Snippet #3...
                sampleSnippet = await CodeSnippet.CreateNew();

                sampleSnippet.Filename    = "ReadTextFileOpenPicker.snippet";
                sampleSnippet.Title       = "Read text file selected by user";
                sampleSnippet.Description = "Read a text file selected by the user via the FileOpenPicker";
                sampleSnippet.Author      = "Russell Archer";
                sampleSnippet.Code        = "// Create the file open picker\r\nvar picker = new FileOpenPicker();\r\n\r\n// Set the type of file to pick\r\npicker.FileTypeFilter.Add(\".txt\");\r\n\r\n// Single-file selection\r\nvar file = await picker.PickSingleFileAsync();\r\n\r\n// If the user chose something, read it into a string var\r\nif (file != null)\r\n    var myText = await FileIO.ReadTextAsync(file);\r\n";

                if (!await repository.WriteSnippetAsync(folder, sampleSnippet))
                {
                    throw new Exception();
                }

                // End of sample snippet creation

                dlg = new MessageDialog("A small selection of sample code snippets has been created in the \"Samples\" folder of your snippet library. \n\nYou may also wish to use the \"Import\" command to import existing Visual Studio code snippets from your Documents library. If you modify snippets you can use the \"Export\" command. \n\nAlternatively, add your snippet library path to Visual Studio using Tools > Code Snippets Manager. This will give Visual Studio direct access to your snippet library. The path to the snippet library may be copied from this app's Preferences page.");
                await dlg.ShowAsync();

                CurrentSnippetPath = folder.Path;  // Jump to the samples folder
                FileSystemUserControl.CurrentSnippetFolder = CurrentSnippetPath;

                return(true);
            }
            catch
            {
            }

            dlg = new MessageDialog("Unable to create sample snippets");
            await dlg.ShowAsync();

            return(false);
        }
예제 #9
0
 public SnippetController(ApplicationDbContext context, IConfiguration configuration)
 {
     _snippetRepository     = new SnippetRepository(context, configuration);
     _userProfileRepository = new UserProfileRepository(context);
     _categoryRepository    = new CategoryRepository(context);
 }