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
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
This commit is contained in:
49
app/internal/logic/deletepostlogic.go
Normal file
49
app/internal/logic/deletepostlogic.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user