How to implement database pagination

Implement database pagination in Axum by defining a Deserialize struct and using the Query extractor to parse page and limit parameters.

Implement database pagination by defining a Pagination struct with page and per_page fields, then using the Query extractor in your Axum handler to deserialize these values from the URL.

use axum::{extract::Query, routing::get, Router};
use serde::Deserialize;

#[derive(Deserialize)]
struct Pagination {
    page: usize,
    per_page: usize,
}

async fn list_items(pagination: Query<Pagination>) {
    let offset = (pagination.page - 1) * pagination.per_page;
    let limit = pagination.per_page;
    // Fetch items from database using offset and limit
}

let app = Router::new().route("/items", get(list_items));