2025-03-24 19:55:08 -04:00
|
|
|
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
|
|
|
use axum_extra::extract::CookieJar;
|
2025-03-27 18:10:47 -04:00
|
|
|
use tetratto_core::model::{ApiReturn, Error, communities::Post};
|
2025-03-24 19:55:08 -04:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
State, get_user_from_token,
|
2025-03-29 00:26:56 -04:00
|
|
|
routes::api::v1::{CreatePost, UpdatePostContent, UpdatePostContext},
|
2025-03-24 19:55:08 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
pub async fn create_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
2025-03-29 00:26:56 -04:00
|
|
|
Json(req): Json<CreatePost>,
|
2025-03-24 19:55:08 -04:00
|
|
|
) -> 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
|
2025-03-27 18:10:47 -04:00
|
|
|
.create_post(Post::new(
|
2025-03-25 22:52:47 -04:00
|
|
|
req.content,
|
2025-03-29 00:26:56 -04:00
|
|
|
match req.community.parse::<usize>() {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
|
|
|
},
|
|
|
|
if let Some(rt) = req.replying_to {
|
|
|
|
match rt.parse::<usize>() {
|
|
|
|
Ok(x) => Some(x),
|
|
|
|
Err(e) => return Json(Error::MiscError(e.to_string()).into()),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
},
|
2025-03-25 22:52:47 -04:00
|
|
|
user.id,
|
|
|
|
))
|
2025-03-24 19:55:08 -04:00
|
|
|
.await
|
|
|
|
{
|
2025-03-29 00:26:56 -04:00
|
|
|
Ok(id) => Json(ApiReturn {
|
2025-03-24 19:55:08 -04:00
|
|
|
ok: true,
|
2025-03-27 18:10:47 -04:00
|
|
|
message: "Post created".to_string(),
|
2025-03-29 00:26:56 -04:00
|
|
|
payload: Some(id.to_string()),
|
2025-03-24 19:55:08 -04:00
|
|
|
}),
|
|
|
|
Err(e) => return 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-25 22:52:47 -04:00
|
|
|
match data.delete_post(id, user).await {
|
2025-03-24 19:55:08 -04:00
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
2025-03-27 18:10:47 -04:00
|
|
|
message: "Post deleted".to_string(),
|
2025-03-24 19:55:08 -04:00
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => return Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn update_content_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Path(id): Path<usize>,
|
2025-03-29 00:26:56 -04:00
|
|
|
Json(req): Json<UpdatePostContent>,
|
2025-03-24 19:55:08 -04:00
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-25 21:19:55 -04:00
|
|
|
match data.update_post_content(id, user, req.content).await {
|
2025-03-24 19:55:08 -04:00
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
2025-03-27 18:10:47 -04:00
|
|
|
message: "Post updated".to_string(),
|
2025-03-24 19:55:08 -04:00
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => return Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|
2025-03-24 20:23:52 -04:00
|
|
|
|
|
|
|
pub async fn update_context_request(
|
|
|
|
jar: CookieJar,
|
|
|
|
Extension(data): Extension<State>,
|
|
|
|
Path(id): Path<usize>,
|
2025-03-29 00:26:56 -04:00
|
|
|
Json(req): Json<UpdatePostContext>,
|
2025-03-24 20:23:52 -04:00
|
|
|
) -> 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()),
|
|
|
|
};
|
|
|
|
|
2025-03-25 21:19:55 -04:00
|
|
|
match data.update_post_context(id, user, req.context).await {
|
2025-03-24 20:23:52 -04:00
|
|
|
Ok(_) => Json(ApiReturn {
|
|
|
|
ok: true,
|
2025-03-27 18:10:47 -04:00
|
|
|
message: "Post updated".to_string(),
|
2025-03-24 20:23:52 -04:00
|
|
|
payload: (),
|
|
|
|
}),
|
|
|
|
Err(e) => return Json(e.into()),
|
|
|
|
}
|
|
|
|
}
|