diff --git a/src/handlers/blog.rs b/src/handlers/blog.rs index 83eedd8..18ab762 100644 --- a/src/handlers/blog.rs +++ b/src/handlers/blog.rs @@ -13,6 +13,10 @@ pub fn config(cfg: &mut web::ServiceConfig) { .route(web::get().to(list_posts)) .route(web::post().to(create_post)), ) + .service( + web::resource("/posts/me") + .route(web::get().to(list_my_posts)), + ) .service( web::resource("/posts/{id}") .route(web::get().to(get_post)) @@ -47,6 +51,18 @@ async fn list_posts( Ok(HttpResponse::Ok().json(response)) } +async fn list_my_posts( + pool: web::Data, + user: AuthenticatedUser, +) -> Result { + // Get all posts by this user (including drafts) + let posts = PostRepository::find_by_author(&pool, &user.user_id).await?; + + let response: Vec = posts.into_iter().map(PostResponse::from).collect(); + + Ok(HttpResponse::Ok().json(response)) +} + async fn get_post( pool: web::Data, path: web::Path, diff --git a/src/repository/post_repository.rs b/src/repository/post_repository.rs index 53a6119..aed8cc9 100644 --- a/src/repository/post_repository.rs +++ b/src/repository/post_repository.rs @@ -46,6 +46,20 @@ impl PostRepository { .map_err(AppError::from) } + pub async fn find_by_author(pool: &SqlitePool, author_id: &str) -> Result, 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( pool: &SqlitePool, limit: i32,