Added return for rowNotFound
This commit is contained in:
parent
805fd182cd
commit
40b7e74088
2 changed files with 36 additions and 28 deletions
|
|
@ -15,14 +15,11 @@ use crate::{
|
|||
storage::DatabaseDriver,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
response::IntoResponse,
|
||||
routing::*,
|
||||
Json, Router,
|
||||
extract::{self, Path, State}, http::StatusCode, response::IntoResponse, routing::*, Json, Router
|
||||
};
|
||||
use chrono::DateTime;
|
||||
use chrono::{DateTime, Utc};
|
||||
use log::{info, error};
|
||||
use sqlx::Row;
|
||||
use sqlx::{error, Row};
|
||||
|
||||
pub fn kv_router(pool: DatabaseDriver) -> Router {
|
||||
Router::new()
|
||||
|
|
@ -79,15 +76,16 @@ async fn get_data(
|
|||
});
|
||||
info!("{:?}", return_secret);
|
||||
|
||||
Ok(Json(return_secret))
|
||||
Ok((StatusCode::OK, Json(return_secret)).into_response())
|
||||
}
|
||||
Err(e) => match e {
|
||||
sqlx::Error::RowNotFound => {
|
||||
error!("{:?}", e);
|
||||
let error_data: HashMap<String, String> = HashMap::from([("error".to_string(), "Secret not found".to_string())]);
|
||||
// let error_secret = KvSecretRes{data: error_data, options: None}; // TODO: Create Error struct instead, consider to create global error struct
|
||||
let error_struct : ErrorStruct = ErrorStruct{err: e.to_string() };
|
||||
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())
|
||||
todo!()
|
||||
},
|
||||
_ => panic!("{:?}", e),
|
||||
},
|
||||
|
|
@ -97,45 +95,41 @@ async fn get_data(
|
|||
async fn post_data(
|
||||
State(pool): State<DatabaseDriver>,
|
||||
Path(path): Path<String>,
|
||||
body: String,
|
||||
extract::Json(payload): extract::Json<KvSecretReq>,
|
||||
) -> &'static str {
|
||||
// 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!(
|
||||
// "Secret: {}, Content: {}, Version: {}, path: {}",
|
||||
// "Secret: {}, Content: {:?}, Version: {:?}, path: {}",
|
||||
// path,
|
||||
// secret.data,
|
||||
// secret.version.unwrap_or(0),
|
||||
// payload.data,
|
||||
// payload.options,
|
||||
// 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";
|
||||
// match sqlx::query!(
|
||||
// "INSERT INTO secret_versions VALUES ($1, $2, $3, $4, $5)",
|
||||
// secret.data,
|
||||
// data,
|
||||
// created_time,
|
||||
// deletion_time,
|
||||
// version,
|
||||
// version
|
||||
// path
|
||||
// )
|
||||
// .execute(&pool)
|
||||
// .await
|
||||
// {
|
||||
// Ok(v) => {
|
||||
// trace!("{:?}", v);
|
||||
// "Success"
|
||||
// info!("{:?}", v);
|
||||
// }
|
||||
// Err(e) => {
|
||||
// trace!("{:?}", e);
|
||||
// "Error"
|
||||
// error!("{:?}", e);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
use axum::{body::Body, http::{Response, StatusCode}, response::IntoResponse, Error};
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::common::HttpError;
|
||||
|
||||
pub type KvSecretData = HashMap<String, String>;
|
||||
|
||||
// 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 }
|
||||
}
|
||||
}
|
||||
|
||||
#[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()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue