Exemple #1
0
        public void DodajPrzykladowoInwestycje()
        {
            using (var ctx = new GameDbContext())
            {
                // Tworzymy grupę w pamięci
                Grupa grupa = new Grupa()
                {
                    Name = "Sosny"
                };

                Inwestycja inwestycja;

                // Dodajemy studenta do grupy
                inwestycja = new Inwestycja()
                {
                    Nazwa = "Wysokie", Data = new DateTime(1990, 10, 10), Kurs = 5000, Przelicznik = 1
                };
                grupa.Inwestycje.Add(inwestycja);


                // Grupę dodajemy do kolekcji reprezentującej dane w bazie danych
                ctx.Grupa.Add(grupa);



                // Zapisujemy zmienne przechowywane w kontekście
                ctx.SaveChanges();
            }
        }
        /// <summary>
        /// Specjalna metoda, która jest wywoływana raz po przebudowaniu bazy danych.
        /// Założenie jest, że baza jest pusta, więc trzeba ją wypełnić początkowymi danymi.
        /// </summary>
        /// <param name="context"></param>
        protected override void Seed(GameDbContext context)
        {
            Grupa grupa;
            Firma firma;

            grupa = new Grupa()
            {
                Name = "Akcje"
            };
            //    Inwestycja inw = new Inwestycja() { Nazwa = "", Kurs = 12.43, Data = new DateTime(2015, 4, 26), Przelicznik = 1 };
            //     firma = new Firma() { Name = "Przykład", AktualnaInwestycja = inw };
            //     grupa.Inwestycje.Add(inw);



            firma = new Firma()
            {
                Name = "Przykład"
            };
            grupa.Inwestycje.Add(new Inwestycja()
            {
                Firma = firma, Nazwa = firma.Name, Kurs = 12.43, Data = new DateTime(2015, 4, 26), Przelicznik = 1
            });
            context.Grupa.Add(grupa);

            Inwestycja tmp1 = new Inwestycja()
            {
                Firma = firma, Nazwa = firma.Name, Kurs = 25.54, Data = new DateTime(2015, 4, 26), Przelicznik = 1, Grupa = grupa
            };
            Użytkownik user = new Użytkownik()
            {
                Id = 0, Nickname = "Michał", Login = "******", StanKonta = 1000000, Hasło = "123abc"
            };

            context.Użytkownik.Add(user);
            Operacja operacja = new Operacja()
            {
                Id = 0, Ilość = 5, Transakcja = transakcja.kupno, Inwestycja = tmp1, StempelCzasowy = new DateTime(2015, 4, 4), Użytkownik = user
            };

            context.Operacja.Add(operacja);
            // grupa = new Grupa() { Name = "Obligacje"};
            // context.Grupa.Add(grupa);
            try
            { context.SaveChanges(); }
            catch (Exception ex)
            {
                MessageBox.Show("Błąd inicjalizacji bazy " + ex);
            }
            base.Seed(context);
        }
Exemple #3
0
        //public List<Inwestycja> PobierzKursyWalut()
        public void PobierzKursyWalut()
        {
            MessageBox.Show("Pobieram waluty");
            // List<Inwestycja> waluty = new List<Inwestycja>();
            WebClient Client = new WebClient();

            using (var ctx = new GameDbContext())
            {
                // MessageBox.Show("Pobieramy dane z NBP!");
                try
                {
                    Client.DownloadFile("http://www.nbp.pl/kursy/xml/dir.txt", "kursy.txt");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Problem z pobieraniem katalogu z notowaniami walut " + ex);
                }
                string       path = "kursy.txt";
                StreamReader sr   = File.OpenText(path);
                string       line = "";
                char[]       buffor;
                string       nazwa   = "";
                bool         zakoncz = false;
                int          counter = 0;
                while (true)
                {
                    do  // check if first letter of line is a which means this is the right file to read from
                    {
                        if ((line = sr.ReadLine()) != null)
                        {
                            buffor = new char[line.Length];
                            StringReader stream = new StringReader(line);
                            stream.Read(buffor, 0, 11);
                            nazwa = new string(buffor);
                            //  MessageBox.Show("Nazwa: "+nazwa);
                        }
                        else
                        {
                            MessageBox.Show("Zakończono wczytywanie danych - waluty");
                            zakoncz = true;
                            break;
                        }
                    } while (!(buffor[0] == 'a' && ((buffor[5] == '1' && buffor[6] == '4') || (buffor[5] == 1 && buffor[6] == 5))));
                    // MessageBox.Show("before while: " + buffor[0]);


                    counter++;
                    if (counter > 30)
                    {
                        break;// -> warunek by zakończyć wczytywanie po 10 plikach
                    }
                    if (zakoncz)
                    {
                        break;
                    }

                    /*   if (nazwa == "a150z040803")
                     * {
                     *     MessageBox.Show("Koniec plików o poprawnej strukturze");
                     *     break;
                     * }*/

                    string url = "http://www.nbp.pl/kursy/xml/" + nazwa + ".xml";
                    //      MessageBox.Show(buffor.ToString());


                    nazwa = "dane" + nazwa + ".xml";
                    //    MessageBox.Show(nazwa);
                    try
                    {
                        Client.DownloadFile(url, nazwa);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Błąd pobierania pliku: " + nazwa + " " + ex);
                    }

                    try
                    {
                        // Create an instance of StreamReader to read from a file.
                        // The using statement also closes the StreamReader.
                        StreamReader str = new StreamReader(nazwa);
                        string       line1;
                        // Read and display lines from the file until the end of
                        // the file is reached.
                        while ((line1 = str.ReadLine()) != null)
                        {
                            //   Console.WriteLine(line1);
                        }
                    }
                    catch (Exception e)
                    {
                        // Let the user know what went wrong.
                        MessageBox.Show("The file could not be read:" + e.Message);
                    }

                    //Parsowanie XMLa:
                    XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object

                    xmlDoc.XmlResolver = null;              // ignoruj plik dtd
                    xmlDoc.Load(nazwa);                     // Load the XML document from the specified file

                    // Get elements
                    XmlNodeList data = xmlDoc.GetElementsByTagName("data_publikacji");

                    DateTime data_time = DateTime.Parse(data[0].InnerText);

                    //    MessageBox.Show(data_time.ToString());
                    Grupa grupa;
                    bool  jestGrupaWaluty = false;
                    foreach (var tmp in ctx.Grupa)
                    {
                        if (tmp.Name == "Waluty")
                        {
                            jestGrupaWaluty = true;
                        }
                    }
                    if (!jestGrupaWaluty)
                    {
                        grupa = new Grupa()
                        {
                            Name = "Waluty"
                        };
                        ctx.Grupa.Add(grupa);
                        ctx.SaveChanges();
                    }

                    try
                    {
                        XmlNodeList pozycja = xmlDoc.SelectNodes("/tabela_kursow/pozycja");
                        //  MessageBox.Show("pobieram dane o kursach "+ pozycja.Count);
                        if (pozycja.Count == 5)
                        {
                            grupa = (from gr in ctx.Grupa
                                     where gr.Name == "Waluty"
                                     select gr).First();


                            foreach (XmlNode xn in pozycja)
                            {
                                //  MessageBox.Show("Hello");
                                XmlNodeList lista_atrybutów = xn.ChildNodes;
                                //      MessageBox.Show("Ile atrybutow: " + lista_atrybutów.Count);
                                int przelicznik = Int32.Parse(lista_atrybutów[3].InnerText);
                                //      MessageBox.Show("Przelicznik: " + lista_atrybutów[3].InnerText);
                                string kurs1_txt = lista_atrybutów[5].InnerText;
                                double kurs      = Convert.ToDouble(kurs1_txt);
                                //     MessageBox.Show("Kurs " + kurs);

                                string nazwa_dodawanej_firmy = lista_atrybutów[4].InnerText;
                                var    fm = (from tmp in ctx.Firma
                                             where tmp.Name == nazwa_dodawanej_firmy
                                             select tmp).First();

                                if (!fm.Archiwum.Exists(item => item.Data == data_time))
                                {
                                    fm.Archiwum.Add(new Inwestycja {
                                        Nazwa = nazwa_dodawanej_firmy, Kurs = kurs, Przelicznik = przelicznik, Data = data_time, Grupa = grupa
                                    });
                                }



                                // waluty.Add(tmp);
                                // ctx.Firma.Add(fm);
                                // ctx.Inwestycja.Add(tmp);

                                //     MessageBox.Show(tmp.ToString());
                            }
                            ctx.SaveChanges();
                        }
                        else
                        {
                            //   MessageBox.Show("Dla 4 atrybutów");
                            grupa = (from gr in ctx.Grupa
                                     where gr.Name == "Waluty"
                                     select gr).First();


                            foreach (XmlNode xn in pozycja)
                            {
                                //  MessageBox.Show("Hello");
                                XmlNodeList lista_atrybutów = xn.ChildNodes;
                                //       MessageBox.Show("Ile atrybutow: " + lista_atrybutów.Count);
                                try
                                {
                                    int przelicznik = Int32.Parse(lista_atrybutów[1].InnerText);

                                    //   MessageBox.Show("Przelicznik: " + lista_atrybutów[1].InnerText);
                                    string kurs1_txt = lista_atrybutów[3].InnerText;
                                    double kurs      = Convert.ToDouble(kurs1_txt);
                                    //   MessageBox.Show("Kurs " + kurs);
                                    string nazwa_dodawanej_firmy = lista_atrybutów[2].InnerText;
                                    var    fm = (from tmp in ctx.Firma
                                                 where tmp.Name == nazwa_dodawanej_firmy
                                                 select tmp).First();



                                    if (!fm.Archiwum.Exists(item => item.Data == data_time))
                                    {
                                        fm.Archiwum.Add(new Inwestycja {
                                            Nazwa = nazwa_dodawanej_firmy, Kurs = kurs, Przelicznik = przelicznik, Data = data_time, Grupa = grupa
                                        });
                                    }


                                    //  Firma fm = new Firma() { Name = "zNeta" };
                                    // Inwestycja tmp = new Inwestycja { Firma = fm, Nazwa = lista_atrybutów[2].InnerText, Kurs = kurs, Przelicznik = przelicznik, Data = data_time, Grupa = grupa };
                                    // waluty.Add(tmp);
                                    //    ctx.Firma.Add(fm);
                                    //     ctx.Inwestycja.Add(tmp);

                                    //  MessageBox.Show(tmp.ToString());
                                }
                                catch (Exception ex) { MessageBox.Show("Parsowanie przelicznika: " + ex); }
                            }
                            ctx.SaveChanges();
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Błąd wyłuskiwania danych o walutach " + ex);
                    }
                }
                // return waluty;
            }
        }