feat: migrate to diesel and new error type in stats

This commit is contained in:
Radical 2025-05-23 12:57:19 +02:00
parent 49e08af3d9
commit a670b32c86

View file

@ -1,31 +1,31 @@
use std::time::SystemTime; use std::time::SystemTime;
use actix_web::{HttpResponse, Responder, get, web}; use actix_web::{HttpResponse, get, web};
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use serde::Serialize; use serde::Serialize;
use crate::error::Error;
use crate::Data; use crate::Data;
use crate::schema::users::dsl::{users, uuid};
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
#[derive(Serialize)] #[derive(Serialize)]
struct Response { struct Response {
accounts: usize, accounts: i64,
uptime: u64, uptime: u64,
version: String, version: String,
build_number: String, build_number: String,
} }
#[get("/stats")] #[get("/stats")]
pub async fn res(data: web::Data<Data>) -> impl Responder { pub async fn res(data: web::Data<Data>) -> Result<HttpResponse, Error> {
let accounts; let accounts: i64 = users
if let Ok(users) = sqlx::query("SELECT uuid FROM users") .select(uuid)
.fetch_all(&data.pool) .count()
.await .get_result(&mut data.pool.get().await?)
{ .await?;
accounts = users.len();
} else {
return HttpResponse::InternalServerError().finish();
}
let response = Response { let response = Response {
// TODO: Get number of accounts from db // TODO: Get number of accounts from db
@ -39,5 +39,5 @@ pub async fn res(data: web::Data<Data>) -> impl Responder {
build_number: String::from("how do i implement this?"), build_number: String::from("how do i implement this?"),
}; };
HttpResponse::Ok().json(response) Ok(HttpResponse::Ok().json(response))
} }