Initial commit with basic axum and sqlx API

This commit is contained in:
2023-05-07 17:41:45 -04:00
commit c2c0f7a28d
13 changed files with 2355 additions and 0 deletions

19
src/handlers/item.rs Normal file
View 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
View 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
View File

@@ -0,0 +1,2 @@
pub mod item;
pub mod items;