-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): integrate space's subscription data in command
- Loading branch information
1 parent
6ff0623
commit 869b9f4
Showing
22 changed files
with
468 additions
and
281 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ use ockam_core::Error; | |
|
||
use crate::cli_state::CliState; | ||
use crate::cloud::space::Space; | ||
use crate::cloud::subscription::Subscription; | ||
|
||
use super::Result; | ||
|
||
|
@@ -13,12 +14,14 @@ impl CliState { | |
space_id: &str, | ||
space_name: &str, | ||
users: Vec<&str>, | ||
subscription: Option<&Subscription>, | ||
) -> Result<Space> { | ||
let repository = self.spaces_repository(); | ||
let space = Space { | ||
id: space_id.to_string(), | ||
name: space_name.to_string(), | ||
users: users.iter().map(|u| u.to_string()).collect(), | ||
subscription: subscription.cloned(), | ||
}; | ||
|
||
repository.store_space(&space).await?; | ||
|
@@ -96,13 +99,26 @@ mod test { | |
|
||
// the first created space becomes the default | ||
let space1 = cli | ||
.store_space("1", "name1", vec!["[email protected]", "[email protected]"]) | ||
.store_space( | ||
"1", | ||
"name1", | ||
vec!["[email protected]", "[email protected]"], | ||
Some(&Subscription { | ||
name: "name1".to_string(), | ||
is_free_trial: false, | ||
marketplace: None, | ||
start_date: None, | ||
end_date: None, | ||
}), | ||
) | ||
.await?; | ||
let result = cli.get_default_space().await?; | ||
assert_eq!(result, space1); | ||
|
||
// the store method can be used to update a space | ||
let updated_space1 = cli.store_space("1", "name1", vec!["[email protected]"]).await?; | ||
let updated_space1 = cli | ||
.store_space("1", "name1", vec!["[email protected]"], None) | ||
.await?; | ||
let result = cli.get_default_space().await?; | ||
assert_eq!(result, updated_space1); | ||
|
||
|
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ use sqlx::*; | |
|
||
use ockam_core::async_trait; | ||
use ockam_core::Result; | ||
use ockam_node::database::{Boolean, FromSqlxError, SqlxDatabase, ToVoid}; | ||
use ockam_node::database::{Boolean, FromSqlxError, Nullable, SqlxDatabase, ToVoid}; | ||
|
||
use crate::cloud::space::Space; | ||
|
||
|
@@ -42,14 +42,20 @@ impl SpacesRepository for SpacesSqlxDatabase { | |
|
||
let query2 = query( | ||
r#" | ||
INSERT INTO space (space_id, space_name, is_default) | ||
VALUES ($1, $2, $3) | ||
INSERT INTO space (space_id, space_name, is_default, subscription) | ||
VALUES ($1, $2, $3, $4) | ||
ON CONFLICT (space_id) | ||
DO UPDATE SET space_name = $2, is_default = $3"#, | ||
DO UPDATE SET space_name = $2, is_default = $3, subscription = $4"#, | ||
) | ||
.bind(&space.id) | ||
.bind(&space.name) | ||
.bind(is_already_default); | ||
.bind(is_already_default) | ||
.bind( | ||
space | ||
.subscription | ||
.as_ref() | ||
.and_then(|s| serde_json::to_string(s).ok()), | ||
); | ||
query2.execute(&mut *transaction).await.void()?; | ||
|
||
// remove any existing users related to that space if any | ||
|
@@ -91,7 +97,8 @@ impl SpacesRepository for SpacesSqlxDatabase { | |
let mut transaction = self.database.begin().await.into_core()?; | ||
|
||
let query1 = | ||
query_as("SELECT space_id, space_name FROM space WHERE space_name = $1").bind(name); | ||
query_as("SELECT space_id, space_name, subscription FROM space WHERE space_name = $1") | ||
.bind(name); | ||
let row: Option<SpaceRow> = query1.fetch_optional(&mut *transaction).await.into_core()?; | ||
let space = match row.map(|r| r.space()) { | ||
Some(mut space) => { | ||
|
@@ -113,7 +120,7 @@ impl SpacesRepository for SpacesSqlxDatabase { | |
async fn get_spaces(&self) -> Result<Vec<Space>> { | ||
let mut transaction = self.database.begin().await.into_core()?; | ||
|
||
let query = query_as("SELECT space_id, space_name FROM space"); | ||
let query = query_as("SELECT space_id, space_name, subscription FROM space"); | ||
let row: Vec<SpaceRow> = query.fetch_all(&mut *transaction).await.into_core()?; | ||
|
||
let mut spaces = vec![]; | ||
|
@@ -180,6 +187,7 @@ impl SpacesRepository for SpacesSqlxDatabase { | |
struct SpaceRow { | ||
space_id: String, | ||
space_name: String, | ||
subscription: Nullable<String>, | ||
} | ||
|
||
impl SpaceRow { | ||
|
@@ -192,6 +200,11 @@ impl SpaceRow { | |
id: self.space_id.clone(), | ||
name: self.space_name.clone(), | ||
users: user_emails, | ||
subscription: self | ||
.subscription | ||
.to_option() | ||
.as_ref() | ||
.and_then(|s| serde_json::from_str(s).ok()), | ||
} | ||
} | ||
} | ||
|
@@ -207,6 +220,7 @@ struct UserSpaceRow { | |
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::cloud::subscription::Subscription; | ||
use ockam_node::database::with_dbs; | ||
use std::sync::Arc; | ||
|
||
|
@@ -220,6 +234,7 @@ mod test { | |
id: "1".to_string(), | ||
name: "name1".to_string(), | ||
users: vec!["[email protected]".to_string(), "[email protected]".to_string()], | ||
subscription: None, | ||
}; | ||
let mut space2 = Space { | ||
id: "2".to_string(), | ||
|
@@ -229,6 +244,13 @@ mod test { | |
"[email protected]".to_string(), | ||
"[email protected]".to_string(), | ||
], | ||
subscription: Some(Subscription { | ||
name: "premium".to_string(), | ||
is_free_trial: false, | ||
marketplace: Some("aws".to_string()), | ||
start_date: Some(chrono::Utc::now().to_string()), | ||
end_date: Some((chrono::Utc::now() + chrono::Duration::days(30)).to_string()), | ||
}), | ||
}; | ||
|
||
repository.store_space(&space1).await?; | ||
|
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
Oops, something went wrong.