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:
@@ -2,6 +2,7 @@ package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||
@@ -26,23 +27,57 @@ func NewDeletePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
|
||||
}
|
||||
|
||||
func (l *DeletePostLogic) DeletePost(req *types.DeletePostReq) (resp *types.DeletePostResp, err error) {
|
||||
// Start a transaction
|
||||
tx, err := l.svcCtx.DB.BeginTx(l.ctx, nil)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to begin transaction: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
// Get cover_id before deleting the post
|
||||
var coverID sql.NullInt64
|
||||
getCoverQuery := `SELECT cover_id FROM posts WHERE id = $1`
|
||||
err = tx.QueryRowContext(l.ctx, getCoverQuery, req.PostId).Scan(&coverID)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
l.Errorf("Post not found with id: %s", req.PostId)
|
||||
return nil, fmt.Errorf("post not found")
|
||||
}
|
||||
l.Errorf("Failed to get post: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Delete hashtag associations (post_hashtags will be deleted by CASCADE)
|
||||
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 hashtag associations: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Delete post from database
|
||||
query := `DELETE FROM posts WHERE id = $1`
|
||||
result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.PostId)
|
||||
deleteQuery := `DELETE FROM posts WHERE id = $1`
|
||||
_, err = tx.ExecContext(l.ctx, deleteQuery, req.PostId)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to delete post: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check if post exists
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
l.Errorf("Failed to get rows affected: %v", err)
|
||||
return nil, err
|
||||
// Delete cover file if exists
|
||||
if coverID.Valid {
|
||||
deleteFileQuery := `DELETE FROM files WHERE id = $1`
|
||||
_, err = tx.ExecContext(l.ctx, deleteFileQuery, coverID.Int64)
|
||||
if err != nil {
|
||||
l.Errorf("Failed to delete cover file: %v", err)
|
||||
// Don't fail the whole operation for this
|
||||
}
|
||||
}
|
||||
if rowsAffected == 0 {
|
||||
l.Errorf("Post not found with id: %s", req.PostId)
|
||||
return nil, fmt.Errorf("post not found")
|
||||
|
||||
// Commit transaction
|
||||
if err = tx.Commit(); err != nil {
|
||||
l.Errorf("Failed to commit transaction: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &types.DeletePostResp{}, nil
|
||||
|
||||
Reference in New Issue
Block a user