redsunlib/src/popular.rs

37 lines
885 B
Rust
Raw Normal View History

2020-10-26 09:25:59 +13:00
// CRATES
2020-10-26 16:30:34 +13:00
use actix_web::{get, web, HttpResponse, Result};
2020-10-26 09:25:59 +13:00
use askama::Template;
2020-10-26 16:57:19 +13:00
#[path = "subreddit.rs"]
mod subreddit;
2020-11-18 08:37:40 +13:00
use subreddit::{posts, Post};
2020-10-26 09:25:59 +13:00
2020-11-18 13:03:28 +13:00
#[path = "utils.rs"]
mod utils;
use utils::{Params};
2020-10-26 09:25:59 +13:00
// STRUCTS
#[derive(Template)]
#[template(path = "popular.html", escape = "none")]
struct PopularTemplate {
2020-11-18 08:37:40 +13:00
posts: Vec<Post>,
2020-10-26 16:57:19 +13:00
sort: String,
2020-10-26 09:25:59 +13:00
}
2020-10-26 16:57:19 +13:00
// RENDER
2020-10-26 09:25:59 +13:00
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
2020-11-18 08:37:40 +13:00
let posts: Vec<Post> = posts(sub_name, &sort).await;
2020-10-26 16:57:19 +13:00
let s = PopularTemplate { posts: posts, sort: sort }.render().unwrap();
2020-10-26 09:25:59 +13:00
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-10-26 16:57:19 +13:00
// SERVICES
#[get("/")]
pub async fn page(params: web::Query<Params>) -> Result<HttpResponse> {
match &params.sort {
Some(sort) => render("popular".to_string(), sort.to_string()).await,
None => render("popular".to_string(), "hot".to_string()).await,
}
}