Skip to content

Commit 92b19df

Browse files
authored
Merge pull request #107 from refactor-group/add_slugify
Slugify organization names
2 parents d4395fb + bf94853 commit 92b19df

9 files changed

+47
-15
lines changed

Cargo.lock

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

entity/src/coaching_relationships.rs

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ pub struct Model {
2020
pub coach_id: Id,
2121
pub coachee_id: Id,
2222
#[serde(skip_deserializing)]
23+
// TODO we need to make sure this is unique in the scope of an organization_id.
24+
// I did some research and there are two ways to do this:
25+
// 1. Create a unique constraint at the database level.
26+
// 2. Add application logic (probably in entity_api) to make the check.
27+
// We'll need to add a migration for that eventually.
28+
#[sea_orm(unique)]
2329
pub slug: String,
2430
#[serde(skip_deserializing)]
2531
#[schema(value_type = String, format = DateTime)] // Applies to OpenAPI schema

entity/src/organizations.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub struct Model {
1616
pub name: String,
1717
pub logo: Option<String>,
1818
#[serde(skip_deserializing)]
19+
#[sea_orm(unique)]
1920
pub slug: String,
2021
#[serde(skip_deserializing)]
2122
#[schema(value_type = String, format = DateTime)] // Applies to OpenAPI schema

entity/src/users.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ pub struct Model {
1616
pub id: Id,
1717
#[sea_orm(unique)]
1818
pub email: String,
19-
pub first_name: Option<String>,
20-
pub last_name: Option<String>,
19+
pub first_name: String,
20+
pub last_name: String,
2121
pub display_name: Option<String>,
2222
#[serde(skip_serializing)]
2323
pub password: String,

entity_api/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ log = "0.4.22"
1414
axum-login = "0.16.0"
1515
async-trait = "0.1.83"
1616
password-auth = "1.0.0"
17+
slugify = "0.1.0"
1718
sqlx = { version = "0.8.2", features = ["time", "runtime-tokio"] }
1819
sqlx-sqlite = { version = "0.8.2" }
1920
utoipa = { version = "4.2.0", features = ["axum_extras", "uuid"] }

entity_api/src/coaching_relationship.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
use super::error::{EntityApiErrorKind, Error};
2+
use crate::user;
23
use chrono::Utc;
34
use entity::{
45
coachees, coaches,
56
coaching_relationships::{self, ActiveModel, Entity, Model},
67
Id,
78
};
9+
use log::*;
810
use sea_orm::{
911
entity::prelude::*, sea_query::Alias, Condition, DatabaseConnection, FromQueryResult, JoinType,
1012
QuerySelect, QueryTrait, Set,
1113
};
1214
use serde::ser::{Serialize, SerializeStruct, Serializer};
13-
14-
use log::*;
15+
use slugify::slugify;
1516

1617
pub async fn create(
1718
db: &DatabaseConnection,
@@ -23,11 +24,15 @@ pub async fn create(
2324
);
2425

2526
let now = Utc::now();
27+
let coach = user::find_by_id(db, coaching_relationship_model.coach_id).await?;
28+
let coachee = user::find_by_id(db, coaching_relationship_model.coachee_id).await?;
29+
let slug = slugify!(format!("{} {}", coach.first_name, coachee.first_name).as_str());
2630

2731
let coaching_relationship_active_model: ActiveModel = ActiveModel {
2832
organization_id: Set(coaching_relationship_model.organization_id),
2933
coach_id: Set(coaching_relationship_model.coach_id),
3034
coachee_id: Set(coaching_relationship_model.coachee_id),
35+
slug: Set(slug),
3136
created_at: Set(now.into()),
3237
updated_at: Set(now.into()),
3338
..Default::default()

entity_api/src/lib.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ pub async fn seed_database(db: &DatabaseConnection) {
3131

3232
let _admin_user: users::ActiveModel = users::ActiveModel {
3333
email: Set("[email protected]".to_owned()),
34-
first_name: Set(Some("Admin".to_owned())),
35-
last_name: Set(Some("User".to_owned())),
34+
first_name: Set("Admin".to_owned()),
35+
last_name: Set("User".to_owned()),
3636
display_name: Set(Some("Admin User".to_owned())),
3737
password: Set(generate_hash("dLxNxnjn&b!2sqkwFbb4s8jX")),
3838
github_username: Set(None),
@@ -47,8 +47,8 @@ pub async fn seed_database(db: &DatabaseConnection) {
4747

4848
let jim_hodapp: users::ActiveModel = users::ActiveModel {
4949
email: Set("[email protected]".to_owned()),
50-
first_name: Set(Some("Jim".to_owned())),
51-
last_name: Set(Some("Hodapp".to_owned())),
50+
first_name: Set("Jim".to_owned()),
51+
last_name: Set("Hodapp".to_owned()),
5252
display_name: Set(Some("Jim H".to_owned())),
5353
password: Set(generate_hash("password")),
5454
github_username: Set(Some("jhodapp".to_owned())),
@@ -63,8 +63,8 @@ pub async fn seed_database(db: &DatabaseConnection) {
6363

6464
let caleb_bourg: users::ActiveModel = users::ActiveModel {
6565
email: Set("[email protected]".to_owned()),
66-
first_name: Set(Some("Caleb".to_owned())),
67-
last_name: Set(Some("Bourg".to_owned())),
66+
first_name: Set("Caleb".to_owned()),
67+
last_name: Set("Bourg".to_owned()),
6868
display_name: Set(Some("cbourg2".to_owned())),
6969
password: Set(generate_hash("password")),
7070
github_username: Set(Some("calebbourg".to_owned())),
@@ -79,8 +79,8 @@ pub async fn seed_database(db: &DatabaseConnection) {
7979

8080
let other_user: users::ActiveModel = users::ActiveModel {
8181
email: Set("[email protected]".to_owned()),
82-
first_name: Set(Some("Other".to_owned())),
83-
last_name: Set(Some("User".to_owned())),
82+
first_name: Set("Other".to_owned()),
83+
last_name: Set("User".to_owned()),
8484
display_name: Set(Some("Other U.".to_owned())),
8585
password: Set(generate_hash("password")),
8686
github_username: Set(None),

entity_api/src/organization.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use sea_orm::{
66
entity::prelude::*, sea_query, ActiveValue::Set, ActiveValue::Unchanged, DatabaseConnection,
77
JoinType, QuerySelect, TryIntoModel,
88
};
9+
use slugify::slugify;
910
use std::collections::HashMap;
1011

1112
use log::*;
@@ -17,10 +18,12 @@ pub async fn create(db: &DatabaseConnection, organization_model: Model) -> Resul
1718
);
1819

1920
let now = Utc::now();
21+
let name = organization_model.name;
2022

2123
let organization_active_model: ActiveModel = ActiveModel {
2224
logo: Set(organization_model.logo),
23-
name: Set(organization_model.name),
25+
name: Set(name.clone()),
26+
slug: Set(slugify!(name.as_str())),
2427
created_at: Set(now.into()),
2528
updated_at: Set(now.into()),
2629
..Default::default()

web/src/router.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -453,8 +453,8 @@ mod organization_endpoints_tests {
453453
Ok(users::Model {
454454
id: Id::new_v4(),
455455
email: "[email protected]".to_string(),
456-
first_name: Some("test".to_string()),
457-
last_name: Some("login".to_string()),
456+
first_name: "test".to_string(),
457+
last_name: "login".to_string(),
458458
display_name: Some("test login".to_string()),
459459
password: generate_hash("password2").to_owned(),
460460
github_username: None,

0 commit comments

Comments
 (0)