public int SaveWithSql(entities.Product product) { entities.Product hasProduct = FindOne(product.GetId()); SQLiteCommand command; if (product.GetId() == 0 | hasProduct == null) { Add(product); command = new SQLiteCommand("INSERT INTO product(sku, name, price, category_id, image, amount) VALUES(@sku, @name, @price, @category_id, @image, @amount)", db.connection); } else { command = new SQLiteCommand("UPDATE product SET sku = @sku, name = @name, price = @price, category_id = @category_id, image = @image, amount = @amount WHERE id = @id ", db.connection); } command.Parameters.AddWithValue("@id", product.GetId()); command.Parameters.AddWithValue("@sku", product.GetSku()); command.Parameters.AddWithValue("@name", product.GetName()); command.Parameters.AddWithValue("@price", Convert.ToDecimal(product.GetPrice())); command.Parameters.AddWithValue("@category_id", product.GetCategoryId()); command.Parameters.AddWithValue("@image", product.GetImage()); command.Parameters.AddWithValue("@amount", product.GetAmount()); int num = command.ExecuteNonQuery(); return(num); }
public bool FillProducts(repositories.Product productRepository, repositories.Category categoryRepository) { SQLiteCommand command = new SQLiteCommand("SELECT * FROM product", db.connection); SQLiteDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { int id = reader.GetInt32(0); string sku = reader.GetString(1); string name = reader.GetString(2); double price = Convert.ToDouble(reader.GetDecimal(3)); dynamic category = null; string image; int amount; if (!reader.IsDBNull(5)) { image = reader.GetString(5); } else { image = null; } if (!reader.IsDBNull(6)) { amount = reader.GetInt32(6); } else { amount = 0; } if (!reader.IsDBNull(4)) { category = categoryRepository.FindOne(reader.GetInt32(4)); } else { category = null; } entities.Product product = new entities.Product(id, sku, name, price, category, image, amount); productRepository.Add(product); } } reader.Close(); return(true); }
public static List <Product> GenerateProductList() { List <Product> list = new List <Product>(); for (int i = 0; i < FakeBD.ProductNames.Count(); i++) { int price = Rand.Next(FakeBD.MAX_PRICE); Product p = new entities.Product(FakeBD.ProductNames[i], price); list.Add(p); } return(list); }
public int DeleteWithSql(entities.Product product) { entities.Product hasProduct = FindOne(product.GetId()); SQLiteCommand command; command = new SQLiteCommand("DELETE FROM product WHERE id = @id", db.connection); command.Parameters.AddWithValue("@id", product.GetId()); int num = command.ExecuteNonQuery(); return(num); }
public Tuple <int, int, int> LoadProducts(repositories.Product products, repositories.Category categories) { List <string[]> lines = ParseCsv(GetCsvString("products")); int added = 0, changeprice = 0, changeamount = 0; foreach (string[] line in lines) { int id = Convert.ToInt32(line[0]); string sku = line[1]; string name = line[2]; double price; if (line[3] == "") { price = 0; } else { price = Convert.ToDouble(line[3]); } int category_id; if (line[4] != "") { category_id = Convert.ToInt32(line[4]); } else { category_id = 0; } string image = line[5]; int amount; if (line[6] != "") { amount = Convert.ToInt32(line[6]); } else { amount = 0; } entities.Product product = products.FindOne(id); if (product == null) { dynamic category; if (category_id != 0) { category = categories.FindOne(category_id); } else { category = null; } entities.Product addProduct = new entities.Product(id, sku, name, price, category, image, amount); products.SaveWithSql(addProduct); added++; } else { if (product.GetPrice() != price) { changeprice++; } if (product.GetAmount() != amount) { changeamount++; } product.SetName(name); product.SetSku(sku); product.SetCategoryId(category_id); product.SetPrice(price); product.SetImage(image); product.SetAmount(amount); products.SaveWithSql(product); } } return(new Tuple <int, int, int>(added, changeprice, changeamount)); }
public void Delete(entities.Product product) { products.Remove(product); }
public void Add(entities.Product product) { products.Add(product); }