- Luotu todo-rs.md golden example Rust-referenssitoteutuksesta
- getGoldenForModel() huomioi nyt LANG: todo.md → todo-rs.md Rust-moodissa
- Korjattu golden-compact-rs.md /:id → /{id} bugi
- Juurisyy: malli sai Python golden examplen mutta piti generoida Rustia
44 lines
1.8 KiB
Markdown
44 lines
1.8 KiB
Markdown
REFERENCE PATTERNS (follow exactly):
|
|
|
|
STACK: Axum 0.8 + SQLx + SQLite + Tokio + Serde
|
|
|
|
Cargo.toml:
|
|
edition = "2024"
|
|
deps: axum 0.8, tokio (full), serde (derive), serde_json, sqlx (sqlite, runtime-tokio), tower-http (cors)
|
|
dev: reqwest (rustls-tls)
|
|
|
|
src/models.rs:
|
|
#[derive(Debug, Serialize, Deserialize, FromRow)]
|
|
struct Entity { id: i64, field: String, optional: Option<String> }
|
|
struct CreateEntity { field: String, optional: Option<String> }
|
|
Status fields: String with default "pending"
|
|
|
|
src/handlers.rs:
|
|
async fn create(State(pool), Json(input)) -> (StatusCode, Json<Entity>)
|
|
POST → StatusCode::CREATED, sqlx::query("INSERT...").execute + query_as last_insert_rowid
|
|
GET list → query_as("SELECT * FROM table").fetch_all
|
|
GET by id → query_as.fetch_optional, return 404 if None
|
|
PUT → query("UPDATE...SET...WHERE id=?"), rows_affected == 0 → 404
|
|
DELETE → StatusCode::NO_CONTENT, rows_affected == 0 → 404
|
|
|
|
src/lib.rs:
|
|
pub fn app(pool: SqlitePool) -> Router
|
|
pub async fn init_db(pool: &SqlitePool) → CREATE TABLE IF NOT EXISTS
|
|
Routes: .route("/{table}", post(create).get(list))
|
|
.route("/{table}/{id}", get(get_one).put(update).delete(delete_one))
|
|
|
|
src/main.rs:
|
|
SqlitePool::connect("sqlite:./app.db"), init_db, bind 0.0.0.0:3000
|
|
|
|
tests/api_test.rs:
|
|
Each test: SqlitePool::connect("sqlite::memory:"), init_db, app(pool)
|
|
Spawn on random port: TcpListener::bind("127.0.0.1:0"), axum::serve
|
|
reqwest::Client for HTTP calls
|
|
Unique Finnish data ("Osta maitoa", "Haettava tehtävä"...)
|
|
test_create → 201 + assert id exists
|
|
test_list → post first, get, assert len >= 1
|
|
test_get_by_id → post, get, assert id matches
|
|
test_not_found → 404
|
|
test_update → post, put with ALL fields, assert 200
|
|
test_delete → post, delete 204, get → 404
|