-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a32c3c5
commit 823f3d7
Showing
29 changed files
with
689 additions
and
444 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,59 @@ | ||
use crate::{ | ||
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME}, | ||
types::other::voyager_error::VoyagerError, | ||
utils::{runtime_helpers::RuntimeSpawnHandled, Error}, | ||
}; | ||
use axum::http::StatusCode; | ||
use mongodb::bson::doc; | ||
use tracing::{event, Level}; | ||
|
||
pub async fn delete(name: String) -> Option<()> { | ||
pub async fn delete(name: String) -> Result<(), VoyagerError> { | ||
event!( | ||
Level::DEBUG, | ||
"Deleting deployment of name {name} from database." | ||
); | ||
|
||
let future = async move { | ||
let result = DB_CONTEXT | ||
.deployments | ||
.delete_one(doc! {"name": name}, None) | ||
.await; | ||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled( | ||
"repositories::deployments::delete", | ||
DB_CONTEXT | ||
.deployments | ||
.delete_one(doc! {"name": &name}, None), | ||
) | ||
.await?; | ||
|
||
result.map_err(Error::from) // MongoDB Error | ||
}; | ||
result.map_or_else( | ||
|e| Err(VoyagerError::delete_mongo(Box::new(e), &name)), | ||
|r| { | ||
if r.deleted_count == 0 { | ||
Err(VoyagerError::delete(&name)) | ||
} else { | ||
Ok(()) | ||
} | ||
}, | ||
) | ||
} | ||
|
||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled("repositories::deployments::delete", future) | ||
.await; | ||
impl VoyagerError { | ||
pub fn delete_mongo(e: Error, name: &str) -> Self { | ||
let message = format!("Failed to delete deployment named '{name}'! Error:{e}"); | ||
|
||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::INTERNAL_SERVER_ERROR, | ||
source: Some(e), | ||
} | ||
} | ||
|
||
pub fn delete(name: &str) -> Self { | ||
let message = format!("Failed to find deployment named '{name}'!"); | ||
|
||
result | ||
.map(|r| { | ||
r.map_or_else( | ||
|e| { | ||
event!(Level::ERROR, "Failed to retrieve deployments: {}", e); | ||
false | ||
}, | ||
|d| d.deleted_count > 0, | ||
) | ||
}) | ||
.map(|r| if r { Some(()) } else { None }) | ||
.and_then(|r| r) | ||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::NOT_FOUND, | ||
source: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,50 @@ | ||
use crate::{ | ||
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME}, | ||
types::model::deployment::Deployment, | ||
utils::runtime_helpers::RuntimeSpawnHandled, | ||
utils::Error, | ||
types::{model::deployment::Deployment, other::voyager_error::VoyagerError}, | ||
utils::{runtime_helpers::RuntimeSpawnHandled, Error}, | ||
}; | ||
use axum::http::StatusCode; | ||
use mongodb::bson::doc; | ||
use tracing::{event, Level}; | ||
|
||
pub async fn find_by_host(host: String) -> Option<Deployment> { | ||
pub async fn find_by_host(host: String) -> Result<Deployment, VoyagerError> { | ||
event!(Level::DEBUG, "Finding deployment by host {}", &host); | ||
|
||
let host_clone = host.clone(); | ||
let future = async move { | ||
let result = DB_CONTEXT | ||
.deployments | ||
.find_one(doc! { "host": &host }, None) | ||
.await; | ||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled( | ||
"repositories::deployments::find_by_host", | ||
DB_CONTEXT | ||
.deployments | ||
.find_one(doc! { "host": &host }, None), | ||
) | ||
.await?; | ||
|
||
result | ||
.map_err(Error::from) // MongoDB Error | ||
.map(|d| d.ok_or_else(|| Error::from("Deployment not found"))) // 'None' Error | ||
.and_then(|inner| inner) // Flatten | ||
}; | ||
result.map_or_else( | ||
|e| Err(VoyagerError::find_mongo_host(Box::new(e), &host)), | ||
|r| r.ok_or_else(|| VoyagerError::find_null_host(&host)), | ||
) | ||
} | ||
|
||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled("repositories::deployments::find_by_host", future) | ||
.await; | ||
impl VoyagerError { | ||
pub fn find_mongo_host(e: Error, host: &str) -> Self { | ||
let message = format!("Failed to find deployment by host '{host}'! Error:{e}"); | ||
|
||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::INTERNAL_SERVER_ERROR, | ||
source: Some(e), | ||
} | ||
} | ||
|
||
pub fn find_null_host(host: &str) -> Self { | ||
let message = format!("Failed to find deployment by host '{host}'!"); | ||
|
||
result | ||
.map(|r| { | ||
r.map_or_else( | ||
|e| { | ||
event!( | ||
Level::ERROR, | ||
"Failed to find deployment with host {}: {}", | ||
host_clone, | ||
e | ||
); | ||
None | ||
}, | ||
Some, | ||
) | ||
}) | ||
.and_then(|d| d) | ||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::NOT_FOUND, | ||
source: None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
use crate::{ | ||
business::repositories::{DB_CONTEXT, REPOSITORIES_RUNTIME}, | ||
types::model::deployment::Deployment, | ||
utils::runtime_helpers::RuntimeSpawnHandled, | ||
utils::Error, | ||
types::{model::deployment::Deployment, other::voyager_error::VoyagerError}, | ||
utils::{runtime_helpers::RuntimeSpawnHandled, Error}, | ||
}; | ||
use axum::http::StatusCode; | ||
use mongodb::bson::doc; | ||
use tracing::{event, Level}; | ||
|
||
|
||
pub async fn find_by_id(id: String) -> Option<Deployment> { | ||
pub async fn find_by_id(id: String) -> Result<Deployment, VoyagerError> { | ||
event!(Level::DEBUG, "Finding deployment with id {}", &id); | ||
|
||
let id_clone = id.clone(); | ||
let future = async move { | ||
let result = DB_CONTEXT | ||
.deployments | ||
.find_one(doc! { "_id": id }, None) | ||
.await; | ||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled( | ||
"repositories::deployments::find_by_id", | ||
DB_CONTEXT.deployments.find_one(doc! { "_id": &id }, None), | ||
) | ||
.await?; | ||
|
||
result.map_or_else( | ||
|e| Err(VoyagerError::find_mongo_id(Box::new(e), &id)), | ||
|r| r.ok_or_else(|| VoyagerError::find_null_id(&id)), | ||
) | ||
} | ||
|
||
result | ||
.map_err(Error::from) // MongoDB Error | ||
.map(|d| d.ok_or_else(|| Error::from("Deployment not found"))) // 'None' Error | ||
.and_then(|inner| inner) // Flatten | ||
}; | ||
impl VoyagerError { | ||
pub fn find_mongo_id(e: Error, id: &str) -> Self { | ||
let message = format!("Failed to find deployment by id '{id}'! Error:{e}"); | ||
|
||
let result = REPOSITORIES_RUNTIME | ||
.spawn_handled("repositories::deployments::find_by_id", future) | ||
.await; | ||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::INTERNAL_SERVER_ERROR, | ||
source: Some(e), | ||
} | ||
} | ||
|
||
pub fn find_null_id(id: &str) -> Self { | ||
let message = format!("Failed to find deployment by id '{id}'!"); | ||
|
||
result | ||
.map(|r| { | ||
r.map_or_else( | ||
|e| { | ||
event!( | ||
Level::ERROR, | ||
"Failed to find deployment with id {}: {}", | ||
id_clone, | ||
e | ||
); | ||
None | ||
}, | ||
Some, | ||
) | ||
}) | ||
.and_then(|d| d) | ||
event!(Level::ERROR, message); | ||
VoyagerError { | ||
message, | ||
status_code: StatusCode::NOT_FOUND, | ||
source: None, | ||
} | ||
} | ||
} |
Oops, something went wrong.