Updated Typst v0.14.2 and added SQLite database support
This commit is contained in:
+17
-22
@@ -27,7 +27,7 @@ pub async fn list_documents(
|
||||
|
||||
let docs = if let Some(folder_id) = query.folder_id {
|
||||
sqlx::query_as::<_, Document>(
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE owner_id = $1 AND folder_id = $2 ORDER BY updated_at DESC"
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE owner_id = ? AND folder_id = ? ORDER BY updated_at DESC"
|
||||
)
|
||||
.bind(&user_id)
|
||||
.bind(&folder_id)
|
||||
@@ -36,7 +36,7 @@ pub async fn list_documents(
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
} else {
|
||||
sqlx::query_as::<_, Document>(
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE owner_id = $1 AND folder_id IS NULL ORDER BY updated_at DESC"
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE owner_id = ? AND folder_id IS NULL ORDER BY updated_at DESC"
|
||||
)
|
||||
.bind(&user_id)
|
||||
.fetch_all(&state.db)
|
||||
@@ -57,7 +57,7 @@ pub async fn create_document(
|
||||
.ok_or((StatusCode::UNAUTHORIZED, "Not logged in".to_string()))?;
|
||||
|
||||
let doc_id = Uuid::new_v4().to_string();
|
||||
|
||||
|
||||
let content = {
|
||||
let ydoc = Doc::new();
|
||||
let text = ydoc.get_or_insert_text("typst");
|
||||
@@ -70,7 +70,7 @@ pub async fn create_document(
|
||||
};
|
||||
|
||||
let doc = sqlx::query_as::<_, Document>(
|
||||
"INSERT INTO documents (id, owner_id, folder_id, title, content) VALUES ($1, $2, $3, $4, $5) RETURNING id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at"
|
||||
"INSERT INTO documents (id, owner_id, folder_id, title, content) VALUES (?, ?, ?, ?, ?) RETURNING id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at"
|
||||
)
|
||||
.bind(&doc_id)
|
||||
.bind(&user_id)
|
||||
@@ -92,7 +92,7 @@ pub async fn get_document(
|
||||
let user_id_opt = jar.get("session_user_id").map(|c| c.value().to_string());
|
||||
|
||||
let mut doc = sqlx::query_as::<_, Document>(
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE id = $1"
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE id = ?"
|
||||
)
|
||||
.bind(&id)
|
||||
.fetch_optional(&state.db)
|
||||
@@ -105,15 +105,13 @@ pub async fn get_document(
|
||||
if let Some(uid) = &user_id_opt {
|
||||
if &doc.owner_id == uid {
|
||||
effective_role = "owner".to_string();
|
||||
} else {
|
||||
if let Ok(Some((role,))) = sqlx::query_as::<_, (String,)>("SELECT role FROM collaborators WHERE document_id = $1 AND user_id = $2")
|
||||
.bind(&id)
|
||||
.bind(uid)
|
||||
.fetch_optional(&state.db)
|
||||
.await
|
||||
{
|
||||
effective_role = role;
|
||||
}
|
||||
} else if let Ok(Some((role,))) = sqlx::query_as::<_, (String,)>("SELECT role FROM collaborators WHERE document_id = ? AND user_id = ?")
|
||||
.bind(&id)
|
||||
.bind(uid)
|
||||
.fetch_optional(&state.db)
|
||||
.await
|
||||
{
|
||||
effective_role = role;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,9 +140,8 @@ pub async fn update_document(
|
||||
let user_id = jar.get("session_user_id").map(|c| c.value().to_string())
|
||||
.ok_or((StatusCode::UNAUTHORIZED, "Not logged in".to_string()))?;
|
||||
|
||||
|
||||
let mut doc = sqlx::query_as::<_, Document>(
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE id = $1 AND owner_id = $2"
|
||||
"SELECT id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at FROM documents WHERE id = ? AND owner_id = ?"
|
||||
)
|
||||
.bind(&id)
|
||||
.bind(&user_id)
|
||||
@@ -171,9 +168,8 @@ pub async fn update_document(
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let doc = sqlx::query_as::<_, Document>(
|
||||
"UPDATE documents SET title = $1, folder_id = $2, public_role = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4 AND owner_id = $5 RETURNING id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at"
|
||||
"UPDATE documents SET title = ?, folder_id = ?, public_role = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ? AND owner_id = ? RETURNING id, owner_id, folder_id, title, content, thumbnail_svg, public_role, created_at, updated_at"
|
||||
)
|
||||
.bind(&doc.title)
|
||||
.bind(&doc.folder_id)
|
||||
@@ -195,7 +191,7 @@ pub async fn delete_document(
|
||||
let user_id = jar.get("session_user_id").map(|c| c.value().to_string())
|
||||
.ok_or((StatusCode::UNAUTHORIZED, "Not logged in".to_string()))?;
|
||||
|
||||
let result = sqlx::query("DELETE FROM documents WHERE id = $1 AND owner_id = $2")
|
||||
let result = sqlx::query("DELETE FROM documents WHERE id = ? AND owner_id = ?")
|
||||
.bind(&id)
|
||||
.bind(&user_id)
|
||||
.execute(&state.db)
|
||||
@@ -218,8 +214,7 @@ pub async fn upload_file(
|
||||
let user_id = jar.get("session_user_id").map(|c| c.value().to_string())
|
||||
.ok_or((StatusCode::UNAUTHORIZED, "Not logged in".to_string()))?;
|
||||
|
||||
|
||||
let doc_exists = sqlx::query_as::<_, (String, Option<String>)>("SELECT id, folder_id FROM documents WHERE id = $1 AND owner_id = $2")
|
||||
let doc_exists = sqlx::query_as::<_, (String, Option<String>)>("SELECT id, folder_id FROM documents WHERE id = ? AND owner_id = ?")
|
||||
.bind(&doc_id)
|
||||
.bind(&user_id)
|
||||
.fetch_optional(&state.db)
|
||||
@@ -248,7 +243,7 @@ pub async fn upload_file(
|
||||
}
|
||||
}
|
||||
|
||||
sqlx::query("INSERT INTO files (id, owner_id, document_id, folder_id, name, mime_type, data) VALUES ($1, $2, $3, $4, $5, $6, $7)")
|
||||
sqlx::query("INSERT INTO files (id, owner_id, document_id, folder_id, name, mime_type, data) VALUES (?, ?, ?, ?, ?, ?, ?)")
|
||||
.bind(&file_id)
|
||||
.bind(&user_id)
|
||||
.bind(&doc_id)
|
||||
|
||||
Reference in New Issue
Block a user