Add hashtag extraction and management in CreatePost and EditPost logic; improve error handling
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
This commit is contained in:
@@ -43,6 +43,13 @@ func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.Crea
|
||||
return nil, fmt.Errorf("invalid content format")
|
||||
}
|
||||
|
||||
// Extract all hashtags from content
|
||||
hashtags, err := utils.ExtractHashtagsFromContent(req.Content)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to parse hashtags from content: %v", err)
|
||||
return nil, fmt.Errorf("invalid content format")
|
||||
}
|
||||
|
||||
// Migrate cover image from tmpfiles to files if provided
|
||||
var coverID sql.NullInt64
|
||||
if req.CoverImageKey != "" {
|
||||
@@ -72,6 +79,36 @@ func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.Crea
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create or get hashtag IDs and link them to the post
|
||||
for _, tagName := range hashtags {
|
||||
var tagID int64
|
||||
|
||||
// Try to get existing hashtag
|
||||
getTagQuery := `SELECT id FROM hashtags WHERE name = $1`
|
||||
err := tx.QueryRowContext(l.ctx, getTagQuery, tagName).Scan(&tagID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
// Hashtag doesn't exist, create it
|
||||
createTagQuery := `INSERT INTO hashtags (name) VALUES ($1) RETURNING id`
|
||||
err = tx.QueryRowContext(l.ctx, createTagQuery, tagName).Scan(&tagID)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to create hashtag %s: %v", tagName, err)
|
||||
continue
|
||||
}
|
||||
} else if err != nil {
|
||||
l.Errorf("Failed to get hashtag %s: %v", tagName, err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Link hashtag to post
|
||||
linkQuery := `INSERT INTO post_hashtags (post_id, hashtag_id) VALUES ($1, $2) ON CONFLICT DO NOTHING`
|
||||
_, err = tx.ExecContext(l.ctx, linkQuery, postID, tagID)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to link hashtag %s to post: %v", tagName, err)
|
||||
// Continue with other hashtags
|
||||
}
|
||||
}
|
||||
|
||||
// Commit transaction
|
||||
if err = tx.Commit(); err != nil {
|
||||
l.Errorf("Failed to commit transaction: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user