-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from Jim-Hodapp-Coaching/add_organizations_by_…
…user_id Add organizations by user
- Loading branch information
Showing
22 changed files
with
570 additions
and
177 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
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
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use super::error::{EntityApiErrorCode, Error}; | ||
use chrono::Utc; | ||
use entity::{ | ||
coaching_relationships, | ||
coaching_relationships::{ActiveModel, Model}, | ||
Id, | ||
}; | ||
use sea_orm::{entity::prelude::*, Condition, DatabaseConnection, Set}; | ||
use serde_json::from_str; | ||
|
||
use log::*; | ||
|
||
pub async fn create( | ||
db: &DatabaseConnection, | ||
coaching_relationship_model: Model, | ||
) -> Result<Model, Error> { | ||
debug!( | ||
"New Coaching Relationship Model to be inserted: {:?}", | ||
coaching_relationship_model | ||
); | ||
|
||
let now = Utc::now(); | ||
|
||
let coaching_relationship_active_model: ActiveModel = ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
organization_id: Set(coaching_relationship_model.organization_id), | ||
coach_id: Set(coaching_relationship_model.coach_id), | ||
coachee_id: Set(coaching_relationship_model.coachee_id), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
}; | ||
|
||
Ok(coaching_relationship_active_model.insert(db).await?) | ||
} | ||
|
||
pub async fn find_by_user(db: &DatabaseConnection, user_id: Id) -> Result<Vec<Model>, Error> { | ||
let coaching_relationships: Vec<coaching_relationships::Model> = | ||
coaching_relationships::Entity::find() | ||
.filter( | ||
Condition::any() | ||
.add(coaching_relationships::Column::CoachId.eq(user_id)) | ||
.add(coaching_relationships::Column::CoacheeId.eq(user_id)), | ||
) | ||
.all(db) | ||
.await?; | ||
|
||
Ok(coaching_relationships) | ||
} | ||
|
||
pub async fn find_by( | ||
db: &DatabaseConnection, | ||
params: std::collections::HashMap<String, String>, | ||
) -> Result<Vec<Model>, Error> { | ||
let mut query = coaching_relationships::Entity::find(); | ||
|
||
for (key, value) in params.iter() { | ||
match key.as_str() { | ||
"organization_id" => { | ||
query = by_organization(query, from_str::<Id>(value).unwrap()).await; | ||
} | ||
_ => { | ||
return Err(Error { | ||
inner: None, | ||
error_code: EntityApiErrorCode::InvalidQueryTerm, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
Ok(query.all(db).await?) | ||
} | ||
|
||
async fn by_organization( | ||
query: Select<coaching_relationships::Entity>, | ||
organization_id: Id, | ||
) -> Select<coaching_relationships::Entity> { | ||
query.filter(coaching_relationships::Column::OrganizationId.eq(organization_id)) | ||
} | ||
|
||
#[cfg(test)] | ||
// We need to gate seaORM's mock feature behind conditional compilation because | ||
// the feature removes the Clone trait implementation from seaORM's DatabaseConnection. | ||
// see https://github.com/SeaQL/sea-orm/issues/830 | ||
#[cfg(feature = "mock")] | ||
mod tests { | ||
use super::*; | ||
use sea_orm::{DatabaseBackend, MockDatabase, Transaction}; | ||
|
||
#[tokio::test] | ||
async fn find_by_user_returns_all_records_associated_with_user() -> Result<(), Error> { | ||
let db = MockDatabase::new(DatabaseBackend::Postgres) | ||
// .append_query_results(coaching_relationships) | ||
.into_connection(); | ||
|
||
let user_id = 1; | ||
let _ = find_by_user(&db, user_id).await; | ||
|
||
assert_eq!( | ||
db.into_transaction_log(), | ||
[Transaction::from_sql_and_values( | ||
DatabaseBackend::Postgres, | ||
r#"SELECT "coaching_relationships"."id", "coaching_relationships"."external_id", "coaching_relationships"."organization_id", "coaching_relationships"."coach_id", "coaching_relationships"."coachee_id", "coaching_relationships"."created_at", "coaching_relationships"."updated_at" FROM "refactor_platform"."coaching_relationships" WHERE "coaching_relationships"."coach_id" = $1 OR "coaching_relationships"."coachee_id" = $2"#, | ||
[user_id.into(), user_id.into()] | ||
)] | ||
); | ||
|
||
Ok(()) | ||
} | ||
} |
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,10 +1,115 @@ | ||
use sea_orm::DatabaseConnection; | ||
use chrono::Utc; | ||
use password_auth::generate_hash; | ||
use sea_orm::{prelude::Uuid, ActiveModelTrait, DatabaseConnection, Set}; | ||
|
||
use entity::coaching_relationships; | ||
use entity::organizations; | ||
use entity::users; | ||
|
||
pub mod coaching_relationship; | ||
pub mod error; | ||
pub mod organization; | ||
pub mod user; | ||
|
||
pub async fn seed_database(db: &DatabaseConnection) { | ||
organization::seed_database(db).await; | ||
user::seed_database(db).await; | ||
let now = Utc::now(); | ||
|
||
let jim_hodapp: users::ActiveModel = users::ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
email: Set("[email protected]".to_owned()), | ||
first_name: Set(Some("Jim".to_owned())), | ||
last_name: Set(Some("Hodapp".to_owned())), | ||
display_name: Set(Some("Jim H".to_owned())), | ||
password: Set(generate_hash("password")), | ||
github_username: Set(Some("jhodapp".to_owned())), | ||
github_profile_url: Set(Some("https://github.com/jhodapp".to_owned())), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
let caleb_bourg: users::ActiveModel = users::ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
email: Set("[email protected]".to_owned()), | ||
first_name: Set(Some("Caleb".to_owned())), | ||
last_name: Set(Some("Bourg".to_owned())), | ||
display_name: Set(Some("cbourg2".to_owned())), | ||
password: Set(generate_hash("password")), | ||
github_username: Set(Some("calebbourg".to_owned())), | ||
github_profile_url: Set(Some("https://github.com/calebbourg".to_owned())), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
let other_user: users::ActiveModel = users::ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
email: Set("[email protected]".to_owned()), | ||
first_name: Set(Some("Other".to_owned())), | ||
last_name: Set(Some("User".to_owned())), | ||
display_name: Set(Some("Other U.".to_owned())), | ||
password: Set(generate_hash("password")), | ||
github_username: Set(None), | ||
github_profile_url: Set(None), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
let jim_hodapp_coaching = organizations::ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
name: Set("Jim Hodapp's Coaching".to_owned()), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
let jim_hodapp_other_org = organizations::ActiveModel { | ||
external_id: Set(Uuid::new_v4()), | ||
name: Set("Jim Hodapp's Other Organization".to_owned()), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
coaching_relationships::ActiveModel { | ||
coach_id: Set(jim_hodapp.id.clone().unwrap()), | ||
coachee_id: Set(caleb_bourg.id.clone().unwrap()), | ||
organization_id: Set(jim_hodapp_coaching.id.unwrap()), | ||
external_id: Set(Uuid::new_v4()), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
|
||
coaching_relationships::ActiveModel { | ||
coach_id: Set(jim_hodapp.id.clone().unwrap()), | ||
coachee_id: Set(other_user.id.clone().unwrap()), | ||
organization_id: Set(jim_hodapp_other_org.id.unwrap()), | ||
external_id: Set(Uuid::new_v4()), | ||
created_at: Set(now.into()), | ||
updated_at: Set(now.into()), | ||
..Default::default() | ||
} | ||
.save(db) | ||
.await | ||
.unwrap(); | ||
} |
Oops, something went wrong.