feature: add draft func

This commit is contained in:
dela
2026-02-12 00:27:48 +08:00
parent 604f323c34
commit b746030d51
2 changed files with 30 additions and 0 deletions

View File

@@ -13,6 +13,10 @@ pub fn config(cfg: &mut web::ServiceConfig) {
.route(web::get().to(list_posts)) .route(web::get().to(list_posts))
.route(web::post().to(create_post)), .route(web::post().to(create_post)),
) )
.service(
web::resource("/posts/me")
.route(web::get().to(list_my_posts)),
)
.service( .service(
web::resource("/posts/{id}") web::resource("/posts/{id}")
.route(web::get().to(get_post)) .route(web::get().to(get_post))
@@ -47,6 +51,18 @@ async fn list_posts(
Ok(HttpResponse::Ok().json(response)) Ok(HttpResponse::Ok().json(response))
} }
async fn list_my_posts(
pool: web::Data<SqlitePool>,
user: AuthenticatedUser,
) -> Result<HttpResponse, AppError> {
// Get all posts by this user (including drafts)
let posts = PostRepository::find_by_author(&pool, &user.user_id).await?;
let response: Vec<PostResponse> = posts.into_iter().map(PostResponse::from).collect();
Ok(HttpResponse::Ok().json(response))
}
async fn get_post( async fn get_post(
pool: web::Data<SqlitePool>, pool: web::Data<SqlitePool>,
path: web::Path<String>, path: web::Path<String>,

View File

@@ -46,6 +46,20 @@ impl PostRepository {
.map_err(AppError::from) .map_err(AppError::from)
} }
pub async fn find_by_author(pool: &SqlitePool, author_id: &str) -> Result<Vec<Post>, AppError> {
sqlx::query_as::<_, Post>(
r#"
SELECT * FROM posts
WHERE author_id = ? AND deleted_at IS NULL
ORDER BY created_at DESC
"#,
)
.bind(author_id)
.fetch_all(pool)
.await
.map_err(AppError::from)
}
pub async fn find_all( pub async fn find_all(
pool: &SqlitePool, pool: &SqlitePool,
limit: i32, limit: i32,