diff --git a/migrations/20240506135416_testdata.sql b/migrations/20240506135416_testdata.sql new file mode 100644 index 0000000..1eb81bf --- /dev/null +++ b/migrations/20240506135416_testdata.sql @@ -0,0 +1,5 @@ +-- Add migration script here + +INSERT INTO metadata VALUES ("bar", false, DateTime('now'), "123", 4, DateTime('now'), "customData"); + +INSERT INTO secret_versions VALUES ("secret_data", DateTime('now'), DateTime('now'), 1, "bar"); \ No newline at end of file diff --git a/src/engines/kv.rs b/src/engines/kv.rs index 91bae58..fb2aef2 100644 --- a/src/engines/kv.rs +++ b/src/engines/kv.rs @@ -7,11 +7,20 @@ pub mod structs; #[cfg(test)] mod tests; -use crate::engines::kv::structs::KvSecret; -use crate::{engines::kv::logic::body_to_json, storage::DatabaseDriver}; -use axum::extract::State; -use axum::Extension; -use axum::{extract::Path, routing::*, Router}; +use crate::{ + engines::kv::{ + logic::body_to_json, + structs::{KvSecret, SecretMeta}, + }, + storage::DatabaseDriver, +}; +use axum::{ + extract::{Path, State}, + routing::*, + Router, +}; +use log::{info, trace}; +use sqlx::{error::UnexpectedNullError, Row}; pub fn kv_router(pool: DatabaseDriver) -> Router { Router::new() @@ -40,15 +49,50 @@ async fn post_config() -> &'static str { todo!("not implemented") } -async fn get_data() -> &'static str { - todo!("not implemented") +async fn get_data(State(pool): State, Path(path): Path) -> &'static str { + // SQL Anfrage gegen Path + match sqlx::query("SELECT * FROM secret_versions WHERE secret_path = $1") + .bind(path) + .fetch_one(&pool) + .await + { + Ok(v) => { + let secret_data: String = v.get("secret_data"); + let created_time: String = v.get("created_time"); + let deletion_time: String = v.get("deletion_time"); + let version_number: i64 = v.get("version_number"); + let secret_path: String = v.get("secret_path"); + info!( + "{:?}", + ( + secret_data, + created_time, + deletion_time, + version_number, + secret_path + ) + ); + // Handle returned data + return "Success"; + } + Err(e) => { + match e { + sqlx::Error::RowNotFound => trace!("{:?}", e), + _ => panic!("{:?}", e), + } + info!("{:?}", e); + return "Error"; + } + } } async fn post_data( - Path(kv_path): Path, - Extension(mount_path): Extension, + State(pool): State, + Path(path): Path, body: String, ) -> &'static str { + // Insert Metadata first -> Else: Error because of foreign key constraint + let mut body_json = body_to_json(body); let secret: KvSecret = KvSecret { @@ -57,12 +101,35 @@ async fn post_data( }; log::debug!( "Secret: {}, Content: {}, Version: {}, path: {}", - kv_path, + path, secret.data, secret.version.unwrap_or(0), - mount_path, + path ); - "RoutingTest foo successful" + + let created_time = "05-03-2024 12:00:00"; + let deletion_time = "05-03-2024 12:00:00"; + let version = "0"; + match sqlx::query!( + "INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)", + secret.data, + created_time, + deletion_time, + version, + version + ) + .execute(&pool) + .await + { + Ok(v) => { + trace!("{:?}", v); + "Success" + } + Err(e) => { + trace!("{:?}", e); + "Error" + } + } // todo!("not implemented") } @@ -86,7 +153,13 @@ async fn get_meta() -> &'static str { todo!("not implemented") } -async fn post_meta() -> &'static str { +async fn post_meta( + State(pool): State, + Path((mount_path, kv_path)): Path<(String, String)>, + body: String, +) -> &'static str { + let mut body_json = body_to_json(body); + let meta_data: SecretMeta = Default::default(); todo!("not implemented") }