Skip to content

Commit 7b855ed

Browse files
committed
fix: getRecent response time
1 parent 0389bc4 commit 7b855ed

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

backend/app/models/history.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,16 @@ def find_all(cls) -> list[Self]:
8383
def get_recent(cls, shoppinglist_id: int, limit: int = 9) -> list[Self]:
8484
sq = db.session.query(ShoppinglistItems.item_id).subquery().select()
8585
sq2 = (
86-
db.session.query(func.max(cls.id))
87-
.filter(cls.status == Status.DROPPED)
88-
.filter(cls.item_id.notin_(sq))
89-
.group_by(cls.item_id)
90-
.join(cls.item)
91-
.subquery()
92-
.select()
93-
)
94-
return (
95-
cls.query.filter(cls.shoppinglist_id == shoppinglist_id)
96-
.filter(cls.id.in_(sq2))
97-
.order_by(cls.created_at.desc(), cls.item_id)
86+
cls.query.filter(
87+
cls.shoppinglist_id == shoppinglist_id,
88+
cls.status == Status.DROPPED,
89+
cls.item_id.notin_(sq),
90+
)
91+
.distinct(cls.item_id)
92+
.order_by(cls.item_id, cls.created_at.desc())
9893
.limit(limit)
94+
.subquery()
9995
)
96+
alias = db.aliased(cls, sq2)
97+
q = db.session.query(alias).order_by(alias.created_at.desc())
98+
return q.all()

0 commit comments

Comments
 (0)