-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dce1fc
commit 9769337
Showing
6 changed files
with
256 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,79 @@ | ||
use diesel::r2d2::{self, ConnectionManager}; | ||
use diesel::pg::PgConnection; | ||
use std::env; | ||
|
||
pub(crate) type DbPool = r2d2::Pool<ConnectionManager<PgConnection>>; | ||
|
||
pub fn establish_connection_pool() -> DbPool { | ||
dotenv::dotenv().ok(); | ||
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); | ||
let manager = ConnectionManager::<PgConnection>::new(database_url); | ||
r2d2::Pool::builder() | ||
.build(manager) | ||
.expect("Failed to create pool.") | ||
use tokio_postgres::{NoTls, Error}; | ||
use crate::config::Config; | ||
use std::sync::Arc; | ||
|
||
async fn create_tables(config: Arc<Config>) -> Result<(), Error> { | ||
let connection_string = format!( | ||
"host={} port={} user={} password={} dbname={}", | ||
config.db_host, | ||
config.db_port, | ||
config.db_user, | ||
config.db_password, | ||
config.db_name, | ||
); | ||
|
||
let (client, connection) = tokio_postgres::connect(&connection_string, NoTls).await?; | ||
|
||
tokio::spawn(async move { | ||
if let Err(e) = connection.await { | ||
eprintln!("connection error: {}", e); | ||
} | ||
}); | ||
|
||
client.execute( | ||
" | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY, | ||
user_data JSON NOT NULL, | ||
chat_data JSON NOT NULL, | ||
epoch_time INTEGER NOT NULL | ||
) | ||
", | ||
&[], | ||
).await?; | ||
|
||
client.execute( | ||
" | ||
CREATE TABLE IF NOT EXISTS requests ( | ||
id SERIAL PRIMARY KEY, | ||
user_id INTEGER REFERENCES users(id), | ||
command TEXT NOT NULL, | ||
response TEXT NOT NULL, | ||
request_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP | ||
) | ||
", | ||
&[], | ||
).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn create_user(client: &tokio_postgres::Client, user_data: &str, chat_data: &str, epoch_time: i32) -> Result<u64, Error> { | ||
let stmt = " | ||
INSERT INTO users (user_data, chat_data, epoch_time) | ||
VALUES ($1, $2, $3) | ||
"; | ||
let rows_affected = client.execute(stmt, &[&user_data, &chat_data, &epoch_time]).await?; | ||
Ok(rows_affected) | ||
} | ||
|
||
async fn create_request(client: &tokio_postgres::Client, user_id: i32, command: &str, response: &str) -> Result<u64, Error> { | ||
let stmt = " | ||
INSERT INTO requests (user_id, command, response) | ||
VALUES ($1, $2, $3) | ||
"; | ||
let rows_affected = client.execute(stmt, &[&user_id, &command, &response]).await?; | ||
Ok(rows_affected) | ||
} | ||
|
||
async fn find_user_by_id(client: &tokio_postgres::Client, user_id: &str) -> Result<Option<i32>, Error> { | ||
let stmt = " | ||
SELECT id FROM users WHERE user_data->>'id' = $1 | ||
"; | ||
let row = client.query_opt(stmt, &[&user_id]).await?; | ||
if let Some(row) = row { | ||
Ok(Some(row.get(0))) | ||
} else { | ||
Ok(None) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.