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:
@@ -56,6 +56,13 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe
|
||||
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 new cover image from tmpfiles to files if provided
|
||||
var coverID sql.NullInt64
|
||||
if req.CoverImageKey != "" {
|
||||
@@ -102,6 +109,45 @@ func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostRe
|
||||
}
|
||||
}
|
||||
|
||||
// Update hashtags - remove old ones and add new ones
|
||||
// First, remove all existing hashtag associations for this post
|
||||
deleteHashtagsQuery := `DELETE FROM post_hashtags WHERE post_id = $1`
|
||||
_, err = tx.ExecContext(l.ctx, deleteHashtagsQuery, req.PostId)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to delete old hashtags: %v", err)
|
||||
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, req.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