Exemplo n.º 1
0
        public IReadOnlyList <Bestelling> HaalOp()
        {
            SqlConnection connection = GetConnection();
            string        query      = "SELECT * FROM Bestelling";

            using (SqlCommand command = connection.CreateCommand())
            {
                command.CommandText = query;
                connection.Open();

                try
                {
                    SqlDataReader               reader         = command.ExecuteReader();
                    List <Bestelling>           bestellingen   = new List <Bestelling>();
                    DbKlantManager              klantManager   = new DbKlantManager(connectionString);
                    DbProduct_BestellingManager pBManager      = new DbProduct_BestellingManager(connectionString);
                    DbProductManager            productManager = new DbProductManager(connectionString);
                    while (reader.Read())
                    {
                        // Query Klant
                        long  klantId = (long)reader["KlantId"];
                        Klant k       = klantManager.HaalOp(klantId);
                        // Query Product_Bestellingen for this BestellingId
                        IReadOnlyList <Product_Bestelling> product_Bestellingen = pBManager.HaalOp(x => x.BestellingId == (long)reader["Id"]);
                        // Query all products related to the BestellingId
                        Dictionary <Product, int> producten = new Dictionary <Product, int>();
                        // Add all products
                        foreach (Product_Bestelling pb in product_Bestellingen)
                        {
                            producten.Add(productManager.HaalOp(pb.ProductId), pb.Aantal);
                        }
                        Bestelling b = BestellingFactory.MaakNieuweBestelling((DateTime)reader["Datum"], k, producten, (long)reader["Id"]);
                        b.PrijsBetaald = (decimal)reader["Prijs"];
                        b.Betaald      = (bool)reader["Betaald"];
                        bestellingen.Add(b);
                    }
                    reader.Close();
                    return(bestellingen);
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: ${e.Message}");
                    throw new DbBestellingManagerException("DbBestellingManager: Fout bij ophalen van bestellingen uit database");
                }
                finally
                {
                    connection.Close();
                }
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            IDFactory idF = new IDFactory(0, 100, 5000); //klant,bestelling,product
            var       kM  = new /*Db*/ KlantManager();
            var       pM  = new ProductManager();
            var       bM  = new BestellingManager();

            pM.VoegProductToe(ProductFactory.MaakProduct("product 1", 10.0, idF));
            pM.VoegProductToe(ProductFactory.MaakProduct("product 2", 12.0, idF));
            pM.VoegProductToe(ProductFactory.MaakProduct("product 3", 13.0, idF));
            foreach (var x in pM.GeefProducten())
            {
                Console.WriteLine(x);
            }

            kM.VoegKlantToe(KlantFactory.MaakKlant("klant 1", "adres 1", idF));
            kM.VoegKlantToe(KlantFactory.MaakKlant("klant 2", "adres 2", idF));
            foreach (var x in kM.GeefKlanten()) //Console.WriteLine(x);
            {
                x.Show();
            }

            bM.VoegBestellingToe(BestellingFactory.MaakBestelling(idF));
            bM.VoegBestellingToe(BestellingFactory.MaakBestelling(kM.GeefKlant(1), idF));

            Bestelling b = bM.GeefBestelling(101);

            b.VoegProductToe(pM.GeefProduct("product 2"), 8);
            b.VoegProductToe(pM.GeefProduct("product 1"), 7);
            Console.WriteLine($"Prijs:{b.Kostprijs()}, {b.PrijsBetaald}");
            b.ZetBetaald();
            Console.WriteLine($"Prijs:{b.Kostprijs()}, {b.PrijsBetaald}");

            foreach (var x in bM.GeefBestellingen()) //Console.WriteLine(x);
            {
                x.Show();
            }
            foreach (var x in kM.GeefKlanten()) //Console.WriteLine(x);
            {
                x.Show();
            }
            Console.WriteLine("-----------------");
            Klant k1 = kM.GeefKlant(1);

            k1.Show();
            k1.VoegToeBestelling(b);
            k1.Show();
        }