1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
pub mod block;
pub mod health;
pub mod log;
pub mod transaction;

pub use health::health;

use actix_web::{web, HttpResponse};
use sqlx::{PgPool, Row};

use crate::types::{CountResponse, ErrorResponse};

pub async fn get_count<'a>(
    conn: web::Data<PgPool>,
    table_name: &'a str,
    description: &'a str,
) -> Result<HttpResponse, HttpResponse> {
    let sql_query = format!("SELECT COUNT(*) FROM public.{}", table_name);
    let result = sqlx::query(&sql_query).fetch_one(conn.as_ref()).await;

    match result {
        Ok(row) => match row.try_get::<i64, _>(0) {
            Ok(count) => Ok(HttpResponse::Ok().json(CountResponse { count })),
            Err(_) => {
                eprintln!("Error: Failed to parse count");
                Ok(HttpResponse::InternalServerError().json(ErrorResponse {
                    error: "Failed to parse count".to_string(),
                }))
            }
        },
        Err(err) => {
            eprintln!("Error: {}", err);
            Ok(HttpResponse::InternalServerError().json(ErrorResponse {
                error: description.to_string(),
            }))
        }
    }
}