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/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" diff --git a/back/data/data.db b/back/data/data.db deleted file mode 100644 index e69de29..0000000 diff --git a/back/src/creat_db.rs b/back/src/creat_db.rs deleted file mode 100644 index ec5a2ed..0000000 --- a/back/src/creat_db.rs +++ /dev/null @@ -1,15 +0,0 @@ -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/create_db.rs b/back/src/create_db.rs new file mode 100644 index 0000000..67eb2af --- /dev/null +++ b/back/src/create_db.rs @@ -0,0 +1,21 @@ +use sqlite::{Connection, OpenFlags}; + +pub fn init() -> sqlite::Result<()> { + let conn = Connection::open_with_flags( + "./data/data.db", + OpenFlags::new() + .with_create() + .with_read_write() + )?; + + conn.execute( + "CREATE TABLE IF NOT EXISTS articles ( + id INTEGER PRIMARY KEY, + title TEXT NOT NULL, + subTitle TEXT, + content TEXT NOT NULL + )", + )?; + + Ok(()) +} diff --git a/back/src/main.rs b/back/src/main.rs index a91bc15..ad8f7f5 100644 --- a/back/src/main.rs +++ b/back/src/main.rs @@ -1,7 +1,21 @@ +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}; +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 { @@ -17,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) @@ -45,7 +65,7 @@ async fn api() -> impl Responder { #[actix_web::main] async fn main() -> Result<(), std::io::Error> { - let conn = Connection::open("./data/data.db"); + let _ = init(); HttpServer::new(|| { App::new() 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