use std::sync::Arc; use serde::Deserialize; use topcoat::{ Result, context::{Cx, app_context}, router::{ content::Form, error::{SeeOther, bad_request, see_other}, route, }, }; use crate::store::Store; mod notebook_id; #[derive(Deserialize)] struct NameForm { name: String, } /// Trims a notebook name and rejects blank names. fn clean_name(raw: &str) -> Result { let name = raw.trim(); if name.is_empty() { return Err(bad_request("notebook name is required").into()); } Ok(name.chars().take(120).collect()) } /// POST /notebooks: creates a notebook and opens it. #[route(POST)] async fn create_notebook(cx: &Cx, Form(form): Form) -> Result { let store = app_context::>(cx); let name = clean_name(&form.name)?; let notebook = store .create_notebook(&name) .await .map_err(topcoat::Error::from_anyhow)?; Ok(see_other(format!("/notebooks/{}?toast=notebook_created", notebook.id))) }