summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/db/product.rs2
-rw-r--r--src/handlers/admin.rs11
-rw-r--r--src/handlers/mod.rs7
3 files changed, 18 insertions, 2 deletions
diff --git a/src/db/product.rs b/src/db/product.rs
index 0b90810..e5e797f 100644
--- a/src/db/product.rs
+++ b/src/db/product.rs
@@ -82,7 +82,7 @@ pub struct SpecificProduct<T: ProductMarker> {
pub detail: T,
}
-async fn get_all_products(pool: &SqlitePool) -> Result<Vec<Product>, AppError> {
+pub async fn get_all_products(pool: &SqlitePool) -> Result<Vec<Product>, AppError> {
Ok(sqlx::query_as!(Product, "SELECT * FROM product")
.fetch_all(pool)
.await?)
diff --git a/src/handlers/admin.rs b/src/handlers/admin.rs
index 1cc3068..03c933b 100644
--- a/src/handlers/admin.rs
+++ b/src/handlers/admin.rs
@@ -11,6 +11,7 @@ use axum::{Extension, Json};
use rand::Rng;
use subtle::ConstantTimeEq;
+use crate::db::product::get_all_products;
use crate::db::story::{format_filepath, Story};
use crate::db::ProductDatabaseHandler;
use crate::error::AppError;
@@ -140,3 +141,13 @@ pub async fn delete_story(
}
Ok(StatusCode::BAD_REQUEST.into_response())
}
+
+pub async fn get_stories(ctx: Extension<ApiContext>) -> Result<impl IntoResponse, AppError> {
+ let stories = Story::get_all(&ctx.pool).await?;
+ Ok((StatusCode::OK, Json(stories)).into_response())
+}
+
+pub async fn get_products(ctx: Extension<ApiContext>) -> Result<impl IntoResponse, AppError> {
+ let products = get_all_products(&ctx.pool).await?;
+ Ok((StatusCode::OK, Json(products)).into_response())
+}
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
index 14ac176..bb5473d 100644
--- a/src/handlers/mod.rs
+++ b/src/handlers/mod.rs
@@ -14,6 +14,7 @@ use frontend_builder::home;
use crate::db::story::Story;
+use self::admin::{get_products, get_stories};
use self::frontend_builder::build_page;
mod admin;
@@ -35,7 +36,11 @@ pub async fn router(pool: SqlitePool) -> Router {
};
Router::new()
- .route("/admin/story", post(upload_story).delete(delete_story))
+ .route(
+ "/admin/story",
+ post(upload_story).delete(delete_story).get(get_stories),
+ )
+ .route("/admin/product", get(get_products))
.route("/admin", get(admin_healthcheck))
.layer(middleware::from_fn_with_state(
state.clone(),