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,59 @@
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 CreatePostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Create a new blog post
func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
return &CreatePostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.CreatePostResp, 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
}
// Insert new post
var postID int64
query := `INSERT INTO posts (title, content, cover_id) VALUES ($1, $2, $3) RETURNING id`
err = l.svcCtx.DB.QueryRowContext(l.ctx, query, req.Title, req.Content, coverID).Scan(&postID)
if err != nil {
l.Errorf("Failed to create post: %v", err)
return nil, err
}
return &types.CreatePostResp{
Post_id: fmt.Sprintf("%d", postID),
}, nil
}

View File

@@ -0,0 +1,49 @@
package logic
import (
"context"
"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 DeletePostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Delete a blog post
func NewDeletePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePostLogic {
return &DeletePostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeletePostLogic) DeletePost(req *types.DeletePostReq) (resp *types.DeletePostResp, err error) {
// Delete post from database
query := `DELETE FROM posts WHERE id = $1`
result, err := l.svcCtx.DB.ExecContext(l.ctx, query, req.Post_id)
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
}
if rowsAffected == 0 {
l.Errorf("Post not found with id: %s", req.Post_id)
return nil, fmt.Errorf("post not found")
}
return &types.DeletePostResp{}, nil
}

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
}