public void VoegToe(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"); } SqlConnection connection = GetConnection(); string bestellingQuery = "INSERT INTO Bestelling (KlantId, Datum, Betaald, Prijs) OUTPUT INSERTED.Id VALUES (@klantId, @datum, @betaald, @prijs)"; using (SqlCommand command = connection.CreateCommand()) { connection.Open(); try { // Add new Bestelling to database command.CommandText = bestellingQuery; 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["@klantId"].Value = bestelling.Klant.KlantId; command.Parameters["@datum"].Value = bestelling.Datum; command.Parameters["@betaald"].Value = bestelling.Betaald; command.Parameters["@prijs"].Value = bestelling.PrijsBetaald; // Execute query and retrieve new identity Id for new Bestelling Int64 newBestellingId = 0; var result = command.ExecuteScalar(); if (result != null) { newBestellingId = (Int64)result; } // Add new Product_Bestelling lines to database (possible with DbProduct_BestellingManager?) DbProduct_BestellingManager pbManager = new DbProduct_BestellingManager(connectionString); foreach (KeyValuePair <Product, int> kvp in bestelling.GeefProducten()) { pbManager.VoegToe(new Product_Bestelling(kvp.Key.ProductId, newBestellingId, kvp.Value)); } } catch (Exception e) { Console.WriteLine($"Error: ${e.Message}"); throw new DbBestellingManagerException("DbBestellingManager: Fout bij toevoegen van Bestelling aan database"); } finally { connection.Close(); } } }
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(); } } }