forge
Explore
Log in

Don't you dare go Hollow

project_note

Clone
★ 0

0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits

notebooks.rs42 lines, 1.0 KiBHistoryRawDownload
1use std::sync::Arc;
2
3use serde::Deserialize;
4use 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
14use crate::store::Store;
15
16mod notebook_id;
17
18#[derive(Deserialize)]
19struct NameForm {
20 name: String,
21}
22
23/// Trims a notebook name and rejects blank names.
24fn 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/// POST /notebooks: creates a notebook and opens it.
33#[route(POST)]
34async 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}