Skip to content

Commit

Permalink
fix: refactor where
Browse files Browse the repository at this point in the history
  • Loading branch information
Valimp committed May 15, 2024
1 parent 1d1233a commit 67d5697
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ def create_ticket(ticket: TicketCreate) -> Ticket:
def get_tickets(
status: TicketStatus | None = None,
type_: IssueType | None = None,
reason_: ReasonType | None = "inappropriate",
reason: ReasonType | None = "inappropriate",
page: int = 1,
page_size: int = 10,
):
Expand All @@ -384,22 +384,22 @@ def get_tickets(
with db:
offset = (page - 1) * page_size
# Get IDs of flags with the specified filters
flag_ids = (
FlagModel.select(FlagModel.ticket_id)
.where(
(FlagModel.type == type_ if type_ else True)
& (FlagModel.reason == reason_ if reason_ else True)
where_clause = []
if status:
where_clause.append(TicketModel.status == status)
if type_:
where_clause.append(TicketModel.type == type_)
if reason:
subquery = (
FlagModel.select(FlagModel.ticket_id)
.where(FlagModel.reason == reason)
)
.distinct()
)
where_clause.append(TicketModel.id.in_(subquery))

# Get the total number of tickets with the specified filters
count = (
TicketModel.select()
.where(
(TicketModel.status == status if status else True)
& (TicketModel.type == type_ if type_ else True)
)
.where(*where_clause)
.count()
)
max_page = count // page_size + int(count % page_size != 0)
Expand All @@ -408,11 +408,7 @@ def get_tickets(
return {
"tickets": list(
TicketModel.select()
.where(
(TicketModel.status == status if status else True)
& (TicketModel.type == type_ if type_ else True)
& (TicketModel.id.in_(flag_ids) if flag_ids else True)
)
.where(*where_clause)
.order_by(TicketModel.created_at.desc())
.offset(offset)
.limit(page_size)
Expand Down

0 comments on commit 67d5697

Please sign in to comment.