| 87 | } |
| 88 | |
| 89 | async fn list_posts( |
| 90 | state: State<AppState>, |
| 91 | Query(params): Query<Params>, |
| 92 | cookies: Cookies, |
| 93 | ) -> Result<Html<String>, (StatusCode, &'static str)> { |
| 94 | let page = params.page.unwrap_or(1); |
| 95 | let posts_per_page = params.posts_per_page.unwrap_or(5); |
| 96 | |
| 97 | let (posts, num_pages) = QueryCore::find_posts_in_page(&state.conn, page, posts_per_page) |
| 98 | .await |
| 99 | .expect("Cannot find posts in page"); |
| 100 | |
| 101 | let mut ctx = tera::Context::new(); |
| 102 | ctx.insert("posts", &posts); |
| 103 | ctx.insert("page", &page); |
| 104 | ctx.insert("posts_per_page", &posts_per_page); |
| 105 | ctx.insert("num_pages", &num_pages); |
| 106 | |
| 107 | if let Some(value) = get_flash_cookie::<FlashData>(&cookies) { |
| 108 | ctx.insert("flash", &value); |
| 109 | } |
| 110 | |
| 111 | let body = state |
| 112 | .templates |
| 113 | .render("index.html.tera", &ctx) |
| 114 | .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "Template error"))?; |
| 115 | |
| 116 | Ok(Html(body)) |
| 117 | } |
| 118 | |
| 119 | async fn new_post(state: State<AppState>) -> Result<Html<String>, (StatusCode, &'static str)> { |
| 120 | let ctx = tera::Context::new(); |