Initial commit
This commit is contained in:
commit
ff50ea6784
12 changed files with 509 additions and 0 deletions
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue