Skip to content

Commit

Permalink
add post endpoint to insert documents
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Oct 5, 2023
1 parent a16045a commit d548143
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 30 deletions.
6 changes: 6 additions & 0 deletions http-server/src/db/collections/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ pub async fn find(state: web::Data<Mutex<Client>>) -> Result<Vec<Log>> {
}
Ok(ret)
}

pub async fn insert(state: web::Data<Mutex<Client>>, log: Log) -> Result<()> {
let log_collection: Collection<Log> = get_collection(state, COLLECTION_NAME).await?;
log_collection.insert_one(log, None).await?;
Ok(())
}
6 changes: 6 additions & 0 deletions http-server/src/db/collections/notary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ pub async fn find(state: web::Data<Mutex<Client>>) -> Result<Vec<Notary>> {
}
Ok(ret)
}

pub async fn insert(state: web::Data<Mutex<Client>>, notary: Notary) -> Result<()> {
let notary_collection: Collection<Notary> = get_collection(state, COLLECTION_NAME).await?;
notary_collection.insert_one(notary, None).await?;
Ok(())
}
6 changes: 6 additions & 0 deletions http-server/src/db/collections/rkh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ pub async fn find(state: web::Data<Mutex<Client>>) -> Result<Vec<RootKeyHolder>>
}
Ok(ret)
}

pub async fn insert(state: web::Data<Mutex<Client>>, rkh: RootKeyHolder) -> Result<()> {
let rkh_collection: Collection<RootKeyHolder> = get_collection(state, COLLECTION_NAME).await?;
rkh_collection.insert_one(rkh, None).await?;
Ok(())
}
28 changes: 18 additions & 10 deletions http-server/src/router/logs.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use crate::db;
use actix_web::{get, http::header::ContentType, web, HttpResponse};
use actix_web::{get, http::header::ContentType, web, HttpResponse, post};
use mongodb::Client;
use std::sync::Mutex;

#[get("/logs")]
pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
let items = match db::collections::logs::find(db_connection).await {
Ok(items) => items,
Err(_) => {
return HttpResponse::InternalServerError().finish();
}
};
return HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&items).unwrap());
match db::collections::logs::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}

#[post("/logs")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::logs::Log>,
) -> HttpResponse {
match db::collections::logs::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}
28 changes: 18 additions & 10 deletions http-server/src/router/notary.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use crate::db;
use actix_web::{get, http::header::ContentType, web, HttpResponse};
use actix_web::{get, http::header::ContentType, web, HttpResponse, post};
use mongodb::Client;
use std::sync::Mutex;

#[get("/notary")]
pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
let items = match db::collections::notary::find(db_connection).await {
Ok(items) => items,
Err(_) => {
return HttpResponse::InternalServerError().finish();
}
};
return HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&items).unwrap());
match db::collections::notary::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}

#[post("/notary")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::notary::Notary>,
) -> HttpResponse {
match db::collections::notary::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}
28 changes: 18 additions & 10 deletions http-server/src/router/rkh.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use crate::db;
use actix_web::{get, http::header::ContentType, web, HttpResponse};
use actix_web::{get, http::header::ContentType, web, HttpResponse, post};
use mongodb::Client;
use std::sync::Mutex;

#[get("/rkh")]
pub async fn get(db_connection: web::Data<Mutex<Client>>) -> HttpResponse {
let items = match db::collections::rkh::find(db_connection).await {
Ok(items) => items,
Err(_) => {
return HttpResponse::InternalServerError().finish();
}
};
return HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&items).unwrap());
match db::collections::rkh::find(db_connection).await {
Ok(i) => HttpResponse::Ok()
.content_type(ContentType::json())
.body(serde_json::to_string(&i).unwrap()),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}

#[post("/rkh")]
pub async fn post(
db_connection: web::Data<Mutex<Client>>,
rkh: web::Json<db::collections::rkh::RootKeyHolder>,
) -> HttpResponse {
match db::collections::rkh::insert(db_connection, rkh.into_inner()).await {
Ok(_) => HttpResponse::Ok().finish(),
Err(_) => HttpResponse::InternalServerError().finish(),
}
}

0 comments on commit d548143

Please sign in to comment.