use actix_web::{HttpResponse, Responder, get, web::Data};
use serde::Serialize;
use sqlx::FromRow;

use crate::AppState;

#[derive(Serialize, FromRow)]
struct Sample {
    sampleid: i32,
    samplename: String,
}

#[get("/sample")]
pub async fn sample(app_state: Data<AppState>) -> impl Responder {
    let query =
        sqlx::query_as::<_, Sample>("SELECT * FROM sample")
            .fetch_all(&app_state.pool)
            .await;

    match query {
        Ok(result) => HttpResponse::Ok().json(result),
        Err(_) => HttpResponse::BadRequest().into(),
    }
}
