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(); } } }
public void VoegToe(Product_Bestelling item) { if (item == null) { throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Product_Bestelling mag niet null zijn"); } if (item.ProductId <= 0 || item.BestellingId <= 0) { throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Product_Bestelling heeft een invalide Id"); } if (item.Aantal <= 0) { throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Aantal moet groter dan 0 zijn"); } SqlConnection connection = GetConnection(); string query = "INSERT INTO Product_Bestelling (productId, bestellingId, aantal) VALUES (@productId, @bestellingId, @aantal)"; using (SqlCommand command = connection.CreateCommand()) { command.CommandText = query; connection.Open(); try { command.Parameters.Add(new SqlParameter("@productId", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("@bestellingId", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("@aantal", SqlDbType.Int)); command.Parameters["@productId"].Value = item.ProductId; command.Parameters["@bestellingId"].Value = item.BestellingId; command.Parameters["@aantal"].Value = item.Aantal; command.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine($"Error: ${e.Message}"); throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Fout bij toevoegen van Product_Bestelling aan database"); } finally { connection.Close(); } } }
public void Verwijder(Product_Bestelling item) { if (item == null) { throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Product_Bestelling mag niet null zijn"); } if (item.ProductId <= 0 || item.BestellingId <= 0) { throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Te verwijderen Product_Bestelling heeft een invalide Id"); } SqlConnection connection = GetConnection(); string query = "DELETE FROM Product_Bestelling WHERE productId=@productId AND bestellingId=@bestellingId"; using (SqlCommand command = connection.CreateCommand()) { command.CommandText = query; connection.Open(); try { command.Parameters.Add(new SqlParameter("@productId", SqlDbType.BigInt)); command.Parameters.Add(new SqlParameter("@bestellingId", SqlDbType.BigInt)); command.Parameters["@productId"].Value = item.ProductId; command.Parameters["@bestellingId"].Value = item.BestellingId; command.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine($"Error: ${e.Message}"); throw new DbProduct_BestellingManagerException("DbProduct_BestellingManager: Fout bij verwijderen van Product_Bestelling uit database"); } finally { connection.Close(); } } }