From 902e96b20548169631cd6591730441b48de7e82c Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:00:02 +0100 Subject: [PATCH 1/6] Added database init function --- .gitignore | 3 ++- back/data/data.db | Bin 0 -> 8192 bytes back/src/{creat_db.rs => create_db.rs} | 13 +++++++------ back/src/main.rs | 5 ++++- 4 files changed, 13 insertions(+), 8 deletions(-) rename back/src/{creat_db.rs => create_db.rs} (52%) diff --git a/.gitignore b/.gitignore index e63b1ee..f4214eb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ back/target/ front/dist/ back/public/ +back/data/ +back/Cargo.lock -back/Cargo.lock \ No newline at end of file diff --git a/back/data/data.db b/back/data/data.db index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..df4b1bae9776ddcf50b6e841d543c37499ff8c0c 100644 GIT binary patch literal 8192 zcmeI#F$=;l5Cz~w1VIqpoP-+{#Kl>#N|0i$8Y9@TYJ@;luyGaimzxePc5+tUBS*OQ zlHuF3SqH{yI$dw)*wT=-80TzE#2DkQDz5TmeKl{~p7r0t+29d1+!Sw)m|F-0AOHaf zKmY;|fB*y_009U<;P(WcV_s`_I{dY<@jlV<&6-1^(|oT?rSOy>WeL(E^hB-Z*%wP bM<4(J2tWV=5P$##AOHafKmY;|_+x=LoZL9g literal 0 HcmV?d00001 diff --git a/back/src/creat_db.rs b/back/src/create_db.rs similarity index 52% rename from back/src/creat_db.rs rename to back/src/create_db.rs index ec5a2ed..60140e1 100644 --- a/back/src/creat_db.rs +++ b/back/src/create_db.rs @@ -1,15 +1,16 @@ -use sqlite::{Connection, State}; +use sqlite::{Connection, State, Error}; -fn main() -> sqlite::Result<()> { +pub fn init() -> sqlite::Result<()> { let conn = Connection::open("./data/data.db")?; conn.execute( - "CREATE TABLE articles ( + "CREATE TABLE IF NOT EXISTS articles ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, subTitle TEXT, - content TEXT NOT NULL, + content TEXT NOT NULL )", - [], )?; -} \ No newline at end of file + + Ok(()) +} diff --git a/back/src/main.rs b/back/src/main.rs index a91bc15..e090ab8 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -3,6 +3,9 @@ use actix_files::Files; use serde_json::json; use sqlite::{Connection, State, Error}; +mod create_db; +use create_db::init; + #[get("/api/hello")] async fn hello() -> impl Responder { HttpResponse::Ok().body("Hello world!") @@ -45,7 +48,7 @@ async fn api() -> impl Responder { #[actix_web::main] async fn main() -> Result<(), std::io::Error> { - let conn = Connection::open("./data/data.db"); + init(); HttpServer::new(|| { App::new() From 8f8fc0c46e1bbdaa3a15c1856e4f320adddb4b34 Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:30:34 +0100 Subject: [PATCH 2/6] Fixed databse init function --- back/data/data.db | Bin 8192 -> 0 bytes back/src/create_db.rs | 11 ++++++++--- back/src/main.rs | 10 +++++----- 3 files changed, 13 insertions(+), 8 deletions(-) delete mode 100644 back/data/data.db diff --git a/back/data/data.db b/back/data/data.db deleted file mode 100644 index df4b1bae9776ddcf50b6e841d543c37499ff8c0c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8192 zcmeI#F$=;l5Cz~w1VIqpoP-+{#Kl>#N|0i$8Y9@TYJ@;luyGaimzxePc5+tUBS*OQ zlHuF3SqH{yI$dw)*wT=-80TzE#2DkQDz5TmeKl{~p7r0t+29d1+!Sw)m|F-0AOHaf zKmY;|fB*y_009U<;P(WcV_s`_I{dY<@jlV<&6-1^(|oT?rSOy>WeL(E^hB-Z*%wP bM<4(J2tWV=5P$##AOHafKmY;|_+x=LoZL9g diff --git a/back/src/create_db.rs b/back/src/create_db.rs index 60140e1..06e2663 100644 --- a/back/src/create_db.rs +++ b/back/src/create_db.rs @@ -1,9 +1,14 @@ -use sqlite::{Connection, State, Error}; +use sqlite::{Connection, OpenFlags}; pub fn init() -> sqlite::Result<()> { - let conn = Connection::open("./data/data.db")?; + let conn = Connection::open_with_flags( + "./data/data.db", + OpenFlags::new() + .with_create() + .with_read_write() + ); - conn.execute( + conn?.execute( "CREATE TABLE IF NOT EXISTS articles ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, diff --git a/back/src/main.rs b/back/src/main.rs index e090ab8..320c18f 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -1,10 +1,10 @@ +mod create_db; +use create_db::init; + use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType}; use actix_files::Files; use serde_json::json; -use sqlite::{Connection, State, Error}; - -mod create_db; -use create_db::init; +use sqlite::{Connection, State}; #[get("/api/hello")] async fn hello() -> impl Responder { @@ -48,7 +48,7 @@ async fn api() -> impl Responder { #[actix_web::main] async fn main() -> Result<(), std::io::Error> { - init(); + let _ = init(); HttpServer::new(|| { App::new() From 85ab4aba4f6ee132bde85e7ffaa9839b04bb0a6a Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:31:27 +0100 Subject: [PATCH 3/6] Fixed databse init function again --- back/src/create_db.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/back/src/create_db.rs b/back/src/create_db.rs index 06e2663..67eb2af 100644 --- a/back/src/create_db.rs +++ b/back/src/create_db.rs @@ -6,9 +6,9 @@ pub fn init() -> sqlite::Result<()> { OpenFlags::new() .with_create() .with_read_write() - ); + )?; - conn?.execute( + conn.execute( "CREATE TABLE IF NOT EXISTS articles ( id INTEGER PRIMARY KEY, title TEXT NOT NULL, From 9b91a5acf36eba0c8941ce1fb1ab9b89eb79f03c Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:33:19 +0100 Subject: [PATCH 4/6] Updated docker-compose.yml --- docker-compose.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index 1e8480c..1e0df8d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -7,4 +7,6 @@ services: restart: always ports: - 8080:8080 + volumes: + - ./back/data:/app/data From 3688d031b4ff20a63237f934cb20c94b2f0674cb Mon Sep 17 00:00:00 2001 From: linlkin Date: Fri, 6 Dec 2024 00:42:10 +0000 Subject: [PATCH 5/6] Actualiser back/src/main.rs finito ? --- back/src/main.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/back/src/main.rs b/back/src/main.rs index 320c18f..ad8f7f5 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -5,6 +5,17 @@ use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::Con use actix_files::Files; use serde_json::json; use sqlite::{Connection, State}; +use serde::{Serialize, Deserialize}; + +#[derive(serde::Serialize)] +struct Article { + id: i64, + title: String, + auteur: Option, + edited_at: Option, + published_at: Option, + content: String, +} #[get("/api/hello")] async fn hello() -> impl Responder { @@ -20,9 +31,15 @@ async fn get_articles() -> impl Responder { while let State::Row = stmt.next().unwrap() { let id = stmt.read::(0).unwrap(); let title = stmt.read::(1).unwrap(); - let subTitle = stmt.read::(2).unwrap(); let content = stmt.read::(3).unwrap(); - articles.push((id, title, subTitle, content)); + articles.push(Article { + id, + title, + auteur: None, + edited_at: None, + published_at: None, + content + }); } HttpResponse::Ok().json(articles) From e2575b55b6edd11626e3db12f603fd2cb5153413 Mon Sep 17 00:00:00 2001 From: linlkin Date: Fri, 6 Dec 2024 00:43:11 +0000 Subject: [PATCH 6/6] Actualiser back/Cargo.toml --- back/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/Cargo.toml b/back/Cargo.toml index 731f030..6be9ee0 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -7,6 +7,6 @@ edition = "2021" actix-files = "0.6.6" actix-web = "4" sqlite = "0.36.1" -serde = "1.0.215" +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0.133"