76 lines
2 KiB
Rust
76 lines
2 KiB
Rust
|
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||
|
use axum_extra::extract::CookieJar;
|
||
|
use tetratto_core::model::{communities::PostDraft, ApiReturn, Error};
|
||
|
use crate::{
|
||
|
get_user_from_token,
|
||
|
routes::api::v1::{CreatePostDraft, UpdatePostContent},
|
||
|
State,
|
||
|
};
|
||
|
|
||
|
pub async fn create_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Json(req): Json<CreatePostDraft>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data
|
||
|
.create_draft(PostDraft::new(req.content, user.id))
|
||
|
.await
|
||
|
{
|
||
|
Ok(id) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "Draft created".to_string(),
|
||
|
payload: Some(id.to_string()),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn delete_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.delete_draft(id, user).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "Draft deleted".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub async fn update_content_request(
|
||
|
jar: CookieJar,
|
||
|
Extension(data): Extension<State>,
|
||
|
Path(id): Path<usize>,
|
||
|
Json(req): Json<UpdatePostContent>,
|
||
|
) -> impl IntoResponse {
|
||
|
let data = &(data.read().await).0;
|
||
|
let user = match get_user_from_token!(jar, data) {
|
||
|
Some(ua) => ua,
|
||
|
None => return Json(Error::NotAllowed.into()),
|
||
|
};
|
||
|
|
||
|
match data.update_draft_content(id, user, req.content).await {
|
||
|
Ok(_) => Json(ApiReturn {
|
||
|
ok: true,
|
||
|
message: "Draft updated".to_string(),
|
||
|
payload: (),
|
||
|
}),
|
||
|
Err(e) => Json(e.into()),
|
||
|
}
|
||
|
}
|