Add some data access methods to Connection
[recipes.git] / backend / src / main.rs
index b483ce2..5c6de4c 100644 (file)
@@ -1,15 +1,13 @@
-use std::io::prelude::*;
-use std::{fs::File, env::args};
+use std::fs::File;
+use std::sync::Mutex;
 
 use actix_files as fs;
-use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpResponse, HttpRequest, web::Query};
-
+use actix_web::{get, web, Responder, middleware, App, HttpServer, HttpRequest};
 use askama_actix::Template;
+use clap::Parser;
 use ron::de::from_reader;
 use serde::Deserialize;
 
-use itertools::Itertools;
-
 mod consts;
 mod model;
 mod db;
@@ -17,14 +15,14 @@ mod db;
 #[derive(Template)]
 #[template(path = "home.html")]
 struct HomeTemplate {
-    recipes: Vec<db::Recipe>
+    recipes: Vec<(i32, String)>,
 }
 
 #[derive(Template)]
 #[template(path = "view_recipe.html")]
 struct ViewRecipeTemplate {
-    recipes: Vec<db::Recipe>,
-    current_recipe: db::Recipe
+    recipes: Vec<(i32, String)>,
+    current_recipe: model::Recipe,
 }
 
 #[derive(Deserialize)]
@@ -33,13 +31,16 @@ pub struct Request {
 }
 
 #[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 } ] }
+async fn home_page(req: HttpRequest, connection: web::Data<db::Connection>) -> impl Responder {
+    HomeTemplate { recipes: connection.get_all_recipe_titles().unwrap() } // TODO: unwrap.
 }
 
 #[get("/recipe/view/{id}")]
-async fn view_page(req: HttpRequest, path: web::Path<(i32,)>) -> impl Responder {
-    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 } }
+async fn view_recipe(req: HttpRequest, path: web::Path<(i32,)>, connection: web::Data<db::Connection>) -> impl Responder {
+    ViewRecipeTemplate {
+        recipes: connection.get_all_recipe_titles().unwrap(),
+        current_recipe: connection.get_recipe(path.0).unwrap(),
+    }
 }
 
 #[derive(Debug, Deserialize)]
@@ -72,18 +73,19 @@ async fn main() -> std::io::Result<()> {
 
     println!("Configuration: {:?}", config);
 
-    // let database_connection = db::create_or_update();
+    let db_connection = web::Data::new(db::Connection::new().unwrap()); // TODO: remove unwrap.
 
     std::env::set_var("RUST_LOG", "actix_web=info");
 
     let mut server =
         HttpServer::new(
-            || {
+            move || {
                 App::new()
                     .wrap(middleware::Logger::default())
                     .wrap(middleware::Compress::default())
+                    .app_data(db_connection.clone())
                     .service(home_page)
-                    .service(view_page)
+                    .service(view_recipe)
                     .service(fs::Files::new("/static", "static").show_files_listing())
             }
         );
@@ -93,7 +95,27 @@ async fn main() -> std::io::Result<()> {
     server.run().await
 }
 
+#[derive(Parser, Debug)]
+struct Args {
+    #[arg(long)]
+    test: bool
+}
+
 fn process_args() -> bool {
+    let args = Args::parse();
+
+    if args.test {
+        if let Err(error) = db::Connection::new() {
+            println!("Error: {:?}", error)
+        }
+        return true;
+    }
+
+    false
+
+    /*
+
+
     fn print_usage() {
         println!("Usage:");
         println!(" {} [--help] [--test]", get_exe_name());
@@ -111,6 +133,6 @@ fn process_args() -> bool {
         }
         return true
     }
-
     false
+    */
 }