feat: Refactor user and thought models to use UUIDs instead of integers

- Updated user and thought models to utilize UUIDs for primary keys.
- Modified persistence functions to accommodate UUIDs for user and thought IDs.
- Implemented tag functionality with new Tag and ThoughtTag models.
- Added migration scripts to create new tables for tags and thought-tag relationships.
- Enhanced thought creation to parse hashtags and link them to thoughts.
- Updated tests to reflect changes in user and thought ID types.
This commit is contained in:
2025-09-06 15:29:38 +02:00
parent c9e99e6f23
commit b83b7acf1c
38 changed files with 638 additions and 107 deletions

View File

@@ -3,10 +3,10 @@ use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "follow")]
pub struct Model {
#[sea_orm(primary_key)]
pub follower_id: i32,
#[sea_orm(primary_key)]
pub followed_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub follower_id: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub followed_id: Uuid,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
@@ -29,4 +29,10 @@ pub enum Relation {
Followed,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::Follower.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -3,5 +3,8 @@
pub mod prelude;
pub mod follow;
pub mod tag;
pub mod thought;
pub mod thought_tag;
pub mod top_friends;
pub mod user;

View File

@@ -1,5 +1,8 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.0.0
pub use super::follow::Entity as Follow;
pub use super::tag::Entity as Tag;
pub use super::thought::Entity as Thought;
pub use super::thought_tag::Entity as ThoughtTag;
pub use super::top_friends::Entity as TopFriends;
pub use super::user::Entity as User;

View File

@@ -0,0 +1,27 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "tag")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub name: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::thought_tag::Entity")]
ThoughtTag,
}
impl Related<super::thought::Entity> for Entity {
fn to() -> RelationDef {
super::thought_tag::Relation::Thought.def()
}
fn via() -> Option<RelationDef> {
Some(super::thought_tag::Relation::Tag.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -3,9 +3,9 @@ use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "thought")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub author_id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
pub author_id: Uuid,
pub content: String,
pub created_at: DateTimeWithTimeZone,
}
@@ -20,6 +20,9 @@ pub enum Relation {
on_delete = "Cascade"
)]
User,
#[sea_orm(has_many = "super::thought_tag::Entity")]
ThoughtTag,
}
impl Related<super::user::Entity> for Entity {
@@ -28,4 +31,13 @@ impl Related<super::user::Entity> for Entity {
}
}
impl Related<super::tag::Entity> for Entity {
fn to() -> RelationDef {
super::thought_tag::Relation::Tag.def()
}
fn via() -> Option<RelationDef> {
Some(super::thought_tag::Relation::Thought.def().rev())
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,40 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "thought_tag")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub thought_id: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub tag_id: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::thought::Entity",
from = "Column::ThoughtId",
to = "super::thought::Column::Id"
)]
Thought,
#[sea_orm(
belongs_to = "super::tag::Entity",
from = "Column::TagId",
to = "super::tag::Column::Id"
)]
Tag,
}
impl Related<super::thought::Entity> for Entity {
fn to() -> RelationDef {
Relation::Thought.def()
}
}
impl Related<super::tag::Entity> for Entity {
fn to() -> RelationDef {
Relation::Tag.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -0,0 +1,35 @@
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "top_friends")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub user_id: Uuid,
#[sea_orm(primary_key, auto_increment = false)]
pub friend_id: Uuid,
pub position: i16,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
to = "super::user::Column::Id"
)]
User,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::FriendId",
to = "super::user::Column::Id"
)]
Friend,
}
impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@@ -5,8 +5,8 @@ use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "user")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub id: Uuid,
#[sea_orm(unique)]
pub username: String,
pub password_hash: Option<String>,
@@ -22,6 +22,12 @@ pub struct Model {
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::thought::Entity")]
Thought,
#[sea_orm(has_many = "super::top_friends::Entity")]
TopFriends,
}
impl ActiveModelBehavior for ActiveModel {}