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:
@@ -1,42 +1,56 @@
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DbConn, DbErr, EntityTrait, JoinType, QueryFilter, QueryOrder,
|
||||
QuerySelect, RelationTrait, Set,
|
||||
prelude::Uuid, ActiveModelTrait, ColumnTrait, DbConn, DbErr, EntityTrait, JoinType,
|
||||
QueryFilter, QueryOrder, QuerySelect, RelationTrait, Set, TransactionTrait,
|
||||
};
|
||||
|
||||
use models::{
|
||||
domains::{thought, user},
|
||||
domains::{tag, thought, thought_tag, user},
|
||||
params::thought::CreateThoughtParams,
|
||||
schemas::thought::ThoughtWithAuthor,
|
||||
};
|
||||
|
||||
use crate::error::UserError;
|
||||
use crate::{
|
||||
error::UserError,
|
||||
persistence::tag::{find_or_create_tags, link_tags_to_thought, parse_hashtags},
|
||||
};
|
||||
|
||||
pub async fn create_thought(
|
||||
db: &DbConn,
|
||||
author_id: i32,
|
||||
author_id: Uuid,
|
||||
params: CreateThoughtParams,
|
||||
) -> Result<thought::Model, DbErr> {
|
||||
thought::ActiveModel {
|
||||
let txn = db.begin().await?;
|
||||
|
||||
let new_thought = thought::ActiveModel {
|
||||
author_id: Set(author_id),
|
||||
content: Set(params.content),
|
||||
content: Set(params.content.clone()),
|
||||
..Default::default()
|
||||
}
|
||||
.insert(db)
|
||||
.await
|
||||
.insert(&txn)
|
||||
.await?;
|
||||
|
||||
let tag_names = parse_hashtags(¶ms.content);
|
||||
if !tag_names.is_empty() {
|
||||
let tags = find_or_create_tags(&txn, tag_names).await?;
|
||||
link_tags_to_thought(&txn, new_thought.id, tags).await?;
|
||||
}
|
||||
|
||||
txn.commit().await?;
|
||||
Ok(new_thought)
|
||||
}
|
||||
|
||||
pub async fn get_thought(db: &DbConn, thought_id: i32) -> Result<Option<thought::Model>, DbErr> {
|
||||
pub async fn get_thought(db: &DbConn, thought_id: Uuid) -> Result<Option<thought::Model>, DbErr> {
|
||||
thought::Entity::find_by_id(thought_id).one(db).await
|
||||
}
|
||||
|
||||
pub async fn delete_thought(db: &DbConn, thought_id: i32) -> Result<(), DbErr> {
|
||||
pub async fn delete_thought(db: &DbConn, thought_id: Uuid) -> Result<(), DbErr> {
|
||||
thought::Entity::delete_by_id(thought_id).exec(db).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn get_thoughts_by_user(
|
||||
db: &DbConn,
|
||||
user_id: i32,
|
||||
user_id: Uuid,
|
||||
) -> Result<Vec<ThoughtWithAuthor>, DbErr> {
|
||||
thought::Entity::find()
|
||||
.select_only()
|
||||
@@ -55,7 +69,7 @@ pub async fn get_thoughts_by_user(
|
||||
|
||||
pub async fn get_feed_for_user(
|
||||
db: &DbConn,
|
||||
followed_ids: Vec<i32>,
|
||||
followed_ids: Vec<Uuid>,
|
||||
) -> Result<Vec<ThoughtWithAuthor>, UserError> {
|
||||
if followed_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
@@ -76,3 +90,24 @@ pub async fn get_feed_for_user(
|
||||
.await
|
||||
.map_err(|e| UserError::Internal(e.to_string()))
|
||||
}
|
||||
|
||||
pub async fn get_thoughts_by_tag_name(
|
||||
db: &DbConn,
|
||||
tag_name: &str,
|
||||
) -> Result<Vec<ThoughtWithAuthor>, DbErr> {
|
||||
thought::Entity::find()
|
||||
.select_only()
|
||||
.column(thought::Column::Id)
|
||||
.column(thought::Column::Content)
|
||||
.column(thought::Column::CreatedAt)
|
||||
.column(thought::Column::AuthorId)
|
||||
.column_as(user::Column::Username, "author_username")
|
||||
.join(JoinType::InnerJoin, thought::Relation::User.def())
|
||||
.join(JoinType::InnerJoin, thought::Relation::ThoughtTag.def())
|
||||
.join(JoinType::InnerJoin, thought_tag::Relation::Tag.def())
|
||||
.filter(tag::Column::Name.eq(tag_name.to_lowercase()))
|
||||
.order_by_desc(thought::Column::CreatedAt)
|
||||
.into_model::<ThoughtWithAuthor>()
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
Reference in New Issue
Block a user