From 57d574d143c314e3b7eaf8c3043ad278b45e6605 Mon Sep 17 00:00:00 2001 From: linlkin Date: Thu, 5 Dec 2024 23:19:15 +0100 Subject: [PATCH 01/11] i added db and articles end points --- back/Cargo.toml | 2 +- back/data/data.db | 0 back/src/creat_db.rs | 15 +++++++++++++++ back/src/main.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 back/data/data.db create mode 100644 back/src/creat_db.rs diff --git a/back/Cargo.toml b/back/Cargo.toml index 8b48126..9a70ea3 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" [dependencies] actix-files = "0.6.6" actix-web = "4" - +sqlite = "0.36.1" diff --git a/back/data/data.db b/back/data/data.db new file mode 100644 index 0000000..e69de29 diff --git a/back/src/creat_db.rs b/back/src/creat_db.rs new file mode 100644 index 0000000..ec5a2ed --- /dev/null +++ b/back/src/creat_db.rs @@ -0,0 +1,15 @@ +use sqlite::{Connection, State}; + +fn main() -> sqlite::Result<()> { + let conn = Connection::open("./data/data.db")?; + + conn.execute( + "CREATE TABLE articles ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + subTitle TEXT, + content TEXT NOT NULL, + )", + [], + )?; +} \ No newline at end of file diff --git a/back/src/main.rs b/back/src/main.rs index d4d0ee1..f0a5196 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -1,10 +1,14 @@ -use actix_web::{App, HttpServer}; +use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder}; use actix_files::Files; +use sqlite::{Connection, State, Error}; #[actix_web::main] -async fn main() -> std::io::Result<()> { +async fn main() -> Result<(), std::io::Error> { + let conn = Connection::open("./data/data.db"); + HttpServer::new(|| { App::new() + .service(hello) .service(Files::new("/", "./public").index_file("index.html")) }) .bind(("0.0.0.0", 8080))? @@ -12,3 +16,24 @@ async fn main() -> std::io::Result<()> { .await } +#[get("/api/hello")] +async fn hello() -> impl Responder { + HttpResponse::Ok().body("Hello world!") +} + +#[get("/api/articles")] +async fn get_articles() -> impl Responder { + let conn = Connection::open("./data/data.db").unwrap(); + let mut stmt = conn.prepare("SELECT * FROM articles").unwrap(); + let mut articles = Vec::new(); + + 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)); + } + + HttpResponse::Ok().json(articles) +} From 669c710f895279dcc13691f491f69b1906ae718e Mon Sep 17 00:00:00 2001 From: Lukian Date: Thu, 5 Dec 2024 23:38:43 +0100 Subject: [PATCH 02/11] Added json usage --- back/Cargo.toml | 2 ++ back/src/main.rs | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/back/Cargo.toml b/back/Cargo.toml index 8b48126..e2495de 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" [dependencies] actix-files = "0.6.6" actix-web = "4" +serde = "1.0.215" +serde_json = "1.0.133" diff --git a/back/src/main.rs b/back/src/main.rs index d4d0ee1..e63b176 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -1,11 +1,32 @@ -use actix_web::{App, HttpServer}; +use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType}; use actix_files::Files; +use serde_json::json; + + +#[get("/api")] +async fn api() -> impl Responder { + let value = json!({ + "code": 200, + "success": true, + "payload": { + "features": [ + "serde", + "json" + ], + "homepage": null + } + }); + HttpResponse::Ok() + .content_type(ContentType::json()) + .body(value.to_string()) +} #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| { App::new() - .service(Files::new("/", "./public").index_file("index.html")) + .service(api) + .service(Files::new("/", "public").index_file("index.html")) }) .bind(("0.0.0.0", 8080))? .run() From c3888c61f987e812336f3819fe6bff6a16cb6197 Mon Sep 17 00:00:00 2001 From: linlkin Date: Thu, 5 Dec 2024 23:02:43 +0000 Subject: [PATCH 03/11] Actualiser back/src/main.rs added the service bc i forgot oupsie Signed-off-by: linlkin --- back/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/back/src/main.rs b/back/src/main.rs index f0a5196..f17e20c 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -9,6 +9,7 @@ async fn main() -> Result<(), std::io::Error> { HttpServer::new(|| { App::new() .service(hello) + .service(get_articles) .service(Files::new("/", "./public").index_file("index.html")) }) .bind(("0.0.0.0", 8080))? From 8316a35e9e80302183095676f8e49734ca991aba Mon Sep 17 00:00:00 2001 From: Lukian Date: Thu, 5 Dec 2024 23:38:43 +0100 Subject: [PATCH 04/11] Added json usage --- back/Cargo.toml | 3 +++ back/src/main.rs | 53 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/back/Cargo.toml b/back/Cargo.toml index 9a70ea3..731f030 100644 --- a/back/Cargo.toml +++ b/back/Cargo.toml @@ -7,3 +7,6 @@ edition = "2021" actix-files = "0.6.6" actix-web = "4" sqlite = "0.36.1" +serde = "1.0.215" +serde_json = "1.0.133" + diff --git a/back/src/main.rs b/back/src/main.rs index f17e20c..a91bc15 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -1,22 +1,8 @@ -use actix_web::{get, http, post, web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{App, HttpServer, get, Responder, HttpResponse, http::header::ContentType}; use actix_files::Files; +use serde_json::json; use sqlite::{Connection, State, Error}; -#[actix_web::main] -async fn main() -> Result<(), std::io::Error> { - let conn = Connection::open("./data/data.db"); - - HttpServer::new(|| { - App::new() - .service(hello) - .service(get_articles) - .service(Files::new("/", "./public").index_file("index.html")) - }) - .bind(("0.0.0.0", 8080))? - .run() - .await -} - #[get("/api/hello")] async fn hello() -> impl Responder { HttpResponse::Ok().body("Hello world!") @@ -38,3 +24,38 @@ async fn get_articles() -> impl Responder { HttpResponse::Ok().json(articles) } + +#[get("/api")] +async fn api() -> impl Responder { + let value = json!({ + "code": 200, + "success": true, + "payload": { + "features": [ + "serde", + "json" + ], + "homepage": null + } + }); + HttpResponse::Ok() + .content_type(ContentType::json()) + .body(value.to_string()) +} + +#[actix_web::main] +async fn main() -> Result<(), std::io::Error> { + let conn = Connection::open("./data/data.db"); + + HttpServer::new(|| { + App::new() + .service(hello) + .service(get_articles) + .service(api) + .service(Files::new("/", "public").index_file("index.html")) + }) + .bind(("0.0.0.0", 8080))? + .run() + .await +} + From 902e96b20548169631cd6591730441b48de7e82c Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:00:02 +0100 Subject: [PATCH 05/11] 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 06/11] 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 07/11] 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 08/11] 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 09/11] 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 10/11] 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" From ee692fec27c4d648054a445b249e66c83f525b9a Mon Sep 17 00:00:00 2001 From: linlkin Date: Fri, 6 Dec 2024 01:50:00 +0000 Subject: [PATCH 11/11] Actualiser back/src/main.rs --- back/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/back/src/main.rs b/back/src/main.rs index ad8f7f5..7242d32 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -74,7 +74,7 @@ async fn main() -> Result<(), std::io::Error> { .service(api) .service(Files::new("/", "public").index_file("index.html")) }) - .bind(("0.0.0.0", 8080))? + .bind(("0.0.0.0", 2486))? .run() .await }