use std::{fs::File, env::args};
use actix_files as fs;
-use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpResponse, web::Query};
+use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpResponse, HttpRequest, web::Query};
use askama::Template;
use listenfd::ListenFd;
mod db;
#[derive(Template)]
-#[template(path = "main.html")]
-struct MainTemplate<'a> {
- test: &'a str,
+#[template(path = "home.html")]
+struct HomeTemplate {
+ recipes: Vec<db::Recipe>
+}
+
+#[derive(Template)]
+#[template(path = "view_recipe.html")]
+struct ViewRecipeTemplate {
+ recipes: Vec<db::Recipe>,
+ current_recipe: db::Recipe
}
#[derive(Deserialize)]
m: Option<String>
}
-fn main_page(query: Query<Request>) -> HttpResponse {
-
- let main_template = MainTemplate { test: &"*** test ***" };
+#[get("/")]
+async fn home_page(req: HttpRequest) -> impl Responder {
+ HomeTemplate { recipes: vec![ db::Recipe { title: String::from("Saumon en croûte feuilletée"), id: 1 }, db::Recipe { title: String::from("Croissant au jambon"), id: 2 } ] }
+}
- let s = main_template.render().unwrap();
- HttpResponse::Ok().content_type("text/html").body(s)
+#[get("/recipe/view/{id}")]
+async fn view_page(req: HttpRequest, path: web::Path<(i32,)>) -> impl Responder {
+ panic!("ERROR");
+ ViewRecipeTemplate { recipes: vec![ db::Recipe { title: String::from("Saumon en croûte feuilletée"), id: 1 }, db::Recipe { title: String::from("Croissant au jambon"), id: 2 } ], current_recipe: db::Recipe { title: String::from("Saumon en croûte feuilletée"), id: 1 } }
}
#[derive(Debug, Deserialize)]
|| {
App::new()
.wrap(middleware::Compress::default())
- .service(web::resource("/").to(main_page))
+ .service(home_page)
+ .service(view_page)
.service(fs::Files::new("/static", "static").show_files_listing())
}
);
return true
} else if args.iter().any(|arg| arg == "--test") {
let db_connection = db::Connection::new();
- db_connection.create_or_update();
return true
}