Пример #1
0
        public void Verwijder(Bestelling bestelling)
        {
            if (bestelling == null)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Bestelling mag niet null zijn");
            }
            if (bestelling.BestellingId <= 0)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Te verwijderen bestelling heeft een invalide Id");
            }
            if (bestelling.GeefProducten().Count == 0)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Geen producten aanwezig in de bestelling");
            }

            SqlConnection connection = GetConnection();
            string        query      = "DELETE FROM Bestelling WHERE Id=@id";

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

                try
                {
                    DbProduct_BestellingManager pbManager = new DbProduct_BestellingManager(connectionString);
                    foreach (KeyValuePair <Product, int> kvp in bestelling.GeefProducten())
                    {
                        Product_Bestelling pb = new Product_Bestelling(kvp.Key.ProductId, bestelling.BestellingId, kvp.Value);
                        pbManager.Verwijder(pb);
                    }
                    command.Parameters.Add(new SqlParameter("@id", SqlDbType.BigInt));
                    command.Parameters["@id"].Value = bestelling.BestellingId;
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: ${e.Message}");
                    throw new DbKlantManagerException("DbBestellingManager: Fout bij verwijderen van bestelling uit database");
                }
                finally
                {
                    connection.Close();
                }
            }
        }
Пример #2
0
        public void UpdateBestelling(Bestelling bestelling)
        {
            if (bestelling == null)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Bestelling mag niet leeg zijn");
            }
            if (bestelling.Datum == null || bestelling.Klant == null)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Datum en Klant mogen niet leeg zijn");
            }
            if (bestelling.GeefProducten().Count == 0)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Geen producten aanwezig in de bestelling");
            }
            if (bestelling.BestellingId == 0)
            {
                throw new DbBestellingManagerException("DbBestellingManager: Up te daten bestelling moet een unieke ID hebben");
            }

            SqlConnection connection      = GetConnection();
            string        bestellingQuery = "UPDATE Bestelling SET KlantId=@klantId, Datum=@datum, Betaald=@betaald, Prijs=@prijs WHERE Id=@id";

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

                try
                {
                    // Remove currently existing Product_Bestelling lines
                    DbProduct_BestellingManager pbManager = new DbProduct_BestellingManager(connectionString);
                    pbManager.Verwijder(bestelling.BestellingId);
                    // Add new Bestelling to database
                    command.CommandText = bestellingQuery;
                    command.Parameters.Add(new SqlParameter("@Id", SqlDbType.BigInt));
                    command.Parameters.Add(new SqlParameter("@klantId", SqlDbType.BigInt));
                    command.Parameters.Add(new SqlParameter("@datum", SqlDbType.DateTime));
                    command.Parameters.Add(new SqlParameter("@betaald", SqlDbType.Bit));
                    command.Parameters.Add(new SqlParameter("@prijs", SqlDbType.Decimal));
                    command.Parameters["@Id"].Value      = bestelling.BestellingId;
                    command.Parameters["@klantId"].Value = bestelling.Klant.KlantId;
                    command.Parameters["@datum"].Value   = bestelling.Datum;
                    command.Parameters["@betaald"].Value = bestelling.Betaald;
                    command.Parameters["@prijs"].Value   = bestelling.PrijsBetaald;

                    command.ExecuteNonQuery();
                    // Add new Product_Bestelling lines to database
                    //DbProduct_BestellingManager pbManager = new DbProduct_BestellingManager(connectionString);
                    foreach (KeyValuePair <Product, int> kvp in bestelling.GeefProducten())
                    {
                        pbManager.VoegToe(new Product_Bestelling(kvp.Key.ProductId, bestelling.BestellingId, kvp.Value));
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"Error: ${e.Message}");
                    throw new DbBestellingManagerException("DbBestellingManager: Fout bij toevoegen van Bestelling aan database");
                }
                finally
                {
                    connection.Close();
                }
            }
        }