ANALYSE-8/services/search.py
2022-10-07 20:10:08 +02:00

84 lines
2.6 KiB
Python

from models.database import Database
from models.user import User
from models.city import City
class Search:
@staticmethod
def search_members(query):
cur = Database.connection.cursor()
cur.execute("""
SELECT * FROM users WHERE
id LIKE ? OR
username LIKE ? OR
firstname LIKE ? OR
lastname LIKE ? OR
email LIKE ? OR
phone LIKE ?
""", (f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%"))
rows = cur.fetchall()
payload = []
for row in rows:
user = User(Database.connection)._set_row_values(row)
if user.role == "MEMBER":
payload.append(user)
return payload
@staticmethod
def search_advisors(query):
cur = Database.connection.cursor()
cur.execute("""
SELECT * FROM users WHERE
id LIKE ? OR
username LIKE ? OR
firstname LIKE ? OR
lastname LIKE ? OR
email LIKE ? OR
phone LIKE ?
""", (f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%"))
rows = cur.fetchall()
payload = []
for row in rows:
user = User(Database.connection)._set_row_values(row)
if user.role == "ADVISOR":
payload.append(user)
return payload
@staticmethod
def search_admins(query):
cur = Database.connection.cursor()
cur.execute("""
SELECT * FROM users WHERE
id LIKE ? OR
username LIKE ? OR
firstname LIKE ? OR
lastname LIKE ? OR
email LIKE ? OR
phone LIKE ?
""", (f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%", f"%{query}%"))
rows = cur.fetchall()
payload = []
for row in rows:
user = User(Database.connection)._set_row_values(row)
if user.role == "SYSTEM_ADMIN":
payload.append(user)
return payload
@staticmethod
def get_all_cites():
cur = Database.connection.cursor()
cur.execute("SELECT * FROM cities")
rows = cur.fetchall()
payload = []
for row in rows:
print(row)
city = City(Database.connection)._set_row_values(row)
payload.append(city)
return payload