add: journal note tags and directories
This commit is contained in:
parent
a37312fecf
commit
af6fbdf04e
16 changed files with 722 additions and 78 deletions
|
@ -4,9 +4,12 @@ use axum::{
|
|||
Extension,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_shared::snow::Snowflake;
|
||||
use crate::{
|
||||
get_user_from_token,
|
||||
routes::api::v1::{UpdateJournalPrivacy, CreateJournal, UpdateJournalTitle},
|
||||
routes::api::v1::{
|
||||
AddJournalDir, CreateJournal, RemoveJournalDir, UpdateJournalPrivacy, UpdateJournalTitle,
|
||||
},
|
||||
State,
|
||||
};
|
||||
use tetratto_core::{
|
||||
|
@ -198,3 +201,86 @@ pub async fn delete_request(
|
|||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_dir_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
Json(props): Json<AddJournalDir>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
if props.name.len() > 32 {
|
||||
return Json(Error::DataTooLong("name".to_string()).into());
|
||||
}
|
||||
|
||||
let mut journal = match data.get_journal_by_id(id).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
// add dir
|
||||
journal.dirs.push((
|
||||
Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
match props.parent.parse() {
|
||||
Ok(p) => p,
|
||||
Err(_) => return Json(Error::Unknown.into()),
|
||||
},
|
||||
props.name,
|
||||
));
|
||||
|
||||
// ...
|
||||
match data.update_journal_dirs(id, &user, journal.dirs).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Journal updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn remove_dir_request(
|
||||
jar: CookieJar,
|
||||
Path(id): Path<usize>,
|
||||
Extension(data): Extension<State>,
|
||||
Json(props): Json<RemoveJournalDir>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserManageNotes) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let mut journal = match data.get_journal_by_id(id).await {
|
||||
Ok(x) => x,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
// add dir
|
||||
let dir_id: usize = match props.dir.parse() {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Json(Error::Unknown.into()),
|
||||
};
|
||||
|
||||
journal
|
||||
.dirs
|
||||
.remove(match journal.dirs.iter().position(|x| x.0 == dir_id) {
|
||||
Some(idx) => idx,
|
||||
None => return Json(Error::GeneralNotFound("directory".to_string()).into()),
|
||||
});
|
||||
|
||||
// ...
|
||||
match data.update_journal_dirs(id, &user, journal.dirs).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Journal updated".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue