All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
161 lines
4.7 KiB
Go
161 lines
4.7 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
|
"git.cialloo.com/CiallooWeb/Blog/app/internal/utils"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type EditPostLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// Edit an existing blog post
|
|
func NewEditPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditPostLogic {
|
|
return &EditPostLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *EditPostLogic) EditPost(req *types.EditPostReq) (resp *types.EditPostResp, 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()
|
|
|
|
// Check if post exists and get old cover_id
|
|
var oldCoverID sql.NullInt64
|
|
checkQuery := `SELECT cover_id FROM posts WHERE id = $1`
|
|
err = tx.QueryRowContext(l.ctx, checkQuery, req.PostId).Scan(&oldCoverID)
|
|
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
|
|
}
|
|
|
|
// Extract all image file keys from content
|
|
imageKeys, err := utils.ExtractImageKeysFromContent(req.Content)
|
|
if err != nil {
|
|
l.Errorf("Failed to parse 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 != "" {
|
|
fileID, err := l.migrateFileFromTmpToFiles(tx, req.CoverImageKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
coverID.Int64 = fileID
|
|
coverID.Valid = true
|
|
|
|
// Update post with new cover
|
|
updateQuery := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4`
|
|
_, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, coverID, req.PostId)
|
|
if err != nil {
|
|
l.Errorf("Failed to update post: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Delete old cover file if exists and different from new one
|
|
if oldCoverID.Valid && oldCoverID.Int64 != coverID.Int64 {
|
|
deleteOldFileQuery := `DELETE FROM files WHERE id = $1`
|
|
_, err = tx.ExecContext(l.ctx, deleteOldFileQuery, oldCoverID.Int64)
|
|
if err != nil {
|
|
l.Errorf("Failed to delete old cover file: %v", err)
|
|
// Don't fail the whole operation for this
|
|
}
|
|
}
|
|
} else {
|
|
// No new cover image - just update the post
|
|
updateQuery := `UPDATE posts SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3`
|
|
_, err = tx.ExecContext(l.ctx, updateQuery, req.Title, req.Content, req.PostId)
|
|
if err != nil {
|
|
l.Errorf("Failed to update post: %v", err)
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
// Migrate all content images from tmpfiles to files
|
|
for _, imageKey := range imageKeys {
|
|
_, err := l.migrateFileFromTmpToFiles(tx, imageKey)
|
|
if err != nil {
|
|
l.Errorf("Failed to migrate content image %s: %v", imageKey, err)
|
|
// Continue with other images, don't fail the whole operation
|
|
}
|
|
}
|
|
|
|
// Commit transaction
|
|
if err = tx.Commit(); err != nil {
|
|
l.Errorf("Failed to commit transaction: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &types.EditPostResp{}, nil
|
|
}
|
|
|
|
// migrateFileFromTmpToFiles moves a file from tmpfiles to files table
|
|
func (l *EditPostLogic) migrateFileFromTmpToFiles(tx *sql.Tx, fileKey string) (int64, error) {
|
|
// Check if file already exists in files table
|
|
var existingID int64
|
|
checkQuery := `SELECT id FROM files WHERE file_key = $1`
|
|
err := tx.QueryRowContext(l.ctx, checkQuery, fileKey).Scan(&existingID)
|
|
if err == nil {
|
|
// File already exists, return its ID
|
|
return existingID, nil
|
|
}
|
|
if err != sql.ErrNoRows {
|
|
l.Errorf("Failed to check existing file: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Verify file exists in tmpfiles table
|
|
var fileName string
|
|
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
|
err = tx.QueryRowContext(l.ctx, tmpQuery, fileKey).Scan(&fileName)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
l.Errorf("File not found in tmpfiles with key: %s", fileKey)
|
|
return 0, fmt.Errorf("file not found")
|
|
}
|
|
l.Errorf("Failed to get tmpfile: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Insert into files table
|
|
var fileID int64
|
|
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
|
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, fileKey).Scan(&fileID)
|
|
if err != nil {
|
|
l.Errorf("Failed to insert file record: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
// Delete from tmpfiles
|
|
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
|
_, err = tx.ExecContext(l.ctx, deleteQuery, fileKey)
|
|
if err != nil {
|
|
l.Errorf("Failed to delete tmpfile: %v", err)
|
|
// Don't fail the operation for this
|
|
}
|
|
|
|
return fileID, nil
|
|
}
|