Implement CRUD operations for blog posts with Create, Edit, and Delete handlers
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s

This commit is contained in:
2025-10-25 11:34:59 +08:00
parent 166382ffa8
commit b05f797eb1
9 changed files with 371 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
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.Cover_image_key != "" {
query := `SELECT id FROM files WHERE file_key = $1`
err := l.svcCtx.DB.QueryRowContext(l.ctx, query, req.Cover_image_key).Scan(&coverID.Int64)
if err != nil {
if err == sql.ErrNoRows {
l.Errorf("Cover image file not found with key: %s", req.Cover_image_key)
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.Post_id)
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.Post_id)
return nil, fmt.Errorf("post not found")
}
return &types.EditPostResp{}, nil
}