updatepush
This commit is contained in:
@@ -11,6 +11,7 @@ def database():
|
||||
)
|
||||
return db_connection
|
||||
|
||||
|
||||
def get_all_products():
|
||||
|
||||
db_con = database()
|
||||
@@ -37,32 +38,58 @@ def get_all_products():
|
||||
results = cursor.fetchall()
|
||||
|
||||
print(results[1])
|
||||
final = []
|
||||
for row in results:
|
||||
final.append(row)
|
||||
print(row)
|
||||
|
||||
cursor.close()
|
||||
db_con.close()
|
||||
return final
|
||||
|
||||
def get_user_history(user_id):
|
||||
db_con = database()
|
||||
cursor = db_con.cursor()
|
||||
|
||||
cursor.execute("SELECT CategoryID FROM Category")
|
||||
categories = cursor.fetchall()
|
||||
|
||||
select_clause = "SELECT p.ProductID"
|
||||
for category in categories:
|
||||
category_id = category[0] # get the uid of the catefory and then append that to the new column
|
||||
select_clause += f", MAX(CASE WHEN pc.CategoryID = {category_id} THEN 1 ELSE 0 END) AS `Cat_{category_id}`"
|
||||
|
||||
final_query = f"""
|
||||
{select_clause}
|
||||
FROM Product p
|
||||
LEFT JOIN Product_Category pc ON p.ProductID = pc.ProductID
|
||||
LEFT JOIN Category c ON pc.CategoryID = c.CategoryID
|
||||
where p.ProductID in (select ProductID from History where UserID = {user_id})
|
||||
GROUP BY p.ProductID;
|
||||
"""
|
||||
|
||||
cursor.execute(final_query)
|
||||
results = cursor.fetchall()
|
||||
|
||||
print(results[1])
|
||||
final = []
|
||||
for row in results:
|
||||
final.append(row)
|
||||
print(row)
|
||||
|
||||
cursor.close()
|
||||
db_con.close()
|
||||
return final
|
||||
|
||||
def get_user_history():
|
||||
db_con = database()
|
||||
cursor = db_con.cursor()
|
||||
user_id = 1
|
||||
query= f"""select ProductID from History where UserID={user_id}"""
|
||||
cursor.execute(query)
|
||||
data = cursor.fetchall()
|
||||
product_arr = []
|
||||
for item in data:
|
||||
product_arr.append(item[0])
|
||||
|
||||
querydata= tuple(product_arr)
|
||||
prod_query = f"""select * from Product where ProductID IN {querydata} """
|
||||
cursor.execute(prod_query)
|
||||
res = cursor.fetchall()
|
||||
for i in res:
|
||||
print(i,"\n")
|
||||
print("all products:")
|
||||
get_all_products()
|
||||
|
||||
print("User History products:")
|
||||
get_user_history(1)
|
||||
|
||||
|
||||
def Calculate_cosin_similarity():
|
||||
pass
|
||||
|
||||
get_user_history()
|
||||
|
||||
|
||||
#get_all_products()
|
||||
def Main:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user