Skip to content

Commit

Permalink
pull user_id from user sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
calebbourg committed Aug 21, 2024
1 parent 6bf5baa commit 776dbc7
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 34 deletions.
1 change: 1 addition & 0 deletions entity/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: Id,
pub coaching_session_id: Id,
#[serde(skip_deserializing)]
pub user_id: Id,
pub body: Option<String>,
pub due_by: Option<DateTimeWithTimeZone>,
Expand Down
1 change: 1 addition & 0 deletions entity/src/agreements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Model {
#[sea_orm(unique)]
pub coaching_session_id: Id,
pub body: Option<String>,
#[serde(skip_deserializing)]
pub user_id: Id,
#[serde(skip_deserializing)]
pub status: status::Status,
Expand Down
1 change: 1 addition & 0 deletions entity/src/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Model {
pub id: Id,
pub coaching_session_id: Id,
pub body: Option<String>,
#[serde(skip_deserializing)]
pub user_id: Id,
#[serde(skip_deserializing)]
pub created_at: DateTimeWithTimeZone,
Expand Down
1 change: 1 addition & 0 deletions entity/src/overarching_goals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: Id,
pub coaching_session_id: Option<Id>,
#[serde(skip_deserializing)]
pub user_id: Id,
pub title: Option<String>,
pub body: Option<String>,
Expand Down
12 changes: 8 additions & 4 deletions entity_api/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ use std::collections::HashMap;

use log::*;

pub async fn create(db: &DatabaseConnection, action_model: Model) -> Result<Model, Error> {
pub async fn create(
db: &DatabaseConnection,
action_model: Model,
user_id: Id,
) -> Result<Model, Error> {
debug!("New Action Model to be inserted: {:?}", action_model);

let now = chrono::Utc::now();

let action_active_model: ActiveModel = ActiveModel {
coaching_session_id: Set(action_model.coaching_session_id),
user_id: Set(action_model.user_id),
user_id: Set(user_id),
due_by: Set(action_model.due_by),
body: Set(action_model.body),
created_at: Set(now.into()),
Expand Down Expand Up @@ -127,10 +131,10 @@ mod tests {

let action_model = Model {
id: Id::new_v4(),
user_id: Id::new_v4(),
coaching_session_id: Id::new_v4(),
body: Some("This is a action".to_owned()),
due_by: Some(now.into()),
user_id: Id::new_v4(),
status_changed_at: None,
status: Default::default(),
created_at: now.into(),
Expand All @@ -141,7 +145,7 @@ mod tests {
.append_query_results(vec![vec![action_model.clone()]])
.into_connection();

let action = create(&db, action_model.clone().into()).await?;
let action = create(&db, action_model.clone().into(), Id::new_v4()).await?;

assert_eq!(action.id, action_model.id);

Expand Down
12 changes: 8 additions & 4 deletions entity_api/src/agreement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ use std::collections::HashMap;

use log::*;

pub async fn create(db: &DatabaseConnection, agreement_model: Model) -> Result<Model, Error> {
pub async fn create(
db: &DatabaseConnection,
agreement_model: Model,
user_id: Id,
) -> Result<Model, Error> {
debug!("New Agreement Model to be inserted: {:?}", agreement_model);

let now = chrono::Utc::now();

let agreement_active_model: ActiveModel = ActiveModel {
coaching_session_id: Set(agreement_model.coaching_session_id),
body: Set(agreement_model.body),
user_id: Set(agreement_model.user_id),
user_id: Set(user_id),
created_at: Set(now.into()),
updated_at: Set(now.into()),
status_changed_at: Set(None),
Expand Down Expand Up @@ -127,9 +131,9 @@ mod tests {

let agreement_model = Model {
id: Id::new_v4(),
user_id: Id::new_v4(),
coaching_session_id: Id::new_v4(),
body: Some("This is a agreement".to_owned()),
user_id: Id::new_v4(),
status_changed_at: None,
status: Default::default(),
created_at: now.into(),
Expand All @@ -140,7 +144,7 @@ mod tests {
.append_query_results(vec![vec![agreement_model.clone()]])
.into_connection();

let agreement = create(&db, agreement_model.clone().into()).await?;
let agreement = create(&db, agreement_model.clone().into(), Id::new_v4()).await?;

assert_eq!(agreement.id, agreement_model.id);

Expand Down
12 changes: 8 additions & 4 deletions entity_api/src/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ use std::collections::HashMap;

use log::*;

pub async fn create(db: &DatabaseConnection, note_model: Model) -> Result<Model, Error> {
pub async fn create(
db: &DatabaseConnection,
note_model: Model,
user_id: Id,
) -> Result<Model, Error> {
debug!("New Note Model to be inserted: {:?}", note_model);

let now = chrono::Utc::now();

let note_active_model: ActiveModel = ActiveModel {
coaching_session_id: Set(note_model.coaching_session_id),
body: Set(note_model.body),
user_id: Set(note_model.user_id),
user_id: Set(user_id),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
Expand Down Expand Up @@ -123,9 +127,9 @@ mod tests {

let note_model = Model {
id: Id::new_v4(),
user_id: Id::new_v4(),
coaching_session_id: Id::new_v4(),
body: Some("This is a note".to_owned()),
user_id: Id::new_v4(),
created_at: now.into(),
updated_at: now.into(),
};
Expand All @@ -134,7 +138,7 @@ mod tests {
.append_query_results(vec![vec![note_model.clone()]])
.into_connection();

let note = create(&db, note_model.clone().into()).await?;
let note = create(&db, note_model.clone().into(), Id::new_v4()).await?;

assert_eq!(note.id, note_model.id);

Expand Down
28 changes: 14 additions & 14 deletions entity_api/src/overarching_goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use log::*;
pub async fn create(
db: &DatabaseConnection,
overarching_goal_model: Model,
user_id: Id,
) -> Result<Model, Error> {
debug!(
"New Overarching Goal Model to be inserted: {:?}",
Expand All @@ -25,7 +26,7 @@ pub async fn create(

let overarching_goal_active_model: ActiveModel = ActiveModel {
coaching_session_id: Set(overarching_goal_model.coaching_session_id),
user_id: Set(overarching_goal_model.user_id),
user_id: Set(user_id),
title: Set(overarching_goal_model.title),
body: Set(overarching_goal_model.body),
created_at: Set(now.into()),
Expand Down Expand Up @@ -135,20 +136,19 @@ pub async fn find_by(
mod tests {
use super::*;
use entity::{overarching_goals::Model, Id};
use sea_orm::{DatabaseBackend, MockDatabase, Transoverarching_goal};
use sea_orm::{DatabaseBackend, MockDatabase, Transaction};

#[tokio::test]
async fn create_returns_a_new_overarching_goal_model() -> Result<(), Error> {
let now = chrono::Utc::now();

let overarching_goal_model = Model {
id: Id::new_v4(),
coaching_session_id: Id::new_v4(),
body: Some("This is a overarching_goal".to_owned()),
due_by: Some(now.into()),
user_id: Id::new_v4(),
status_changed_at: None,
status: Default::default(),
coaching_session_id: Some(Id::new_v4()),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
completed_at: Some(now.into()),
created_at: now.into(),
updated_at: now.into(),
};
Expand All @@ -157,7 +157,8 @@ mod tests {
.append_query_results(vec![vec![overarching_goal_model.clone()]])
.into_connection();

let overarching_goal = create(&db, overarching_goal_model.clone().into()).await?;
let overarching_goal =
create(&db, overarching_goal_model.clone().into(), Id::new_v4()).await?;

assert_eq!(overarching_goal.id, overarching_goal_model.id);

Expand All @@ -170,12 +171,11 @@ mod tests {

let overarching_goal_model = Model {
id: Id::new_v4(),
coaching_session_id: Id::new_v4(),
due_by: Some(now.into()),
coaching_session_id: Some(Id::new_v4()),
title: Some("title".to_owned()),
body: Some("This is a overarching_goal".to_owned()),
user_id: Id::new_v4(),
status_changed_at: None,
status: Default::default(),
completed_at: Some(now.into()),
created_at: now.into(),
updated_at: now.into(),
};
Expand Down Expand Up @@ -215,9 +215,9 @@ mod tests {

assert_eq!(
db.into_transaction_log(),
[Transoverarching_goal::from_sql_and_values(
[Transaction::from_sql_and_values(
DatabaseBackend::Postgres,
r#"SELECT "overarching_goals"."id", "overarching_goals"."coaching_session_id", "overarching_goals"."user_id", "overarching_goals"."body", "overarching_goals"."title", "overarching_goals"."created_at", "overarching_goals"."updated_at" FROM "refactor_platform"."overarching_goals" WHERE "overarching_goals"."coaching_session_id" = $1"#,
r#"SELECT "overarching_goals"."id", "overarching_goals"."coaching_session_id", "overarching_goals"."user_id", "overarching_goals"."title", "overarching_goals"."body", "overarching_goals"."completed_at", "overarching_goals"."created_at", "overarching_goals"."updated_at" FROM "refactor_platform"."overarching_goals" WHERE "overarching_goals"."coaching_session_id" = $1"#,
[coaching_session_id.into()]
)]
);
Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/action_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ use log::*;

pub async fn create(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Json(action_model): Json<Model>,
) -> Result<impl IntoResponse, Error> {
debug!("POST Create a New Action from: {:?}", action_model);

let action = ActionApi::create(app_state.db_conn_ref(), action_model).await?;
let action = ActionApi::create(app_state.db_conn_ref(), action_model, user.id).await?;

debug!("New Action: {:?}", action);

Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/agreement_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ use log::*;

pub async fn create(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Json(agreement_model): Json<Model>,
) -> Result<impl IntoResponse, Error> {
debug!("POST Create a New Agreement from: {:?}", agreement_model);

let agreement = AgreementApi::create(app_state.db_conn_ref(), agreement_model).await?;
let agreement = AgreementApi::create(app_state.db_conn_ref(), agreement_model, user.id).await?;

debug!("New Agreement: {:?}", agreement);

Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/note_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ use log::*;

pub async fn create(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Json(note_model): Json<notes::Model>,
) -> Result<impl IntoResponse, Error> {
debug!("POST Create a New Note from: {:?}", note_model);

let note = NoteApi::create(app_state.db_conn_ref(), note_model).await?;
let note = NoteApi::create(app_state.db_conn_ref(), note_model, user.id).await?;

debug!("New Note: {:?}", note);

Expand Down
4 changes: 2 additions & 2 deletions web/src/controller/overarching_goal_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use log::*;

pub async fn create(
CompareApiVersion(_v): CompareApiVersion,
AuthenticatedUser(_user): AuthenticatedUser,
AuthenticatedUser(user): AuthenticatedUser,
// TODO: create a new Extractor to authorize the user to access
// the data requested
State(app_state): State<AppState>,
Expand All @@ -45,7 +45,7 @@ pub async fn create(
);

let overarching_goals =
OverarchingGoalApi::create(app_state.db_conn_ref(), overarching_goals_model).await?;
OverarchingGoalApi::create(app_state.db_conn_ref(), overarching_goals_model, user.id).await?;

debug!("New Overarching Goal: {:?}", overarching_goals);

Expand Down

0 comments on commit 776dbc7

Please sign in to comment.