Skip to content

Commit 229f8e7

Browse files
authored
Merge pull request #103 from refactor-group/more_refactoring
More refactoring
2 parents bb28178 + 2d0b587 commit 229f8e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+356
-564
lines changed

Cargo.lock

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
This diagram represents the dependency structure of the crates in this project. Each arrow indicates a dependency relationship between the crates. For example, the `web` crate depends on both the `domain` and `service` crates, while the `entity_api` crate depends on the `entity` and `service` crates.
3+
4+
```mermaid
5+
graph TD;
6+
web-->domain;
7+
web-->service;
8+
domain-->entity_api;
9+
entity_api-->entity;
10+
entity_api-->service;
11+
```

domain/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2021"
55

66
[dependencies]
77
chrono = { version = "0.4.38", features = ["serde"] }
8-
entity = { path = "../entity" }
98
entity_api = { path = "../entity_api" }
109
jsonwebtoken = "9"
1110
service = { path = "../service" }

domain/src/action.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::actions::Model;
2+
use crate::error::Error;
3+
use entity_api::IntoQueryFilterMap;
4+
use entity_api::{actions, query};
5+
use sea_orm::DatabaseConnection;
6+
7+
pub use entity_api::action::{create, delete_by_id, find_by_id, update, update_status};
8+
9+
pub async fn find_by(
10+
db: &DatabaseConnection,
11+
params: impl IntoQueryFilterMap,
12+
) -> Result<Vec<Model>, Error> {
13+
let actions =
14+
query::find_by::<actions::Entity, actions::Column>(db, params.into_query_filter_map())
15+
.await?;
16+
17+
Ok(actions)
18+
}

domain/src/agreement.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
use crate::agreements::Model;
12
use crate::error::Error;
2-
use entity::agreements::Model;
3-
pub use entity_api::agreement::{create, delete_by_id, find_by_id, update};
4-
use entity_api::{agreement, IntoQueryFilterMap};
3+
use entity_api::IntoQueryFilterMap;
4+
use entity_api::{agreements, query};
55
use sea_orm::DatabaseConnection;
66

7+
pub use entity_api::agreement::{create, delete_by_id, find_by_id, update};
8+
79
pub async fn find_by(
810
db: &DatabaseConnection,
911
params: impl IntoQueryFilterMap,
1012
) -> Result<Vec<Model>, Error> {
11-
let agreements = agreement::find_by(db, params.into_query_filter_map()).await?;
13+
let agreements = query::find_by::<agreements::Entity, agreements::Column>(
14+
db,
15+
params.into_query_filter_map(),
16+
)
17+
.await?;
1218

1319
Ok(agreements)
1420
}

domain/src/coaching_relationship.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::coaching_relationships::Model;
2+
use crate::error::Error;
3+
use entity_api::IntoQueryFilterMap;
4+
use entity_api::{coaching_relationships, query};
5+
use sea_orm::DatabaseConnection;
6+
7+
pub use entity_api::coaching_relationship::{
8+
create, find_by_id, find_by_organization_with_user_names, find_by_user,
9+
get_relationship_with_user_names, CoachingRelationshipWithUserNames,
10+
};
11+
12+
pub async fn find_by(
13+
db: &DatabaseConnection,
14+
params: impl IntoQueryFilterMap,
15+
) -> Result<Vec<Model>, Error> {
16+
let coaching_relationships = query::find_by::<
17+
coaching_relationships::Entity,
18+
coaching_relationships::Column,
19+
>(db, params.into_query_filter_map())
20+
.await?;
21+
22+
Ok(coaching_relationships)
23+
}

domain/src/coaching_session.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
use crate::coaching_sessions::Model;
12
use crate::error::{DomainErrorKind, Error, ExternalErrorKind, InternalErrorKind};
23
use crate::gateway::tiptap::client as tiptap_client;
34
use chrono::{DurationRound, TimeDelta};
4-
use entity::coaching_sessions::Model;
5-
use entity_api::{coaching_relationship, coaching_session, organization};
5+
use entity_api::{
6+
coaching_relationship, coaching_session, coaching_sessions, organization, query,
7+
IntoQueryFilterMap,
8+
};
69
use log::*;
710
use sea_orm::DatabaseConnection;
811
use serde_json::json;
912
use service::config::Config;
1013

14+
pub use entity_api::coaching_session::{find_by_id, find_by_id_with_coaching_relationship};
15+
1116
pub async fn create(
1217
db: &DatabaseConnection,
1318
config: &Config,
@@ -82,20 +87,15 @@ pub async fn create(
8287
}
8388
}
8489

85-
pub async fn find_by_id(db: &DatabaseConnection, id: entity::Id) -> Result<Model, Error> {
86-
Ok(coaching_session::find_by_id(db, id).await?)
87-
}
88-
89-
pub async fn find_by_id_with_coaching_relationship(
90-
db: &DatabaseConnection,
91-
id: entity::Id,
92-
) -> Result<(Model, entity::coaching_relationships::Model), Error> {
93-
Ok(coaching_session::find_by_id_with_coaching_relationship(db, id).await?)
94-
}
95-
9690
pub async fn find_by(
9791
db: &DatabaseConnection,
98-
params: std::collections::HashMap<String, String>,
92+
params: impl IntoQueryFilterMap,
9993
) -> Result<Vec<Model>, Error> {
100-
Ok(coaching_session::find_by(db, params).await?)
94+
let coaching_sessions = query::find_by::<coaching_sessions::Entity, coaching_sessions::Column>(
95+
db,
96+
params.into_query_filter_map(),
97+
)
98+
.await?;
99+
100+
Ok(coaching_sessions)
101101
}

domain/src/jwt/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! use domain::jwt::generate_collab_token;
1414
//! use sea_orm::DatabaseConnection;
1515
//! use service::config::Config;
16-
//! use entity::Id;
16+
//! use crate::Id;
1717
//!
1818
//! async fn example(db: &DatabaseConnection, config: &Config, coaching_session_id: Id) {
1919
//! match generate_collab_token(db, config, coaching_session_id).await {
@@ -23,18 +23,14 @@
2323
//! }
2424
//! ```
2525
26-
use crate::coaching_session;
2726
use crate::error::{DomainErrorKind, Error, InternalErrorKind};
27+
use crate::{coaching_session, jwts::Jwt, Id};
2828
use claims::TiptapCollabClaims;
29-
use entity::Id;
3029
use jsonwebtoken::{encode, EncodingKey, Header};
3130
use log::*;
3231
use sea_orm::DatabaseConnection;
3332
use service::config::Config;
3433

35-
// re-export the Jwt struct from the entity module
36-
pub use entity::jwt::Jwt;
37-
3834
pub(crate) mod claims;
3935

4036
/// Generates a collaboration token for a coaching session.

domain/src/lib.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
//! This module re-exports `IntoQueryFilterMap` and `QueryFilterMap` from the `entity_api` crate.
1+
//! This module re-exports various items from the `entity_api` crate.
22
//!
33
//! The purpose of this re-export is to ensure that consumers of the `domain` crate do not need to
44
//! directly depend on the `entity_api` crate. By re-exporting these items, we provide a clear and
55
//! consistent interface for working with query filters within the domain layer, while encapsulating
66
//! the underlying implementation details remain in the `entity_api` crate.
77
pub use entity_api::{IntoQueryFilterMap, QueryFilterMap};
88

9+
// Re-exports from `entity`
10+
pub use entity_api::user::{AuthSession, Backend, Credentials};
11+
pub use entity_api::{
12+
actions, agreements, coachees, coaches, coaching_relationships, coaching_sessions, jwts, notes,
13+
organizations, overarching_goals, users, Id,
14+
};
15+
16+
pub mod action;
917
pub mod agreement;
18+
pub mod coaching_relationship;
1019
pub mod coaching_session;
1120
pub mod error;
1221
pub mod jwt;
22+
pub mod note;
23+
pub mod organization;
24+
pub mod overarching_goal;
25+
pub mod user;
1326

1427
pub(crate) mod gateway;

domain/src/note.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub use entity_api::note::{create, find_by, find_by_id, update};

domain/src/organization.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub use entity_api::organization::{
2+
create, delete_by_id, find_all, find_by, find_by_id, find_by_user, update,
3+
};

domain/src/overarching_goal.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::error::Error;
2+
use crate::overarching_goals::Model;
3+
use entity_api::IntoQueryFilterMap;
4+
use entity_api::{overarching_goals, query};
5+
use sea_orm::DatabaseConnection;
6+
7+
pub use entity_api::overarching_goal::{create, find_by_id, update, update_status};
8+
9+
pub async fn find_by(
10+
db: &DatabaseConnection,
11+
params: impl IntoQueryFilterMap,
12+
) -> Result<Vec<Model>, Error> {
13+
let overarching_goals = query::find_by::<overarching_goals::Entity, overarching_goals::Column>(
14+
db,
15+
params.into_query_filter_map(),
16+
)
17+
.await?;
18+
19+
Ok(overarching_goals)
20+
}

domain/src/user.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub use entity_api::user::{create, find_by_email};
File renamed without changes.

entity/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod coachees;
88
pub mod coaches;
99
pub mod coaching_relationships;
1010
pub mod coaching_sessions;
11-
pub mod jwt;
11+
pub mod jwts;
1212
pub mod notes;
1313
pub mod organizations;
1414
pub mod overarching_goals;

entity_api/src/action.rs

+9-88
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
use super::error::{EntityApiErrorKind, Error};
2-
use crate::uuid_parse_str;
3-
use entity::actions::{self, ActiveModel, Entity, Model};
2+
use entity::actions::{ActiveModel, Entity, Model};
43
use entity::{status::Status, Id};
54
use sea_orm::{
65
entity::prelude::*,
76
ActiveValue::{Set, Unchanged},
87
DatabaseConnection, TryIntoModel,
98
};
10-
use std::collections::HashMap;
119

1210
use log::*;
1311

@@ -106,68 +104,16 @@ pub async fn update_status(
106104
pub async fn delete_by_id(db: &DatabaseConnection, id: Id) -> Result<(), Error> {
107105
let result = find_by_id(db, id).await?;
108106

109-
match result {
110-
Some(action_model) => {
111-
debug!("Existing Action model to be deleted: {:?}", action_model);
112-
113-
action_model.delete(db).await?;
114-
Ok(())
115-
}
116-
None => Err(Error {
117-
source: None,
118-
error_kind: EntityApiErrorKind::RecordNotFound,
119-
}),
120-
}
121-
}
122-
123-
pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Option<Model>, Error> {
124-
match Entity::find_by_id(id).one(db).await {
125-
Ok(Some(action)) => {
126-
debug!("Action found: {:?}", action);
107+
result.delete(db).await?;
127108

128-
Ok(Some(action))
129-
}
130-
Ok(None) => {
131-
error!("Action with id {} not found", id);
132-
133-
Err(Error {
134-
source: None,
135-
error_kind: EntityApiErrorKind::RecordNotFound,
136-
})
137-
}
138-
Err(err) => {
139-
error!("Action with id {} not found and returned error {}", id, err);
140-
Err(Error {
141-
source: None,
142-
error_kind: EntityApiErrorKind::RecordNotFound,
143-
})
144-
}
145-
}
109+
Ok(())
146110
}
147111

148-
pub async fn find_by(
149-
db: &DatabaseConnection,
150-
query_params: HashMap<String, String>,
151-
) -> Result<Vec<Model>, Error> {
152-
let mut query = Entity::find();
153-
154-
for (key, value) in query_params {
155-
match key.as_str() {
156-
"coaching_session_id" => {
157-
let coaching_session_id = uuid_parse_str(&value)?;
158-
159-
query = query.filter(actions::Column::CoachingSessionId.eq(coaching_session_id));
160-
}
161-
_ => {
162-
return Err(Error {
163-
source: None,
164-
error_kind: EntityApiErrorKind::InvalidQueryTerm,
165-
});
166-
}
167-
}
168-
}
169-
170-
Ok(query.all(db).await?)
112+
pub async fn find_by_id(db: &DatabaseConnection, id: Id) -> Result<Model, Error> {
113+
Entity::find_by_id(id).one(db).await?.ok_or_else(|| Error {
114+
source: None,
115+
error_kind: EntityApiErrorKind::RecordNotFound,
116+
})
171117
}
172118

173119
#[cfg(test)]
@@ -178,7 +124,7 @@ pub async fn find_by(
178124
mod tests {
179125
use super::*;
180126
use entity::{actions::Model, Id};
181-
use sea_orm::{DatabaseBackend, MockDatabase, Transaction};
127+
use sea_orm::{DatabaseBackend, MockDatabase};
182128

183129
#[tokio::test]
184130
async fn create_returns_a_new_action_model() -> Result<(), Error> {
@@ -286,29 +232,4 @@ mod tests {
286232

287233
Ok(())
288234
}
289-
290-
#[tokio::test]
291-
async fn find_by_returns_all_actions_associated_with_coaching_session() -> Result<(), Error> {
292-
let db = MockDatabase::new(DatabaseBackend::Postgres).into_connection();
293-
let mut query_params = HashMap::new();
294-
let coaching_session_id = Id::new_v4();
295-
296-
query_params.insert(
297-
"coaching_session_id".to_owned(),
298-
coaching_session_id.to_string(),
299-
);
300-
301-
let _ = find_by(&db, query_params).await;
302-
303-
assert_eq!(
304-
db.into_transaction_log(),
305-
[Transaction::from_sql_and_values(
306-
DatabaseBackend::Postgres,
307-
r#"SELECT "actions"."id", "actions"."coaching_session_id", "actions"."user_id", "actions"."body", "actions"."due_by", CAST("actions"."status" AS text), "actions"."status_changed_at", "actions"."created_at", "actions"."updated_at" FROM "refactor_platform"."actions" WHERE "actions"."coaching_session_id" = $1"#,
308-
[coaching_session_id.into()]
309-
)]
310-
);
311-
312-
Ok(())
313-
}
314235
}

0 commit comments

Comments
 (0)