| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use serde::Deserialize; |
| 4 | use topcoat::{ |
| 5 | Result, |
| 6 | context::{Cx, app_context}, |
| 7 | router::{ |
| 8 | content::Form, |
| 9 | error::{SeeOther, bad_request, see_other}, |
| 10 | route, |
| 11 | }, |
| 12 | }; |
| 13 | |
| 14 | use crate::store::Store; |
| 15 | |
| 16 | mod notebook_id; |
| 17 | |
| 18 | #[derive(Deserialize)] |
| 19 | struct NameForm { |
| 20 | name: String, |
| 21 | } |
| 22 | |
| 23 | |
| 24 | fn clean_name(raw: &str) -> Result<String> { |
| 25 | let name = raw.trim(); |
| 26 | if name.is_empty() { |
| 27 | return Err(bad_request("notebook name is required").into()); |
| 28 | } |
| 29 | Ok(name.chars().take(120).collect()) |
| 30 | } |
| 31 | |
| 32 | |
| 33 | #[route(POST)] |
| 34 | async fn create_notebook(cx: &Cx, Form(form): Form<NameForm>) -> Result<SeeOther> { |
| 35 | let store = app_context::<Arc<Store>>(cx); |
| 36 | let name = clean_name(&form.name)?; |
| 37 | let notebook = store |
| 38 | .create_notebook(&name) |
| 39 | .await |
| 40 | .map_err(topcoat::Error::from_anyhow)?; |
| 41 | Ok(see_other(format!("/notebooks/{}?toast=notebook_created", notebook.id))) |
| 42 | } |