Skip to content

Commit

Permalink
added single query
Browse files Browse the repository at this point in the history
  • Loading branch information
deeper-x committed Aug 9, 2024
1 parent 45a58e0 commit 85c298e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/db/dml.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::result;

use deadpool_postgres::Client;
use std::time::SystemTime;
use tokio_pg_mapper::FromTokioPostgresRow;
use tokio_postgres::Row;

use crate::{db::models::Migration, db::models::Ping, settings::errors::MyError};

Expand Down Expand Up @@ -38,6 +36,26 @@ pub async fn get_migration_records(client: &Client) -> Result<Vec<Migration>, My
Ok(results)
}

pub async fn get_migration_record(
client: &Client,
id_migration: i64,
) -> Result<Migration, MyError> {
let stmt = client
.prepare("SELECT id, query, ts_created FROM migrations WHERE id = $1;")
.await
.unwrap();

let row: tokio_postgres::Row = client.query_one(&stmt, &[&id_migration]).await?;

let id: i64 = row.get("id");
let query: String = row.get("query");
let ts_created: SystemTime = row.get("ts_created");

let m: Migration = Migration::new(id, query, ts_created);

Ok(m)
}

pub async fn add_migration_record(
client: &Client,
migration: Migration,
Expand Down
12 changes: 11 additions & 1 deletion src/db/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ pub struct Ping {
pub ts_created: SystemTime,
}

#[derive(Deserialize, PostgresMapper, Serialize)]
#[derive(Debug, Deserialize, PostgresMapper, Serialize)]
#[pg_mapper(table = "migrations")]
pub struct Migration {
pub id: Option<i64>,
pub query: String,
pub ts_created: Option<SystemTime>,
}

impl Migration {
pub fn new(id: i64, query: String, ts_created: SystemTime) -> Migration {
Migration {
id: Some(id),
query,
ts_created: Some(ts_created),
}
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ async fn main() -> std::io::Result<()> {
.service(
web::scope("/migration")
.route("/all", web::get().to(server::router::get_migration_records))
.route("/add", web::post().to(server::router::add_migration_record)),
.route("/add", web::post().to(server::router::add_migration_record))
.route(
"/details/{id_migration}",
web::get().to(server::router::get_migration_details),
),
)
})
.bind(config.server_addr.clone())?
Expand Down
28 changes: 28 additions & 0 deletions src/server/router.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::time::SystemTime;

use actix_web::{web, Error, HttpResponse};
use deadpool_postgres::{Client, Pool};

use crate::db::models::Migration;
use crate::db::{dml, models};
use crate::settings;
use crate::settings::errors::MyError;

// retrives ping records
pub async fn get_ping_records(db_pool: web::Data<Pool>) -> Result<HttpResponse, Error> {
Expand All @@ -27,6 +31,30 @@ pub async fn get_migration_records(db_pool: web::Data<Pool>) -> Result<HttpRespo
Ok(HttpResponse::Ok().json(migrations))
}

pub async fn get_migration_details(
db_pool: web::Data<Pool>,
path: web::Path<(i64,)>,
) -> Result<HttpResponse, Error> {
let client = db_pool
.get()
.await
.map_err(settings::errors::MyError::PoolError)?;

let id_migration: (i64,) = path.into_inner();

let migration_data: Result<Migration, MyError> =
dml::get_migration_record(&client, id_migration.0).await;

match migration_data {
Ok(it) => Ok(HttpResponse::Ok().json(it)),

Err(err) => {
println!("{}", err);
Ok(HttpResponse::InternalServerError().json("102"))
}
}
}

pub async fn add_migration_record(
migration: web::Json<models::Migration>,
db_pool: web::Data<Pool>,
Expand Down

0 comments on commit 85c298e

Please sign in to comment.