All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 56s
133 lines
3.8 KiB
Go
133 lines
3.8 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"
|
|
|
|
"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) {
|
|
var coverID sql.NullInt64
|
|
|
|
// If cover image key is provided, move it from tmpfiles to files table
|
|
if req.CoverImageKey != "" {
|
|
// 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 the tmpfile record
|
|
var fileName string
|
|
tmpQuery := `SELECT file_name FROM tmpfiles WHERE key = $1`
|
|
err = tx.QueryRowContext(l.ctx, tmpQuery, req.CoverImageKey).Scan(&fileName)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
l.Errorf("Cover image not found in tmpfiles with key: %s", req.CoverImageKey)
|
|
return nil, fmt.Errorf("cover image not found")
|
|
}
|
|
l.Errorf("Failed to get tmpfile: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Get old cover_id to delete later
|
|
var oldCoverID sql.NullInt64
|
|
oldCoverQuery := `SELECT cover_id FROM posts WHERE id = $1`
|
|
err = tx.QueryRowContext(l.ctx, oldCoverQuery, 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 old cover: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Insert into files table
|
|
fileQuery := `INSERT INTO files (file_name, file_key) VALUES ($1, $2) RETURNING id`
|
|
err = tx.QueryRowContext(l.ctx, fileQuery, fileName, req.CoverImageKey).Scan(&coverID.Int64)
|
|
if err != nil {
|
|
l.Errorf("Failed to insert file record: %v", err)
|
|
return nil, err
|
|
}
|
|
coverID.Valid = true
|
|
|
|
// Delete from tmpfiles
|
|
deleteQuery := `DELETE FROM tmpfiles WHERE key = $1`
|
|
_, err = tx.ExecContext(l.ctx, deleteQuery, req.CoverImageKey)
|
|
if err != nil {
|
|
l.Errorf("Failed to delete tmpfile: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
// Update post
|
|
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
|
|
if oldCoverID.Valid {
|
|
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
|
|
}
|
|
}
|
|
|
|
// Commit transaction
|
|
if err = tx.Commit(); err != nil {
|
|
l.Errorf("Failed to commit transaction: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &types.EditPostResp{}, nil
|
|
}
|
|
|
|
// No new cover image - just update the post
|
|
query := `UPDATE posts SET title = $1, content = $2, updated_at = CURRENT_TIMESTAMP WHERE id = $3`
|
|
result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Title, req.Content, req.PostId)
|
|
if err != nil {
|
|
l.Errorf("Failed to update 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
|
|
}
|
|
if rowsAffected == 0 {
|
|
l.Errorf("Post not found with id: %s", req.PostId)
|
|
return nil, fmt.Errorf("post not found")
|
|
}
|
|
|
|
return &types.EditPostResp{}, nil
|
|
}
|