Skip to content

Commit

Permalink
[bugfix] DB connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rauner committed Feb 19, 2024
1 parent edb53ed commit ce1e22c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 140 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ edition = "2021"
log = "*"
env_logger = "*"
dotenv = "0.15"
sqlx = { version = "*", features = ["runtime-tokio-rustls", "sqlite"] }
tokio = { version = "*", features = ["full"] }
axum = "*"
reqwest = { version = "*", features = ["json", "multipart"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio-postgres = "0.7.10"
urlencoding = "2.1.3"
sqlx = { version = "0.7.3", features = ["runtime-tokio-rustls", "sqlite", "mysql"] }

[dev-dependencies]
tokio-test = "0.4"
Expand Down
98 changes: 0 additions & 98 deletions data/prompt.txt

This file was deleted.

64 changes: 25 additions & 39 deletions src/assistant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::path::Path;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use urlencoding::encode;
use tokio_postgres::NoTls;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Expand All @@ -18,7 +16,10 @@ use serde::{Deserialize, Serialize};
use serde_json::json;
use std::{env, time::Duration};

use sqlx::sqlite::{SqliteConnectOptions, SqlitePool};
use sqlx::{sqlite::{SqliteConnectOptions, SqlitePool}, FromRow};
use sqlx::Pool;
use sqlx::{mysql::MySqlPoolOptions, MySql};


// Define a custom error type that can be converted into an HTTP response.
#[derive(Debug)]
Expand Down Expand Up @@ -134,19 +135,19 @@ pub struct Ressources {
instructions: String,
}
#[derive(Serialize)]
#[derive(FromRow)] // Derive the FromRow trait
struct Bike {
slug: String,
category: String,
motor: String,
color: String,
frame_size_code: String,
rider_height_min: i32,
rider_height_max: i32,
motor: String,
price: i32,
color: String,
rider_height_max: i32,
rider_height_min: i32,
slug: String,
}
impl Ressources {
pub async fn bikes_db(&self) -> Result<(), AssistantError> {
// Retrieve database credentials from environment variables
let host = env::var("DB_HOST")
.map_err(|_| AssistantError::DatabaseError("DB_HOST environment variable not set".to_string()))?;
let port = env::var("DB_PORT")
Expand All @@ -157,21 +158,17 @@ impl Ressources {
.map_err(|_| AssistantError::DatabaseError("DB_USER environment variable not set".to_string()))?;
let password = env::var("DB_PASSWORD")
.map_err(|_| AssistantError::DatabaseError("DB_PASSWORD environment variable not set".to_string()))?;
let encoded_password = encode(&password);

// Create the database URL for MySQL
let database_url = format!("mysql://{}:{}@{}:{}/{}", user, password, host, port, dbname);
let (client, connection) =
tokio_postgres::connect(&database_url, NoTls).await

// Create a connection pool for MySQL
let pool: Pool<MySql> = MySqlPoolOptions::new()
.connect(&database_url).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
// The connection object performs the actual communication with the database,
// so spawn it off to run on its own.
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});

// Define the query
let main_query = r#"
let main_query = "
SELECT bikes.slug as slug,
bike_categories.slug as category,
motor,
Expand All @@ -184,35 +181,24 @@ impl Ressources {
JOIN bike_additional_infos ON bikes.id = bike_additional_infos.bike_id
JOIN bike_categories ON bikes.bike_category_id = bike_categories.id
WHERE bikes.status = 'active'
"#;
// Execute the query
let stmt = client.prepare(main_query).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
let rows = client.query(&stmt, &[]).await
";

// Execute the query using sqlx
let bikes: Vec<Bike> = sqlx::query_as(main_query)
.fetch_all(&pool).await
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
// Convert the tokio_postgres::Row into your serializable struct.
let bikes: Vec<Bike> = rows
.iter()
.map(|row| Bike {
slug: row.get("slug"),
category: row.get("category"),
motor: row.get("motor"),
frame_size_code: row.get("frame_size_code"),
rider_height_min: row.get("rider_height_min"),
rider_height_max: row.get("rider_height_max"),
price: row.get("price"),
color: row.get("color"),
})
.collect();

// Serialize the bikes to JSON
let bikes_json_string = serde_json::to_string_pretty(&bikes)
.map_err(|e| AssistantError::OpenAIError(e.to_string()))?;

// Write the JSON data to a file in the specified folder_path
let file_path = PathBuf::from(&self.folder_path).join("bikes.json");
let mut file = File::create(file_path)
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;
file.write_all(bikes_json_string.as_bytes())
.map_err(|e| AssistantError::DatabaseError(e.to_string()))?;

Ok(())
}
pub async fn scrape_context(&self) -> Result<(), AssistantError> {
Expand Down

0 comments on commit ce1e22c

Please sign in to comment.