Initial commit
This commit is contained in:
commit
ff50ea6784
12 changed files with 509 additions and 0 deletions
0
models/__init__.py
Normal file
0
models/__init__.py
Normal file
36
models/database.py
Normal file
36
models/database.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from config import (
|
||||
DB_HOST,
|
||||
DB_PORT,
|
||||
DB_NAME,
|
||||
DB_USER,
|
||||
DB_PASSWORD,
|
||||
)
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.connection = psycopg2.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
dbname=DB_NAME,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
cursor_factory=RealDictCursor,
|
||||
)
|
||||
|
||||
def fetch_all(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchall()
|
||||
|
||||
def fetch_one(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchone()
|
||||
|
||||
def execute(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
self.connection.commit()
|
||||
87
models/toy_model.py
Normal file
87
models/toy_model.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
from models.database import Database
|
||||
|
||||
|
||||
class ToyModel:
|
||||
def __init__(self, database: Database):
|
||||
self.database = database
|
||||
|
||||
def get_all(self):
|
||||
query = """
|
||||
SELECT t.toy_id,
|
||||
t.toy_name,
|
||||
c.category_name,
|
||||
m.manufacturer_name,
|
||||
t.price,
|
||||
t.discount,
|
||||
COALESCE(s.quantity, 0) AS quantity,
|
||||
STRING_AGG(DISTINCT a.age_label, ', ') AS ages,
|
||||
STRING_AGG(DISTINCT sup.supplier_name, ', ') AS suppliers
|
||||
FROM toys t
|
||||
JOIN categories c ON t.category_id = c.category_id
|
||||
JOIN manufacturers m ON t.manufacturer_id = m.manufacturer_id
|
||||
LEFT JOIN stock s ON t.toy_id = s.toy_id
|
||||
LEFT JOIN toy_age_groups ta ON t.toy_id = ta.toy_id
|
||||
LEFT JOIN age_groups a ON ta.age_group_id = a.age_group_id
|
||||
LEFT JOIN toy_suppliers ts ON t.toy_id = ts.toy_id
|
||||
LEFT JOIN suppliers sup ON ts.supplier_id = sup.supplier_id
|
||||
GROUP BY t.toy_id, c.category_name,
|
||||
m.manufacturer_name, s.quantity
|
||||
ORDER BY t.toy_name
|
||||
"""
|
||||
return self.database.fetch_all(query)
|
||||
|
||||
def search_by_age(self, age: str):
|
||||
query = """
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT t.toy_id,
|
||||
t.toy_name,
|
||||
c.category_name,
|
||||
m.manufacturer_name,
|
||||
t.price,
|
||||
t.discount,
|
||||
COALESCE(s.quantity, 0) AS quantity,
|
||||
STRING_AGG(DISTINCT a.age_label, ', ') AS ages,
|
||||
STRING_AGG(DISTINCT sup.supplier_name, ', ') AS suppliers
|
||||
FROM toys t
|
||||
JOIN categories c ON t.category_id = c.category_id
|
||||
JOIN manufacturers m ON t.manufacturer_id = m.manufacturer_id
|
||||
LEFT JOIN stock s ON t.toy_id = s.toy_id
|
||||
LEFT JOIN toy_age_groups ta ON t.toy_id = ta.toy_id
|
||||
LEFT JOIN age_groups a ON ta.age_group_id = a.age_group_id
|
||||
LEFT JOIN toy_suppliers ts ON t.toy_id = ts.toy_id
|
||||
LEFT JOIN suppliers sup ON ts.supplier_id = sup.supplier_id
|
||||
GROUP BY t.toy_id, c.category_name,
|
||||
m.manufacturer_name, s.quantity
|
||||
) sub
|
||||
WHERE ages ILIKE %s
|
||||
"""
|
||||
return self.database.fetch_all(query, (f"%{age}%",))
|
||||
|
||||
def sort_by_price(self):
|
||||
query = """
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT t.toy_id,
|
||||
t.toy_name,
|
||||
c.category_name,
|
||||
m.manufacturer_name,
|
||||
t.price,
|
||||
t.discount,
|
||||
COALESCE(s.quantity, 0) AS quantity,
|
||||
STRING_AGG(DISTINCT a.age_label, ', ') AS ages,
|
||||
STRING_AGG(DISTINCT sup.supplier_name, ', ') AS suppliers
|
||||
FROM toys t
|
||||
JOIN categories c ON t.category_id = c.category_id
|
||||
JOIN manufacturers m ON t.manufacturer_id = m.manufacturer_id
|
||||
LEFT JOIN stock s ON t.toy_id = s.toy_id
|
||||
LEFT JOIN toy_age_groups ta ON t.toy_id = ta.toy_id
|
||||
LEFT JOIN age_groups a ON ta.age_group_id = a.age_group_id
|
||||
LEFT JOIN toy_suppliers ts ON t.toy_id = ts.toy_id
|
||||
LEFT JOIN suppliers sup ON ts.supplier_id = sup.supplier_id
|
||||
GROUP BY t.toy_id, c.category_name,
|
||||
m.manufacturer_name, s.quantity
|
||||
) sub
|
||||
ORDER BY price
|
||||
"""
|
||||
return self.database.fetch_all(query)
|
||||
17
models/user_model.py
Normal file
17
models/user_model.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from models.database import Database
|
||||
|
||||
|
||||
class UserModel:
|
||||
def __init__(self, database: Database):
|
||||
self.database = database
|
||||
|
||||
def authenticate(self, login: str, password: str):
|
||||
query = """
|
||||
SELECT u.user_id,
|
||||
u.full_name,
|
||||
r.role_name
|
||||
FROM users u
|
||||
JOIN roles r ON u.role_id = r.role_id
|
||||
WHERE u.login = %s AND u.password = %s
|
||||
"""
|
||||
return self.database.fetch_one(query, (login, password))
|
||||
Loading…
Add table
Add a link
Reference in a new issue