use axum::http::StatusCode; use sqlx::PgPool; use uuid::Uuid; pub async fn init_db(url: String) -> Result { let database_url = format!("postgres://chatapp:secret@{url}/chatapp"); PgPool::connect(database_url.as_str()).await } pub async fn user_id_from_uuid(db: &PgPool, user_uuid: Uuid) -> Result { sqlx::query_scalar("SELECT id FROM user_ WHERE uuid = $1") .bind(user_uuid) .fetch_one(db) .await .map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token"))) } pub async fn room_id_from_uuid(db: &PgPool, room_uuid: Uuid) -> Result { sqlx::query_scalar("SELECT id FROM room_ WHERE uuid = $1") .bind(room_uuid) .fetch_one(db) .await .map_err(|_| (StatusCode::NOT_FOUND, "Failed to find room".into())) } pub async fn username_from_uuid( db: &PgPool, user_uuid: Uuid, ) -> Result { sqlx::query_scalar("SELECT username FROM user_ WHERE uuid = $1") .bind(user_uuid) .fetch_one(db) .await .map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token"))) } pub async fn username_from_id(db: &PgPool, user_id: i32) -> Result { sqlx::query_scalar("SELECT username FROM user_ WHERE id = $1") .bind(user_id) .fetch_one(db) .await .map_err(|_| (StatusCode::UNAUTHORIZED, String::from("Wrong token"))) } pub async fn id_from_username(db: &PgPool, username: String) -> Result { sqlx::query_scalar("SELECT id FROM user_ WHERE username = $1") .bind(username) .fetch_one(db) .await .map_err(|_| (StatusCode::NOT_FOUND, "User not found".into())) } pub async fn room_name_from_uuid( db: &PgPool, room_uuid: Uuid, ) -> Result { sqlx::query_scalar("SELECT name FROM room_ WHERE uuid = $1") .bind(room_uuid) .fetch_one(db) .await .map_err(|_| (StatusCode::NOT_FOUND, "Failed to find room".into())) }