From 1ae83668ee97a39788819174171ac5d15e81a552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morr=C3=ADgan?= <81323548+TheMorrigna@users.noreply.github.com> Date: Sun, 15 Dec 2024 11:07:51 +0100 Subject: [PATCH] stupid shit, lets clean up --- src/api.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/db/mod.rs | 3 ++- src/db/models.rs | 16 ++++++++-------- src/main.rs | 1 + 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 src/api.rs diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000..2d07234 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,46 @@ +use actix_web::{ + get, + web::{self, Json, Redirect}, + Scope, +}; +use serde::Deserialize; + +use crate::db::DbInterface; + +pub fn api_scope() -> Scope { + web::scope("/api") + .service(create_book) + .service(create_chapter) + .service(create_user) +} + +#[derive(Deserialize)] +struct BookForm { + title: String, + description: String, +} + +#[get("/create/book")] +async fn create_book(Json(form): Json, db: web::Data) -> Redirect { + let id = db + .create_book(&form.title, &form.description, todo!()) + .await + .unwrap(); + + Redirect::to(format!("r/b/{}", id)).permanent() +} + +#[get("/create/chapter")] +async fn create_chapter() -> String { + todo!() +} + +#[derive(Deserialize)] +struct UserForm { + name: String, +} + +#[get("/create/user")] +async fn create_user(web::Form(form): web::Form, db: web::Data) -> String { + todo!() +} diff --git a/src/db/mod.rs b/src/db/mod.rs index 69236fc..c058993 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -16,6 +16,7 @@ pub enum DbInterface { } /// Error type for handling DB related errors +#[derive(Debug)] pub enum DbError { /// No such entry found NotFound, @@ -116,7 +117,7 @@ impl DbInterface { } } - /// Tries to create a chapter and returns the chapter's id if successfu + /// Tries to create a chapter and returns the chapter's id if successful pub async fn create_chapter( &self, title: &String, diff --git a/src/db/models.rs b/src/db/models.rs index 470c4d1..51feae0 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -1,3 +1,11 @@ +pub struct Book { + pub id: u32, + pub title: String, + pub description: String, + pub creation_date: String, + pub author_id: u32, +} + pub struct Chapter { pub id: u32, pub title: String, @@ -7,14 +15,6 @@ pub struct Chapter { 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/main.rs b/src/main.rs index b7c77d8..3470a57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use actix_web::{get, web, App, HttpServer}; use log::info; +mod api; mod config; mod db; mod reading;