libreddit/src/popular.rs

37 lines
883 B
Rust
Raw Normal View History

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