Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slugify organization names #107

Merged
merged 5 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions entity/src/coaching_relationships.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub struct Model {
pub coach_id: Id,
pub coachee_id: Id,
#[serde(skip_deserializing)]
// TODO we need to make sure this is unique in the scope of an organization_id.
// I did some research and there are two ways to do this:
// 1. Create a unique constraint at the database level.
// 2. Add application logic (probably in entity_api) to make the check.
// We'll need to add a migration for that eventually.
#[sea_orm(unique)]
pub slug: String,
#[serde(skip_deserializing)]
#[schema(value_type = String, format = DateTime)] // Applies to OpenAPI schema
Expand Down
1 change: 1 addition & 0 deletions entity/src/organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct Model {
pub name: String,
pub logo: Option<String>,
#[serde(skip_deserializing)]
#[sea_orm(unique)]
pub slug: String,
#[serde(skip_deserializing)]
#[schema(value_type = String, format = DateTime)] // Applies to OpenAPI schema
Expand Down
4 changes: 2 additions & 2 deletions entity/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ pub struct Model {
pub id: Id,
#[sea_orm(unique)]
pub email: String,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub first_name: String,
pub last_name: String,
pub display_name: Option<String>,
#[serde(skip_serializing)]
pub password: String,
Expand Down
1 change: 1 addition & 0 deletions entity_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log = "0.4.22"
axum-login = "0.16.0"
async-trait = "0.1.83"
password-auth = "1.0.0"
slugify = "0.1.0"
sqlx = { version = "0.8.2", features = ["time", "runtime-tokio"] }
sqlx-sqlite = { version = "0.8.2" }
utoipa = { version = "4.2.0", features = ["axum_extras", "uuid"] }
Expand Down
9 changes: 7 additions & 2 deletions entity_api/src/coaching_relationship.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use super::error::{EntityApiErrorKind, Error};
use crate::user;
use chrono::Utc;
use entity::{
coachees, coaches,
coaching_relationships::{self, ActiveModel, Entity, Model},
Id,
};
use log::*;
use sea_orm::{
entity::prelude::*, sea_query::Alias, Condition, DatabaseConnection, FromQueryResult, JoinType,
QuerySelect, QueryTrait, Set,
};
use serde::ser::{Serialize, SerializeStruct, Serializer};

use log::*;
use slugify::slugify;

pub async fn create(
db: &DatabaseConnection,
Expand All @@ -23,11 +24,15 @@ pub async fn create(
);

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

let coaching_relationship_active_model: ActiveModel = ActiveModel {
organization_id: Set(coaching_relationship_model.organization_id),
coach_id: Set(coaching_relationship_model.coach_id),
coachee_id: Set(coaching_relationship_model.coachee_id),
slug: Set(slug),
created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
Expand Down
16 changes: 8 additions & 8 deletions entity_api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub async fn seed_database(db: &DatabaseConnection) {

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

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

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

let other_user: users::ActiveModel = users::ActiveModel {
email: Set("[email protected]".to_owned()),
first_name: Set(Some("Other".to_owned())),
last_name: Set(Some("User".to_owned())),
first_name: Set("Other".to_owned()),
last_name: Set("User".to_owned()),
display_name: Set(Some("Other U.".to_owned())),
password: Set(generate_hash("password")),
github_username: Set(None),
Expand Down
5 changes: 4 additions & 1 deletion entity_api/src/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sea_orm::{
entity::prelude::*, sea_query, ActiveValue::Set, ActiveValue::Unchanged, DatabaseConnection,
JoinType, QuerySelect, TryIntoModel,
};
use slugify::slugify;
use std::collections::HashMap;

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

let now = Utc::now();
let name = organization_model.name;

let organization_active_model: ActiveModel = ActiveModel {
logo: Set(organization_model.logo),
name: Set(organization_model.name),
name: Set(name.clone()),
slug: Set(slugify!(name.as_str())),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, so easy! Love it.

created_at: Set(now.into()),
updated_at: Set(now.into()),
..Default::default()
Expand Down
4 changes: 2 additions & 2 deletions web/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ mod organization_endpoints_tests {
Ok(users::Model {
id: Id::new_v4(),
email: "[email protected]".to_string(),
first_name: Some("test".to_string()),
last_name: Some("login".to_string()),
first_name: "test".to_string(),
last_name: "login".to_string(),
display_name: Some("test login".to_string()),
password: generate_hash("password2").to_owned(),
github_username: None,
Expand Down