add database model types and a few other db things

This commit is contained in:
Milim 2024-12-07 10:58:50 +01:00
parent 0e50b214ed
commit 28f571e02b
No known key found for this signature in database
5 changed files with 40 additions and 2 deletions

View file

@ -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,

View file

@ -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!(),
}
}
}

21
src/db/models.rs Normal file
View file

@ -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,
}

1
src/db/postgres.rs Normal file
View file

@ -0,0 +1 @@
//! Module containing database code for Postgres

5
src/db/sqlite.rs Normal file
View file

@ -0,0 +1,5 @@
//! Module containing database code for SQLite
pub fn sqlite_chapter(pool: &sqlx::Pool<sqlx::Sqlite>, id: u32) -> super::models::Chapter {
todo!()
}