How to create a logic that when I logged in as a seller, each of them have different set of products.
NOTE: I temporarily hardcoded "2" for the parameters to add seller id in the Database.GetProductDetails() and Database.AddProduct().
**FROM THE DATABASE.CS**
//Initializing for Seller Accounts Database
public async static void InitializeDB_SELLERACCOUNTS()
{
await ApplicationData.Current.LocalFolder.CreateFileAsync("MyDatabase.db", CreationCollisionOption.OpenIfExists);
string pathtoDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyDatabase.db");
using (SqliteConnection con = new SqliteConnection($"Filename={pathtoDB}"))
{
con.Open();
string initCMD = @"CREATE TABLE IF NOT EXISTS Sellers (
SELLER_ID INTEGER PRIMARY KEY AUTOINCREMENT,
BusinessName TEXT NOT NULL,
Email TEXT NOT NULL,
Username TEXT NOT NULL,
LastName TEXT NOT NULL,
FirstName TEXT NOT NULL,
MiddleName TEXT NOT NULL,
Password TEXT NOT NULL,
PhoneNumber TEXT NOT NULL,
AddressLine1 TEXT NOT NULL,
AddressLine2 TEXT NOT NULL
)";
SqliteCommand CMDcreateTable = new SqliteCommand(initCMD, con);
CMDcreateTable.ExecuteReader();
con.Close();
}
}
//Initializing for Product Details Database
public async static void InitializeDB_PRODUCTDETAILS()
{
await ApplicationData.Current.LocalFolder.CreateFileAsync("MyDatabase.db", CreationCollisionOption.OpenIfExists);
string pathtoDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyDatabase.db");
using (SqliteConnection con = new SqliteConnection($"Filename={pathtoDB}"))
{
con.Open();
string initCMD = @"CREATE TABLE IF NOT EXISTS ProductDetails (
PRODUCTDETAILS_ID INTEGER PRIMARY KEY AUTOINCREMENT,
Seller_ID INTEGER NOT NULL,
ProductName TEXT NOT NULL,
ProductCategory TEXT,
ProductPrice REAL,
ProductDescription TEXT,
ProductQuantity INTEGER,
ProductPicture BLOB,
FOREIGN KEY(Seller_ID) REFERENCES Sellers(SELLER_ID)
)";
SqliteCommand CMDcreateTable = new SqliteCommand(initCMD, con);
CMDcreateTable.ExecuteReader();
con.Close();
}
}
// Query to Retreive Seller's specific product details
public static List<ProductDetails> GetProductDetails(int seller_id)
{
List<ProductDetails> productList = new List<ProductDetails>();
string pathtoDB = Path.Combine(ApplicationData.Current.LocalFolder.Path, "MyDatabase.db");
using (SqliteConnection con = new SqliteConnection($"Filename={pathtoDB}"))
{
con.Open();
string selectCMD = "SELECT * FROM ProductDetails WHERE Seller_ID = @Seller_ID";
SqliteCommand cmdSelectRecords = new SqliteCommand(selectCMD, con);
cmdSelectRecords.Parameters.AddWithValue("@Seller_ID", seller_id);
SqliteDataReader reader = cmdSelectRecords.ExecuteReader();
while (reader.Read())
{
ProductDetails product = new ProductDetails();
product.PRODUCTDETAILS_ID = reader.GetInt32(0);
product.Seller_ID = reader.GetInt32(1);
product.ProductName = reader.GetString(2);
product.ProductCategory = reader.IsDBNull(3) ? null : reader.GetString(3);
product.ProductPrice = reader.GetDouble(4);
product.ProductDescription = reader.GetString(5);
product.ProductQuantity = reader.GetInt32(6);
product.ProductPicture = reader.IsDBNull(7) ? null : GetByteArrayFromBlob(reader, 7); // Retrieve image data as byte array
productList.Add(product);
}
reader.Close();
con.Close();
}
return productList;
}
**FROM THE YOURWATERPRODUCTS.CS**
public sealed partial class YourWaterProducts : Page
{
private StorageFile selectedFile;
public YourWaterProducts()
{
this.InitializeComponent();
PopulateProductList();
}
private void PopulateProductList()
{
List<ProductDetails> productDetailsList = Database.GetProductDetails(2);
// Bind the list of product details to the ListView
ListProducts.ItemsSource = productDetailsList;
}
**FROM THE ADDPRODUCTS.CS**
if (result == ContentDialogResult.Primary)
{
// If the user confirms, proceed to add the product
string productName = tbxProductName.Text;
string productCategory = (cbxProductCategory.SelectedItem as ComboBoxItem)?.Content.ToString();
string productDescription = tbxProductDescription.Text;
byte[] productPicture = await ConvertImageToByteArray(selectedFile);
// Call AddProduct with SellerId
Database.AddProduct(2, productName, productCategory, productPrice, productDescription, productQuantity, productPicture);
I really hope that when I try to logged in as a seller, each of them will show up different sets of products based on their seller's id.