| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use topcoat::{ |
| 4 | Result, |
| 5 | context::{Cx, app_context}, |
| 6 | router::{ |
| 7 | error::{RouterErrorExt, SeeOther, bad_request, see_other}, |
| 8 | module_param, path_param, route, |
| 9 | }, |
| 10 | }; |
| 11 | |
| 12 | use crate::hub::Hub; |
| 13 | use crate::store::Store; |
| 14 | |
| 15 | module_param!(note_id); |
| 16 | |
| 17 | |
| 18 | |
| 19 | #[route(POST "./delete")] |
| 20 | async fn delete_forever(cx: &Cx) -> Result<SeeOther> { |
| 21 | let store = app_context::<Arc<Store>>(cx); |
| 22 | let hub = app_context::<Arc<Hub>>(cx); |
| 23 | let id = path_param::<NoteId>(cx).to_owned(); |
| 24 | let meta = store |
| 25 | .get_note(&id) |
| 26 | .await |
| 27 | .map_err(topcoat::Error::from_anyhow)? |
| 28 | .ok_or_not_found()?; |
| 29 | if meta.trashed_at.is_none() { |
| 30 | return Err(bad_request("the note is not in the trash").into()); |
| 31 | } |
| 32 | store.delete_note(&id).await.map_err(topcoat::Error::from_anyhow)?; |
| 33 | hub.forget(&id); |
| 34 | Ok(see_other("/trash")) |
| 35 | } |