stupid shit, lets clean up
This commit is contained in:
parent
107ad5dfa5
commit
1ae83668ee
4 changed files with 57 additions and 9 deletions
46
src/api.rs
Normal file
46
src/api.rs
Normal file
|
@ -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<BookForm>, db: web::Data<DbInterface>) -> 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<UserForm>, db: web::Data<DbInterface>) -> String {
|
||||||
|
todo!()
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ pub enum DbInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Error type for handling DB related errors
|
/// Error type for handling DB related errors
|
||||||
|
#[derive(Debug)]
|
||||||
pub enum DbError {
|
pub enum DbError {
|
||||||
/// No such entry found
|
/// No such entry found
|
||||||
NotFound,
|
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(
|
pub async fn create_chapter(
|
||||||
&self,
|
&self,
|
||||||
title: &String,
|
title: &String,
|
||||||
|
|
|
@ -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 struct Chapter {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
@ -7,14 +15,6 @@ pub struct Chapter {
|
||||||
pub author_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 struct User {
|
||||||
pub id: u32,
|
pub id: u32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use actix_web::{get, web, App, HttpServer};
|
use actix_web::{get, web, App, HttpServer};
|
||||||
use log::info;
|
use log::info;
|
||||||
|
|
||||||
|
mod api;
|
||||||
mod config;
|
mod config;
|
||||||
mod db;
|
mod db;
|
||||||
mod reading;
|
mod reading;
|
||||||
|
|
Loading…
Add table
Reference in a new issue