Exemplo n.º 1
0
        public void GitHubDataContextTest_Owners()
        {
            //  Arrange
            Owner             owner     = null;
            int               count     = 0;
            GitHubDataContext dbContext = new GitHubDataContext();

            //Act.
            owner = new Owner();
            count = dbContext.Owners.Count();
            dbContext.Owners.Add(owner);
            dbContext.Owners.Remove(owner);

            //Assert
            Assert.IsTrue(count == dbContext.Owners.Count());
        }
Exemplo n.º 2
0
        public void GitHubDataContextTest_Licenses()
        {
            //  Arrange
            License           license   = null;
            int               count     = 0;
            GitHubDataContext dbContext = new GitHubDataContext();

            //Act.
            license = new License();
            count   = dbContext.Licenses.Count();
            dbContext.Licenses.Add(license);
            dbContext.Licenses.Remove(license);

            //Assert
            Assert.IsTrue(count == dbContext.Licenses.Count());
        }
Exemplo n.º 3
0
        /**********************************************************************************************//**
        * \fn  public bool SetSqlServerItems(List<Item> items = null)
        *
        * \brief   Sets SQL server items
        *
        * \author  Delmiro Paes
        *
        * \param   items   (Optional) The items.
        *
        * \returns True if it succeeds, false if it fails.
        **************************************************************************************************/
        public bool SetSqlServerItems(List <Item> items = null)
        {
            if (items == null || items.Count == 0)
            {
                return(false);
            }
            else
            {
                //TODO: SQL Error, what to do?
                if (CheckSqlServerAccess() == false)
                {
                    return(false);
                }
            }

            int workItems = 0;

            using (GitHubDataContext dbContext = new GitHubDataContext())
            {
                try
                {
                    dbContext.Configuration.AutoDetectChangesEnabled = false;

                    dbContext.ClearDatabase();

                    workItems = dbContext.SaveChanges();

                    foreach (Item item in items)
                    {
                        dbContext.Items.Add(item);
                    }

                    workItems = dbContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    return(false);
                }
                finally
                {
                    dbContext.Configuration.AutoDetectChangesEnabled = true;
                }
            }

            return(true);
        }
Exemplo n.º 4
0
        /**********************************************************************************************//**
        * \fn  public static bool CheckSqlServerAccess()
        *
        * \brief   Determines if we can check SQL server access
        *
        * \author  Delmiro Paes
        *
        * \returns True if it succeeds, false if it fails.
        **************************************************************************************************/
        public static bool CheckSqlServerAccess()
        {
            using (GitHubDataContext dbContext = new GitHubDataContext())
            {
                try
                {
                    dbContext.Database.Connection.Open();
                    dbContext.Database.Connection.Close();
                }
                catch (SqlException ex)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 5
0
        /**********************************************************************************************//**
        * \fn  public string GetJsonFromSql()
        *
        * \brief   Gets JSON from SQL
        *
        * \author  Delmiro Paes
        *
        * \returns The JSON from SQL.
        **************************************************************************************************/
        public string GetJsonFromSql()
        {
            string      jsonResult = string.Empty;
            List <Item> items      = new List <Item>();

            if (CheckSqlServerAccess() == false)
            {
                //TODO: SQL Error, what to do?
                return("");
            }

            using (GitHubDataContext dbContext = new GitHubDataContext())
            {
                try
                {
                    //TODO: Just speed this up with Sql store procedure.
                    items = dbContext.Items
                            .Join
                            (
                        dbContext.Items.GroupBy(g => new { g.Language })
                        .Select(c => new { Language = c.Key.Language, SUM = c.Sum(s => s.Stargazers_count) })
                        //TODO: Remove this, not necessary.
                        .OrderByDescending(obd => obd.SUM),
                        itemIn => itemIn.Language,
                        itemOuter => itemOuter.Language,
                        (itemOuter, itemIn) => new { itemOuter, itemIn.SUM }
                            )
                            .OrderByDescending(obd => obd.SUM)
                            .ThenByDescending(obd => obd.itemOuter.Stargazers_count)
                            .Select(s => s.itemOuter)
                            .Include("Owner")
                            .Include("License")
                            .ToList();

                    //TODO: Check items count = 0.
                    //  All local.
                    ADHCServiceJsonResult adhcServiceJsonResult = new ADHCServiceJsonResult();
                    int totalCount = 0;
                    adhcServiceJsonResult.total_count        = 0;
                    adhcServiceJsonResult.incomplete_results = false;
                    adhcServiceJsonResult.LanguageItems      = new List <LanguageItem>();
                    List <LanguageItem> languageItems = new List <LanguageItem>();
                    LanguageItem        languageItem  = new LanguageItem();

                    for (int iCount = 0; iCount < items.Count; ++iCount)
                    {
                        languageItem.Name        = items[iCount].Language;
                        languageItem.TotalStars += items[iCount].Stargazers_count;
                        languageItem.TotalProjects++;
                        totalCount++;

                        if (iCount + 1 == items.Count)
                        {
                            languageItem.Items.Add(items[iCount]);
                            adhcServiceJsonResult.LanguageItems.Add(languageItem);
                            break;
                        }
                        else if (languageItem.Name != items[iCount + 1].Language)
                        {
                            languageItem.Items.Add(items[iCount]);
                            adhcServiceJsonResult.LanguageItems.Add(languageItem);
                            languageItem = new LanguageItem();
                        }
                        else
                        {
                            languageItem.Items.Add(items[iCount]);
                        }
                    }

                    adhcServiceJsonResult.incomplete_results = false;
                    adhcServiceJsonResult.total_count        = totalCount;

                    jsonResult = JsonConvert.SerializeObject(adhcServiceJsonResult);
                }
                catch (Exception)
                {
                    return(string.Empty);
                }
            }

            return(jsonResult);
        }