add basic chapter content view

This commit is contained in:
Milim 2024-12-07 18:01:11 +01:00
parent be6b86ee28
commit 107ad5dfa5
No known key found for this signature in database
5 changed files with 54 additions and 48 deletions

View file

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

View file

@ -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<Sqlite>),
/// Used for Postgres database
@ -33,7 +33,7 @@ impl From<sqlx::Error> for DbError {
}
type DbResult<T> = Result<T, DbError>;
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<models::Book> {
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<u32> {
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<models::Chapter> {
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<u32> {
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<models::User> {
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<u32> {
pub async fn create_user(&self, name: &String) -> DbResult<u32> {
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!(),
}
}
}

View file

@ -23,8 +23,8 @@ pub async fn sqlite_book(pool: &Pool<Sqlite>, id: u32) -> DbResult<Book> {
pub async fn sqlite_book_create(
pool: &Pool<Sqlite>,
title: String,
description: String,
title: &String,
description: &String,
author_id: u32,
) -> DbResult<u32> {
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<Sqlite>, id: u32) -> DbResult<Chapter> {
pub async fn sqlite_chapter_create(
pool: &Pool<Sqlite>,
title: String,
text: String,
title: &String,
text: &String,
book_id: u32,
author_id: u32,
) -> DbResult<u32> {
@ -89,7 +89,7 @@ pub async fn sqlite_user(pool: &Pool<Sqlite>, id: u32) -> DbResult<User> {
Ok(user)
}
pub async fn sqlite_user_create(pool: &Pool<Sqlite>, name: String) -> DbResult<u32> {
pub async fn sqlite_user_create(pool: &Pool<Sqlite>, name: &String) -> DbResult<u32> {
let id = sqlx::query("INSERT INTO users (name) VALUES ( ? )")
.bind(name)
.execute(pool)

View file

@ -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))?

24
src/reading/mod.rs Normal file
View file

@ -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<u32>, db: web::Data<DbInterface>) -> Option<String> {
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<u32>, db: web::Data<DbInterface>) -> Option<String> {
let chapter = db.get_chapter(*id).await.ok()?;
Some(format!("Text: {}", chapter.text))
}