| 43 | |
| 44 | #[handler] |
| 45 | async fn list(state: Data<&AppState>, Query(params): Query<Params>) -> Result<impl IntoResponse> { |
| 46 | let conn = &state.conn; |
| 47 | let page = params.page.unwrap_or(1); |
| 48 | let posts_per_page = params.posts_per_page.unwrap_or(DEFAULT_POSTS_PER_PAGE); |
| 49 | |
| 50 | let (posts, num_pages) = QueryCore::find_posts_in_page(conn, page, posts_per_page) |
| 51 | .await |
| 52 | .map_err(InternalServerError)?; |
| 53 | |
| 54 | let mut ctx = tera::Context::new(); |
| 55 | ctx.insert("posts", &posts); |
| 56 | ctx.insert("page", &page); |
| 57 | ctx.insert("posts_per_page", &posts_per_page); |
| 58 | ctx.insert("num_pages", &num_pages); |
| 59 | |
| 60 | let body = state |
| 61 | .templates |
| 62 | .render("index.html.tera", &ctx) |
| 63 | .map_err(InternalServerError)?; |
| 64 | Ok(Html(body)) |
| 65 | } |
| 66 | |
| 67 | #[handler] |
| 68 | async fn new(state: Data<&AppState>) -> Result<impl IntoResponse> { |