Example #1
0
        //Create 2 examples foreach CodeLanguage from Database
        public static void CodeSnippedExampleForeachCodeLanguageInDatabase()
        {
            int ExamplePerLanguage = 2;

            for (int l = 0; l < DbCodeLanguage.GetallLanguages().Count; l++)
            {
                for (int examples = 0; examples < ExamplePerLanguage; examples++)
                {
                    SnippetInfo snippetInfo = new SnippetInfo(
                        9999,
                        UserInfo.Userinformation.ID,
                        9999,
                        "Name" + examples,
                        "Code" + examples,
                        DateTime.Now,
                        "Usage" + examples,
                        DateTime.Now,
                        "Description" + examples,
                        DateTime.Now,
                        DbCodeLanguage.GetallLanguages()[l].ID,//<-------------
                        DateTime.Now
                        );
                    System.Threading.Thread.Sleep(1000);

                    DbSnippets.AddNewSnippet(snippetInfo);
                }
            }
        }
Example #2
0
        //-------------------------Filter------------------------------
        //Filter out all codesnippets
        public static List <SnippetInfo> GetFilteredSnippeds(string name, string CodeLanguage, DateFilter dateFilter, TypeFilter typeFilter)
        {
            //Create Temp List
            List <SnippetInfo> Temp = new List <SnippetInfo>();

            //create connection and open it
            MySqlConnection connection = DbInfo.Connection();

            //Build Mysql command
            MySqlCommand cmd = connection.CreateCommand();

            //The Default cmd String
            string cmdString = "SELECT `ID`, `UserID`, `TagCollectionID`, `Name`, `Code`, `CodeEditDate`, `UsageExample`, `UsageEditDate`, `Description`, `DescriptionEditDate`, `LanguageID`, `CreateDate` FROM `codesnippets` ";

            //If filter not is all add where string
            if (CodeLanguage != "All")
            {
                cmdString += "WHERE `LanguageID` = @LanguageID";
            }

            //Add the CommandText
            cmd.CommandText = cmdString;

            //Add Parameter
            cmd.Parameters.AddWithValue("@LanguageID", DbCodeLanguage.ToID(CodeLanguage));

            //Create reader
            MySqlDataReader reader = cmd.ExecuteReader();

            //While reading
            while (reader.Read())
            {
                //Save a new Temporary Snippet
                SnippetInfo TempSnippet = (new SnippetInfo(
                                               int.Parse(reader["ID"].ToString()),
                                               int.Parse(reader["UserID"].ToString()),
                                               int.Parse(reader["TagCollectionID"].ToString()),
                                               reader["Name"].ToString(),
                                               reader["Code"].ToString(),
                                               DateTime.Parse(reader["CodeEditDate"].ToString()),
                                               reader["UsageExample"].ToString(),
                                               DateTime.Parse(reader["UsageEditDate"].ToString()),
                                               reader["Description"].ToString(),
                                               DateTime.Parse(reader["DescriptionEditDate"].ToString()),
                                               int.Parse(reader["LanguageID"].ToString()),
                                               DateTime.Parse(reader["CreateDate"].ToString())
                                               ));

                //If User is owner of this CodeSnippet
                if (int.Parse(reader["UserID"].ToString()) == UserInfo.Userinformation.ID)
                {
                    //Add snipped Based on TypeFilter
                    switch (typeFilter)
                    {
                    case TypeFilter.SnippedName:
                        if (TempSnippet._Name.ToLower().Contains(name.ToLower()))
                        {
                            Temp.Add(TempSnippet);
                        }
                        break;

                    case TypeFilter.Code:
                        if (TempSnippet._Code.ToLower().Contains(name.ToLower()))
                        {
                            Temp.Add(TempSnippet);
                        }
                        break;

                    case TypeFilter.Description:
                        if (TempSnippet._Description.ToLower().Contains(name.ToLower()))
                        {
                            Temp.Add(TempSnippet);
                        }
                        break;

                    case TypeFilter.Usage:
                        if (TempSnippet._UsageExample.ToLower().Contains(name.ToLower()))
                        {
                            Temp.Add(TempSnippet);
                        }
                        break;
                    }
                    //Filter List Based on DateFilter
                    switch (dateFilter)
                    {
                    case DateFilter.Newest:
                        SortAscending(Temp);
                        break;

                    case DateFilter.Oldest:
                        SortDescending(Temp);
                        break;
                    }
                }
            }

            //Close Connection
            connection.Close();

            //Return Temp List
            return(Temp);
        }