diff --git a/app/api.py b/app/api.py index 88d4a51..02421d7 100644 --- a/app/api.py +++ b/app/api.py @@ -359,28 +359,48 @@ def create_ticket(ticket: TicketCreate) -> Ticket: return _create_ticket(ticket) -def _get_ticket(status: TicketStatus | None, type_: IssueType | None): - """Get tickets with optional filters.""" - query = TicketModel.select() - - if status is not None: - query = query.where(TicketModel.status == status) - - if type_ is not None: - query = query.where(TicketModel.type == type_) - - with db: - return list(query.dicts()) - - @api_v1_router.get("/tickets") -def get_tickets(status: TicketStatus | None = None, type_: IssueType | None = None): +def get_tickets( + status: TicketStatus | None = None, + type_: IssueType | None = None, + page: int = 1, + page_size: int = 10, +): """Get all tickets. This function is used to get all tickets with status open. """ with db: - return _get_ticket(status, type_) + offset = (page - 1) * page_size + # 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) + ) + .count() + ) + max_page = count // page_size + int(count % page_size != 0) + if page > max_page: + raise HTTPException( + status_code=404, + detail=f"Page {page} not found. Max page is {max_page}", + ) + return { + "tickets": list( + TicketModel.select() + .where( + (TicketModel.status == status if status else True) + & (TicketModel.type == type_ if type_ else True) + ) + .order_by(TicketModel.created_at.desc()) + .offset(offset) + .limit(page_size) + .dicts() + ), + "max_page": max_page, + } @api_v1_router.get("/tickets/{ticket_id}")