diff --git a/src/books/mod.rs b/src/books/mod.rs deleted file mode 100644 index 8fbd08c..0000000 --- a/src/books/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -use actix_web::{get, web, Scope}; - -/// scope to handle all reading related pages -pub fn reading_scope() -> Scope { - web::scope("/b").service(book_view).service(chapter_view) -} - -/// route to view info for a specific book -#[get("/{book}")] -async fn book_view(book: web::Path) -> String { - format!("This is the info for {book}") -} - -/// view for reading a chapter -#[get("/{book}/{chapter})")] -async fn chapter_view(path: web::Path<(String, String)>) -> String { - format!("This is {} of {}", path.0, path.1) -} diff --git a/src/db/mod.rs b/src/db/mod.rs index 4cd2f49..69236fc 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -8,7 +8,7 @@ mod sqlite; /// Utility for interacting with the database #[derive(Clone)] -pub enum DataBase { +pub enum DbInterface { /// Used for Sqlite database Sqlite(sqlx::Pool), /// Used for Postgres database @@ -33,7 +33,7 @@ impl From for DbError { } type DbResult = Result; -impl DataBase { +impl DbInterface { /// Database backed by SQLite pub async fn sqlite() -> Self { // Check if db exists, if not create it. @@ -88,63 +88,63 @@ impl DataBase { /// Tries to fetch a book from the database pub async fn get_book(&self, id: u32) -> DbResult { match self { - DataBase::Sqlite(pool) => sqlite::sqlite_book(pool, id).await, - DataBase::Postgres(pool) => todo!(), + DbInterface::Sqlite(pool) => sqlite::sqlite_book(pool, id).await, + DbInterface::Postgres(pool) => todo!(), } } /// Tries to create a book and returns the book's id if successful pub async fn create_book( &self, - title: String, - description: String, + title: &String, + description: &String, author_id: u32, ) -> DbResult { match self { - DataBase::Sqlite(pool) => { + DbInterface::Sqlite(pool) => { sqlite::sqlite_book_create(pool, title, description, author_id).await } - DataBase::Postgres(pool) => todo!(), + DbInterface::Postgres(pool) => todo!(), } } /// Tries to fetch a chapter from the database pub async fn get_chapter(&self, id: u32) -> DbResult { match self { - DataBase::Sqlite(pool) => sqlite::sqlite_chapter(pool, id).await, - DataBase::Postgres(pool) => todo!(), + DbInterface::Sqlite(pool) => sqlite::sqlite_chapter(pool, id).await, + DbInterface::Postgres(pool) => todo!(), } } /// Tries to create a chapter and returns the chapter's id if successfu pub async fn create_chapter( &self, - title: String, - text: String, + title: &String, + text: &String, book_id: u32, author_id: u32, ) -> DbResult { match self { - DataBase::Sqlite(pool) => { + DbInterface::Sqlite(pool) => { sqlite::sqlite_chapter_create(pool, title, text, book_id, author_id).await } - DataBase::Postgres(pool) => todo!(), + DbInterface::Postgres(pool) => todo!(), } } /// Tries to fetch a user from the database pub async fn get_user(&self, id: u32) -> DbResult { match self { - DataBase::Sqlite(pool) => sqlite::sqlite_user(pool, id).await, - DataBase::Postgres(pool) => todo!(), + DbInterface::Sqlite(pool) => sqlite::sqlite_user(pool, id).await, + DbInterface::Postgres(pool) => todo!(), } } /// Tries to create a user and returns the user's id if successful - pub async fn create_user(&self, name: String) -> DbResult { + pub async fn create_user(&self, name: &String) -> DbResult { match self { - DataBase::Sqlite(pool) => sqlite::sqlite_user_create(pool, name).await, - DataBase::Postgres(pool) => todo!(), + DbInterface::Sqlite(pool) => sqlite::sqlite_user_create(pool, name).await, + DbInterface::Postgres(pool) => todo!(), } } } diff --git a/src/db/sqlite.rs b/src/db/sqlite.rs index 201fabf..1f52e46 100644 --- a/src/db/sqlite.rs +++ b/src/db/sqlite.rs @@ -23,8 +23,8 @@ pub async fn sqlite_book(pool: &Pool, id: u32) -> DbResult { pub async fn sqlite_book_create( pool: &Pool, - title: String, - description: String, + title: &String, + description: &String, author_id: u32, ) -> DbResult { let id = sqlx::query("INSERT INTO books (title, description, author_id, book_creation_date) VALUES ( ?1, ?2, ?3, ?4 )") @@ -57,8 +57,8 @@ pub async fn sqlite_chapter(pool: &Pool, id: u32) -> DbResult { pub async fn sqlite_chapter_create( pool: &Pool, - title: String, - text: String, + title: &String, + text: &String, book_id: u32, author_id: u32, ) -> DbResult { @@ -89,7 +89,7 @@ pub async fn sqlite_user(pool: &Pool, id: u32) -> DbResult { Ok(user) } -pub async fn sqlite_user_create(pool: &Pool, name: String) -> DbResult { +pub async fn sqlite_user_create(pool: &Pool, name: &String) -> DbResult { let id = sqlx::query("INSERT INTO users (name) VALUES ( ? )") .bind(name) .execute(pool) diff --git a/src/main.rs b/src/main.rs index 6019417..b7c77d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -use actix_web::{get, App, HttpServer}; +use actix_web::{get, web, App, HttpServer}; use log::info; -mod books; mod config; mod db; +mod reading; #[actix_web::main] async fn main() -> Result<(), std::io::Error> { @@ -13,14 +13,14 @@ async fn main() -> Result<(), std::io::Error> { .init(); let config = config::parse_config(); - let db = db::DataBase::sqlite().await; + let db = db::DbInterface::sqlite().await; info!("Server starting..."); HttpServer::new(move || { App::new() - .app_data(config) - .app_data(db.clone()) - .service(books::reading_scope()) + .app_data(web::Data::new(config)) + .app_data(web::Data::new(db.clone())) + .service(reading::reading_scope()) .service(hello) }) .bind((config.binding_ip, config.port))? diff --git a/src/reading/mod.rs b/src/reading/mod.rs new file mode 100644 index 0000000..f6bdc3c --- /dev/null +++ b/src/reading/mod.rs @@ -0,0 +1,24 @@ +use actix_web::{get, web, Scope}; + +use crate::db::DbInterface; + +/// scope to handle all reading related pages +pub fn reading_scope() -> Scope { + web::scope("/r").service(book_view).service(chapter_view) +} + +/// route to view info for a specific book +#[get("/b/{book}")] +async fn book_view(book: web::Path, db: web::Data) -> Option { + Some(format!( + "This is the info for {}", + db.get_book(*book).await.ok()?.title + )) +} + +/// view for reading a chapter +#[get("/c/{chapter})")] +async fn chapter_view(id: web::Path, db: web::Data) -> Option { + let chapter = db.get_chapter(*id).await.ok()?; + Some(format!("Text: {}", chapter.text)) +}