All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 53s
68 lines
1.8 KiB
Go
68 lines
1.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, get the file ID from files table
|
|
if req.CoverImageKey != "" {
|
|
query := `SELECT id FROM files WHERE file_key = $1`
|
|
err := l.svcCtx.DB.QueryRowContext(l.ctx, query, req.CoverImageKey).Scan(&coverID.Int64)
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
l.Errorf("Cover image file not found with key: %s", req.CoverImageKey)
|
|
return nil, fmt.Errorf("cover image not found")
|
|
}
|
|
l.Errorf("Failed to get cover image file: %v", err)
|
|
return nil, err
|
|
}
|
|
coverID.Valid = true
|
|
}
|
|
|
|
// Update post with updated_at timestamp
|
|
query := `UPDATE posts SET title = $1, content = $2, cover_id = $3, updated_at = CURRENT_TIMESTAMP WHERE id = $4`
|
|
result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Title, req.Content, coverID, 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
|
|
}
|