Added return for rowNotFound

This commit is contained in:
someone 2024-05-11 20:49:30 +02:00
parent 805fd182cd
commit 40b7e74088
2 changed files with 36 additions and 28 deletions

View file

@ -15,14 +15,11 @@ use crate::{
storage::DatabaseDriver, storage::DatabaseDriver,
}; };
use axum::{ use axum::{
extract::{Path, State}, extract::{self, Path, State}, http::StatusCode, response::IntoResponse, routing::*, Json, Router
response::IntoResponse,
routing::*,
Json, Router,
}; };
use chrono::DateTime; use chrono::{DateTime, Utc};
use log::{info, error}; use log::{info, error};
use sqlx::Row; use sqlx::{error, Row};
pub fn kv_router(pool: DatabaseDriver) -> Router { pub fn kv_router(pool: DatabaseDriver) -> Router {
Router::new() Router::new()
@ -79,15 +76,16 @@ async fn get_data(
}); });
info!("{:?}", return_secret); info!("{:?}", return_secret);
Ok(Json(return_secret)) Ok((StatusCode::OK, Json(return_secret)).into_response())
} }
Err(e) => match e { Err(e) => match e {
sqlx::Error::RowNotFound => { sqlx::Error::RowNotFound => {
error!("{:?}", e); error!("{:?}", e);
let error_data: HashMap<String, String> = HashMap::from([("error".to_string(), "Secret not found".to_string())]); let error_struct : ErrorStruct = ErrorStruct{err: e.to_string() };
// let error_secret = KvSecretRes{data: error_data, options: None}; // TODO: Create Error struct instead, consider to create global error struct error!("{:?}", error_struct.err);
Ok(error_struct.into_response()) // TODO: API doesn't specify return value in case of error. Error struct correct? Else send empty secret back?
// let error_secret = KvSecretRes{data: None, options: None};
// Ok(Json()) // Ok(Json())
todo!()
}, },
_ => panic!("{:?}", e), _ => panic!("{:?}", e),
}, },
@ -97,45 +95,41 @@ async fn get_data(
async fn post_data( async fn post_data(
State(pool): State<DatabaseDriver>, State(pool): State<DatabaseDriver>,
Path(path): Path<String>, Path(path): Path<String>,
body: String, extract::Json(payload): extract::Json<KvSecretReq>,
) -> &'static str { ) -> &'static str {
// Insert Metadata first -> Else: Error because of foreign key constraint // Insert Metadata first -> Else: Error because of foreign key constraint
// let mut body_json = body_to_json(body);
// let secret: KvSecret = KvSecret {
// data: body_json["data"]["password1"].take().to_string(),
// version: body_json["data"]["version"].take().as_i64(),
// };
// log::debug!( // log::debug!(
// "Secret: {}, Content: {}, Version: {}, path: {}", // "Secret: {}, Content: {:?}, Version: {:?}, path: {}",
// path, // path,
// secret.data, // payload.data,
// secret.version.unwrap_or(0), // payload.options,
// path // path
// ); // );
// let created_time = "05-03-2024 12:00:00";
// let deletion_time = "05-03-2024 12:00:00"; // let data = payload.data.get("data");
// log::debug!("{:?}", data);
// let created_time = Utc::now();
// let deletion_time = "05-03-2024 12:00:00"; // TODO:
// let version = "0"; // let version = "0";
// match sqlx::query!( // match sqlx::query!(
// "INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)", // "INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)",
// secret.data, // data,
// created_time, // created_time,
// deletion_time, // deletion_time,
// version, // version,
// version // path
// ) // )
// .execute(&pool) // .execute(&pool)
// .await // .await
// { // {
// Ok(v) => { // Ok(v) => {
// trace!("{:?}", v); // info!("{:?}", v);
// "Success"
// } // }
// Err(e) => { // Err(e) => {
// trace!("{:?}", e); // error!("{:?}", e);
// "Error"
// } // }
// } // }

View file

@ -1,7 +1,10 @@
use axum::{body::Body, http::{Response, StatusCode}, response::IntoResponse, Error};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap; use std::collections::HashMap;
use crate::common::HttpError;
pub type KvSecretData = HashMap<String, String>; pub type KvSecretData = HashMap<String, String>;
// This file contains structures for serializing HTTP Responses (Res) and deserializing Requests (Req) for the KV engine // This file contains structures for serializing HTTP Responses (Res) and deserializing Requests (Req) for the KV engine
@ -42,3 +45,14 @@ impl KvSecretRes {
KvSecretRes { data } KvSecretRes { data }
} }
} }
#[derive(Serialize)]
pub struct ErrorStruct {
pub err: String
}
impl ErrorStruct {
pub fn into_response(self) -> Response<Body> {
let body = self.err;
(StatusCode::NOT_FOUND, body).into_response()
}
}