60 lines
1.5 KiB
Rust

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<i64>,
last_login: Option<NaiveDateTime>, // TODO use proper timestamp (NaiveDateTime?)
pw: String,
lat: Option<f64>,
lng: Option<f64>,
}
impl Station {
async fn all(db: &SqlitePool) -> Vec<Self> {
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<Self> {
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<Arc<SqlitePool>> {
routes::routes()
}