Add Sorting to Homepage

This commit is contained in:
spikecodes 2020-10-25 20:30:34 -07:00
parent 1b5a3a59c0
commit 1e7bbb385c
3 changed files with 14 additions and 3 deletions

1
Cargo.lock generated
View file

@ -1062,6 +1062,7 @@ dependencies = [
"chrono",
"pulldown-cmark",
"reqwest",
"serde",
"serde_json",
]

View file

@ -8,6 +8,7 @@ edition = "2018"
actix-web = "3"
actix-files = "0.4.0"
askama = "0.9"
serde = "1.0.117"
serde_json = "1.0"
reqwest = { version = "0.10", features = ["blocking"] }
pulldown-cmark = "0.8.0"

View file

@ -1,5 +1,6 @@
// CRATES
use actix_web::{get, HttpResponse, Result};
use actix_web::{get, web, HttpResponse, Result};
use serde::Deserialize;
use askama::Template;
#[path = "subreddit.rs"] mod subreddit;
@ -12,9 +13,17 @@ struct PopularTemplate {
sort: String
}
#[derive(Deserialize)]
pub struct Params {
sort: Option<String>
}
#[get("/")]
pub async fn page() -> Result<HttpResponse> {
render("popular".to_string(), "hot".to_string()).await
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,
}
}
async fn render(sub_name: String, sort: String) -> Result<HttpResponse> {