use axum::Router; use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, SqlitePool}; use std::sync::Arc; mod routes; #[derive(FromRow, Debug, Serialize, Deserialize)] struct Station { id: i64, name: String, amount_people: Option, last_login: Option, // TODO use proper timestamp (NaiveDateTime?) pw: String, lat: Option, lng: Option, } impl Station { async fn all(db: &SqlitePool) -> Vec { sqlx::query_as::<_, Self>( "SELECT id, name, amount_people, last_login, pw, lat, lng FROM station;", ) .fetch_all(db) .await .unwrap() } pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option { sqlx::query_as!( Self, "SELECT id, name, amount_people, last_login, pw, lat, lng FROM station WHERE id like ?", id ) .fetch_one(db) .await .ok() } async fn create(db: &SqlitePool, name: &str) -> Result<(), String> { sqlx::query!("INSERT INTO station(name) VALUES (?)", name) .execute(db) .await .map_err(|e| e.to_string())?; Ok(()) } async fn delete(&self, db: &SqlitePool) { sqlx::query!("DELETE FROM station WHERE id = ?", self.id) .execute(db) .await .unwrap(); } } pub(super) fn routes() -> Router> { routes::routes() }