예제 #1
0
        public List<Show> Load()
        {
            bool stop;
            Dictionary<string, Show> showDictionary = new Dictionary<string, Show>();

            int pageNumber = 0;
            do
            {
                Dictionary<string, Show> shows;
                stop = LoadPage(GetPageUrlByNumber(pageNumber), out shows);

                foreach (var show in shows)
                {
                    if (showDictionary.ContainsKey(show.Key))
                    {
                        //Remove duplicates from series list
                        var seriesList = show.Value.Episodes.Except(showDictionary[show.Key].Episodes);
                        showDictionary[show.Key].Episodes.AddRange(seriesList);
                    }
                    else
                    {
                        showDictionary.Add(show.Key, show.Value);
                    }
                }
                pageNumber++;
            } while (!stop);

            List<Show> result = showDictionary.Select(s => s.Value).ToList();
            if (result.Count != 0)
            {
                LastId = result.Aggregate(
                    new List<Episode>(),
                    (list, show) =>
                    {
                        list.AddRange(show.Episodes);
                        return list;
                    }
                    ).OrderByDescending(s => s.SiteId).First().SiteId;
            }
            return result;
        }
예제 #2
0
 public Test GetAllTestById(int testId)
 {
     using (SqlConnection connection = new SqlConnection(_connectionString))
     {
         var command = connection.CreateCommand();
         command.CommandType = CommandType.StoredProcedure;
         command.CommandText = "dbo.GetAllTestById";
         command.Parameters.AddWithValue("@id", testId);
         connection.Open();
         try
         {
             var  reader = command.ExecuteReader();
             Test test   = null;
             Dictionary <int, Question> questions = new Dictionary <int, Question>();
             while (reader.Read())
             {
                 if (test == null)
                 {
                     test = new Test()
                     {
                         ID          = (int)reader["testId"],
                         Description = reader["description"] as string,
                         Time        = (int)reader["time"],
                         Category    = new Category()
                         {
                             ID = (int)reader["id_Category"]
                         },
                         Subject = new Subject()
                         {
                             ID = (int)reader["id_Subject"]
                         }
                     };
                 }
                 Question questionFromRequest = new Question()
                 {
                     ID    = (int)reader["idQuestion"],
                     Text  = reader["textQuestion"] as string,
                     Image = reader["imageQuestion"] == DBNull.Value ? null : Convert.FromBase64String((string)reader["imageQuestion"])
                 };
                 Answer answer = new Answer()
                 {
                     ID         = (int)reader["idAnswer"],
                     AnswerText = reader["textAnswer"] as string,
                     IsCorrect  = (bool)reader["IsCorrectAnswer"]
                 };
                 if (questions.TryGetValue((int)reader["idQuestion"], out Question question))
                 {
                     question.Answers.Add(answer);
                 }
                 else
                 {
                     questions.Add((int)reader["idQuestion"], questionFromRequest);
                     questionFromRequest.Answers.Add(answer);
                 }
             }
             test.Questions = questions.Select(kvp => kvp.Value).ToList();;
             return(test);
         }
         catch (SqlException e)
         {
             _log.Error(e.Message);
             throw e;
         }
     }
 }