All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
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
|
|
}
|