public List <IProduct> GetAllProducts(ICustomer customer) { var produtcts = new List <IProduct>(); if (customer?.CustomerId != null) { var sql = $"SELECT [Product].[Id], [Product].[Name] " + $"FROM CustomerLikesProduct " + $"LEFT JOIN Product ON CustomerLikesProduct.ProductId = Product.Id " + $"WHERE CustomerLikesProduct.CustomerId = @CustomerId"; Query(sql, (command) => { command.Parameters.Add(new SqlParameter("CustomerId", customer.CustomerId)); var reader = command.ExecuteReader(); while (reader.Read()) { produtcts.Add(ProductDbManager.CreateProductFromSqlReader(reader)); } }); } return(produtcts); }
private void AddCustomerLikesProduct() { MainWindow.SystemMessage("Hämta en kund"); var customer = Find(); var productDb = new ProductDbManager(((DbManager)DbManager).ConnectionString); var products = productDb.Read(null); if (products != null) { foreach (var x in products) { MainWindow.Add(x.Print()); } var input = MainWindow.GetInputWithQuestion("Vilken produkt gillar kunden:"); var product = products.Find(prod => prod.Name == input); if (product != null) { string sql = "INSERT INTO CustomerLikesProduct (CustomerID, ProductID)" + "VALUES (@CustomerId, @ProductId) "; ((DbManager)DbManager).Query(sql, (command) => { command.Parameters.Add(new SqlParameter("CustomerId", customer.CustomerId)); command.Parameters.Add(new SqlParameter("ProductId", product.Id)); command.ExecuteNonQuery(); }); } } }