hisham-cmd commited on
Commit
0bf747b
·
verified ·
1 Parent(s): f5d1a9e

Update backend with high-quality catalog and fixes

Browse files
Files changed (2) hide show
  1. app/api/admin.py +6 -1
  2. app/api/products.py +16 -5
app/api/admin.py CHANGED
@@ -106,7 +106,12 @@ def get_dashboard_stats(
106
 
107
  low_stock_items = db.query(Product).filter(
108
  Product.stock <= 10,
109
- Product.is_active == True
 
 
 
 
 
110
  ).limit(5).all()
111
 
112
  low_stock_data = [{
 
106
 
107
  low_stock_items = db.query(Product).filter(
108
  Product.stock <= 10,
109
+ Product.is_active == True,
110
+ Product.deleted_at == None
111
+ ).order_by(
112
+ func.abs(Product.id > 3708).desc(), # 1 if > 3708, 0 otherwise (Sort 1s first)
113
+ case((Product.id > 3708, Product.id), else_=None).desc(),
114
+ Product.id.asc()
115
  ).limit(5).all()
116
 
117
  low_stock_data = [{
app/api/products.py CHANGED
@@ -116,11 +116,22 @@ async def list_products(
116
  total = query.count()
117
 
118
  # Sort
119
- sort_column = getattr(Product, sort_by, Product.created_at)
120
- if sort_order == "desc":
121
- query = query.order_by(desc(sort_column))
122
- else:
123
- query = query.order_by(asc(sort_column))
 
 
 
 
 
 
 
 
 
 
 
124
 
125
  # Pagination
126
  offset = (page - 1) * page_size
 
116
  total = query.count()
117
 
118
  # Sort
119
+ from sqlalchemy import case
120
+
121
+ # Custom Hybrid Sorting Logic:
122
+ # 1. New Products (ID > 3708) first, sorted by ID DESC
123
+ # 2. Original Catalog (ID <= 3708) next, sorted by ID ASC
124
+ query = query.order_by(
125
+ case(
126
+ (Product.id > 3708, 0),
127
+ else_=1
128
+ ),
129
+ case(
130
+ (Product.id > 3708, Product.id),
131
+ else_=None
132
+ ).desc(),
133
+ Product.id.asc()
134
+ )
135
 
136
  # Pagination
137
  offset = (page - 1) * page_size