add: taken slug check

This commit is contained in:
trisua 2025-07-20 20:43:01 -04:00
parent a33ee961fe
commit d80368e6c2
5 changed files with 85 additions and 7 deletions

View file

@ -34,6 +34,7 @@ pub fn routes() -> Router {
.route("/api/v1/render", post(render_request))
.route("/api/v1/entries", post(create_request))
.route("/api/v1/entries/{slug}", post(edit_request))
.route("/api/v1/entries/{slug}", get(exists_request))
}
fn default_context(data: &DataClient, build_code: &str) -> Context {
@ -178,6 +179,25 @@ async fn render_request(Json(req): Json<RenderMarkdown>) -> impl IntoResponse {
crate::markdown::render_markdown(&req.content)
}
async fn exists_request(
Extension(data): Extension<State>,
Path(slug): Path<String>,
) -> impl IntoResponse {
let (ref data, _, _) = *data.read().await;
Json(ApiReturn {
ok: true,
message: "Success".to_string(),
payload: data
.query(&SimplifiedQuery {
query: AppDataSelectQuery::KeyIs(format!("entries('{}')", slug)),
mode: AppDataSelectMode::One(0),
})
.await
.is_ok(),
})
}
#[derive(Deserialize)]
struct CreateEntry {
content: String,
@ -206,6 +226,14 @@ async fn create_request(
return Json(Error::DataTooLong("slug".to_string()).into());
}
if req.content.len() < 2 {
return Json(Error::DataTooShort("content".to_string()).into());
}
if req.content.len() > 150_000 {
return Json(Error::DataTooLong("content".to_string()).into());
}
// check for existing
if data
.query(&SimplifiedQuery {
@ -274,6 +302,16 @@ async fn edit_request(
) -> impl IntoResponse {
let (ref data, _, _) = *data.read().await;
// check content length
if req.content.len() < 2 {
return Json(Error::DataTooShort("content".to_string()).into());
}
if req.content.len() > 150_000 {
return Json(Error::DataTooLong("content".to_string()).into());
}
// ...
let (id, mut entry) = match data
.query(&SimplifiedQuery {
query: AppDataSelectQuery::KeyIs(format!("entries('{}')", slug)),