/// <summary> /// Uprava parametru produktu /// Pozn.: metoda volana pri uprave produktu administratorem /// </summary> /// <param name="product">produkt k uprave</param> public static void UpdateProduct(Product product) { using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) { Cathegory cathegory = CachedCathegories.Find(c => c.Description == product.Cathegory); byte[] productPhoto = ImageToBLOB(product.Photo); connection.Open(); string commandText = $"UPDATE {Product.TableName} " + $"SET {Product.NameColumn} = @{Product.NameColumn}, " + $"{Product.CathegoryColumn} = @{Product.CathegoryColumn}, " + $"{Product.PriceColumn} = @{Product.PriceColumn}, " + $"{Product.PhotoColumn} = @{Product.PhotoColumn}, " + $"{Product.DescriptionColumn} = @{Product.DescriptionColumn} " + $"WHERE {Product.IDColumn} = @{Product.IDColumn}"; using (SQLiteCommand command = new SQLiteCommand(commandText, connection)) { command.Parameters.AddWithValue($"@{Product.NameColumn}", product.Name); command.Parameters.AddWithValue($"@{Product.CathegoryColumn}", cathegory.ID); command.Parameters.AddWithValue($"@{Product.PriceColumn}", product.Price); command.Parameters.AddWithValue($"@{Product.PhotoColumn}", productPhoto); command.Parameters.AddWithValue($"@{Product.DescriptionColumn}", product.Description); command.Parameters.AddWithValue($"@{Product.IDColumn}", product.ID); command.ExecuteNonQuery(); } connection.Close(); } }
/// <summary> /// Nacteni kategorii z databaze do pameti /// </summary> /// <param name="reader">precteny sqlite zaznamy k zpracovani</param> public static void LoadCathegories(SQLiteDataReader reader) { while (reader.Read()) { // nacteni kategorie do objektu Cathegory loadedCathegory = new Cathegory ( Convert.ToInt32(reader[Cathegory.IDColumn]), Convert.ToString(reader[Cathegory.DescriptionColumn]) ); // pridani objektu kategorie do cached kategorii CachedCathegories.Add(loadedCathegory); } }
/// <summary> /// Ulozeni noveho produktu do databaze /// </summary> /// <param name="product">produkt k ulozeni</param> public static void CreateProduct(Product product) { int lastInsertedID = -1; using (SQLiteConnection connection = new SQLiteConnection(ConnectionString)) { Cathegory cathegory = CachedCathegories.Find(c => c.Description == product.Cathegory); byte[] productPhoto = ImageToBLOB(product.Photo); connection.Open(); string commandText = $"INSERT INTO {Product.TableName} " + $"({Product.NameColumn}, {Product.CathegoryColumn}, " + $"{Product.PriceColumn}, {Product.PhotoColumn}, {Product.DescriptionColumn}) " + $"VALUES (@{Product.NameColumn}, " + $"@{Product.CathegoryColumn}, " + $"@{Product.PriceColumn}, " + $"@{Product.PhotoColumn}, " + $"@{Product.DescriptionColumn})"; using (SQLiteCommand command = new SQLiteCommand(commandText, connection)) { command.Parameters.AddWithValue($"@{Product.NameColumn}", product.Name); command.Parameters.AddWithValue($"@{Product.CathegoryColumn}", cathegory.ID); command.Parameters.AddWithValue($"@{Product.PriceColumn}", product.Price); command.Parameters.AddWithValue($"@{Product.PhotoColumn}", productPhoto); command.Parameters.AddWithValue($"@{Product.DescriptionColumn}", product.Description); command.Parameters.AddWithValue(Product.PhotoColumn, productPhoto); command.ExecuteNonQuery(); } lastInsertedID = Convert.ToInt32(connection.LastInsertRowId); connection.Close(); } // passed produktu priradi ID product.ChangeID(lastInsertedID); // vytvoreny produkt se priradi ku CachedProducts CachedProducts.Add(product); }