app-cloudep-tweeting-service/internal/logic/postservice/delete_post_logic.go

64 lines
1.6 KiB
Go

package postservicelogic
import (
"app-cloudep-tweeting-service/internal/domain"
"context"
"fmt"
"strings"
ers "code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
"app-cloudep-tweeting-service/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type DeletePostLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDeletePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeletePostLogic {
return &DeletePostLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// DeletePostError 0502103 資料庫錯誤
func DeletePostError(s ...string) *ers.LibError {
return ers.NewError(code.CloudEPTweeting, code.DBError,
domain.DelPostError.ToUint32(),
fmt.Sprintf("%s", strings.Join(s, " ")))
}
// DeletePostErrorL logs error message and returns Error
func DeletePostErrorL(l logx.Logger, filed []logx.LogField, s ...string) *ers.LibError {
e := DeletePostError(s...)
l.WithCallerSkip(1).WithFields(filed...).Error(e.Error())
return e
}
// DeletePost 刪除貼文
func (l *DeletePostLogic) DeletePost(in *tweeting.DeletePostsReq) (*tweeting.OKResp, error) {
_, err := l.svcCtx.PostModel.DeleteMany(l.ctx, in.GetPostId()...)
if err != nil {
e := DeletePostErrorL(
logx.WithContext(l.ctx),
[]logx.LogField{
{Key: "req", Value: in},
{Key: "func", Value: "PostModel.DeleteMany"},
{Key: "err", Value: err},
},
"failed to del post").Wrap(err)
return nil, e
}
return &tweeting.OKResp{}, nil
}