Initial commit with basic axum and sqlx API
This commit is contained in:
19
src/handlers/item.rs
Normal file
19
src/handlers/item.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use axum::{extract::{State, Path}, Json};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::item::{create_item, get_item, CreateItem, Item};
|
||||
|
||||
pub async fn get(
|
||||
State(pool): State<PgPool>,
|
||||
Path(id): Path<i32>,
|
||||
) -> Result<Json<Item>, AppError> {
|
||||
Ok(Json(get_item(pool, id).await?))
|
||||
}
|
||||
|
||||
pub async fn post(
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<CreateItem>,
|
||||
) -> Result<Json<Item>, AppError> {
|
||||
Ok(Json(create_item(pool, payload).await?))
|
||||
}
|
||||
9
src/handlers/items.rs
Normal file
9
src/handlers/items.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use axum::{extract::State, Json};
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::item::{get_items, Item};
|
||||
|
||||
pub async fn get(State(pool): State<PgPool>) -> Result<Json<Vec<Item>>, AppError> {
|
||||
Ok(Json(get_items(pool).await?))
|
||||
}
|
||||
2
src/handlers/mod.rs
Normal file
2
src/handlers/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod item;
|
||||
pub mod items;
|
||||
Reference in New Issue
Block a user