From 902e96b20548169631cd6591730441b48de7e82c Mon Sep 17 00:00:00 2001 From: Lukian Date: Fri, 6 Dec 2024 01:00:02 +0100 Subject: [PATCH] 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()