From 28f571e02babc91b814ed026901445e8d74ce4df Mon Sep 17 00:00:00 2001 From: Milim Date: Sat, 7 Dec 2024 10:58:50 +0100 Subject: [PATCH] add database model types and a few other db things --- .../20241207082654_initial_table_setup.sql | 4 ++-- src/db/mod.rs | 11 ++++++++++ src/db/models.rs | 21 +++++++++++++++++++ src/db/postgres.rs | 1 + src/db/sqlite.rs | 5 +++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/db/models.rs create mode 100644 src/db/postgres.rs create mode 100644 src/db/sqlite.rs diff --git a/migrations/sqlite/20241207082654_initial_table_setup.sql b/migrations/sqlite/20241207082654_initial_table_setup.sql index e6842a0..3fb9ee3 100644 --- a/migrations/sqlite/20241207082654_initial_table_setup.sql +++ b/migrations/sqlite/20241207082654_initial_table_setup.sql @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS books( book_id INTEGER PRIMARY KEY, book_title TEXT NOT NULL, book_description TEXT, - book_creation_date INTEGER NOT NULL, + book_creation_date TEXT NOT NULL, author_id INTEGER NOT NULL, FOREIGN KEY (author_id) REFERENCES users(user_id) ON DELETE CASCADE @@ -17,7 +17,7 @@ CREATE TABLE IF NOT EXISTS chapters( chapter_id INTEGER PRIMARY KEY, chapter_title TEXT NOT NULL, chapter_text TEXT NOT NULL, - chapter_creation_date INT NOT NULL, + chapter_creation_date TEXT NOT NULL, book_id INTEGER NOT NULL, author_id INTEGER NOT NULL, FOREIGN KEY (book_id) REFERENCES books(book_id) ON DELETE CASCADE, diff --git a/src/db/mod.rs b/src/db/mod.rs index 44214b8..a894e29 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,6 +2,10 @@ use log::{info, warn}; use sqlx::{migrate::MigrateDatabase, PgPool, Postgres, Sqlite, SqlitePool}; +pub mod models; +mod postgres; +mod sqlite; + /// Utility for interacting with the database #[derive(Clone)] pub enum DataBase { @@ -62,4 +66,11 @@ impl DataBase { Self::Postgres(pool) } + + pub fn get_chapter(&self, id: u32) -> models::Chapter { + match self { + DataBase::Sqlite(pool) => sqlite::sqlite_chapter(pool, id), + DataBase::Postgres(pool) => todo!(), + } + } } diff --git a/src/db/models.rs b/src/db/models.rs new file mode 100644 index 0000000..ac2fd84 --- /dev/null +++ b/src/db/models.rs @@ -0,0 +1,21 @@ +pub struct Chapter { + pub id: u32, + pub title: String, + pub text: String, + pub creation_data: String, + pub book_id: u32, + pub author_id: u32, +} + +pub struct Book { + pub id: u32, + pub title: String, + pub description: String, + pub creation_date: String, + pub author_id: u32, +} + +pub struct User { + pub id: u32, + pub name: String, +} diff --git a/src/db/postgres.rs b/src/db/postgres.rs new file mode 100644 index 0000000..624925e --- /dev/null +++ b/src/db/postgres.rs @@ -0,0 +1 @@ +//! Module containing database code for Postgres diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs new file mode 100644 index 0000000..f0a727d --- /dev/null +++ b/src/db/sqlite.rs @@ -0,0 +1,5 @@ +//! Module containing database code for SQLite + +pub fn sqlite_chapter(pool: &sqlx::Pool, id: u32) -> super::models::Chapter { + todo!() +}