redlib/src/popular.rs

37 lines
883 B
Rust
Raw Normal View History

2020-10-25 13:25:59 -07:00
// CRATES
2020-10-25 20:30:34 -07:00
use actix_web::{get, web, HttpResponse, Result};
2020-10-25 13:25:59 -07:00
use askama::Template;
2020-10-25 20:57:19 -07:00
#[path = "subreddit.rs"]
mod subreddit;
2020-11-17 11:37:40 -08:00
use subreddit::{posts, Post};
2020-10-25 13:25:59 -07:00
2020-11-17 16:03:28 -08:00
#[path = "utils.rs"]
mod utils;
2020-11-18 16:31:46 -08:00
use utils::Params;
2020-11-17 16:03:28 -08:00
2020-10-25 13:25:59 -07:00
// STRUCTS
#[derive(Template)]
#[template(path = "popular.html", escape = "none")]
struct PopularTemplate {
2020-11-17 11:37:40 -08:00
posts: Vec<Post>,
2020-10-25 20:57:19 -07:00
sort: String,
2020-10-25 13:25:59 -07:00
}
2020-10-25 20:57:19 -07:00
// RENDER
2020-10-25 13:25:59 -07:00
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {
2020-11-17 11:37:40 -08:00
let posts: Vec<Post> = posts(sub_name, &sort).await;
2020-10-25 20:57:19 -07:00
let s = PopularTemplate { posts: posts, sort: sort }.render().unwrap();
2020-10-25 13:25:59 -07:00
Ok(HttpResponse::Ok().content_type("text/html").body(s))
}
2020-10-25 20:57:19 -07: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,
}
}