64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package socialnetworkservicelogic
|
|
|
|
import (
|
|
"app-cloudep-tweeting-service/internal/domain"
|
|
"context"
|
|
|
|
ers "code.30cm.net/digimon/library-go/errs"
|
|
|
|
"app-cloudep-tweeting-service/gen_result/pb/tweeting"
|
|
"app-cloudep-tweeting-service/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type GetFollowerCountLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewGetFollowerCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetFollowerCountLogic {
|
|
return &GetFollowerCountLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
type getFollowCountReq struct {
|
|
UID string `validate:"required"`
|
|
}
|
|
|
|
// GetFollowerCount 取得跟隨者數量
|
|
func (l *GetFollowerCountLogic) GetFollowerCount(in *tweeting.FollowCountReq) (*tweeting.FollowCountResp, error) {
|
|
// 驗證資料
|
|
if err := l.svcCtx.Validate.ValidateAll(&getFollowCountReq{
|
|
UID: in.Uid,
|
|
}); err != nil {
|
|
// 錯誤代碼 05-011-00
|
|
return nil, ers.InvalidFormat(err.Error())
|
|
}
|
|
|
|
followerCount, err := l.svcCtx.SocialNetworkRepository.GetFollowerCount(l.ctx, in.GetUid())
|
|
if err != nil {
|
|
// 錯誤代碼 05-021-32
|
|
e := domain.CommentErrorL(
|
|
domain.GetFollowerCountErrorCode,
|
|
logx.WithContext(l.ctx),
|
|
[]logx.LogField{
|
|
{Key: "req", Value: in},
|
|
{Key: "func", Value: "SocialNetworkRepository.GetFollowerCount"},
|
|
{Key: "err", Value: err},
|
|
},
|
|
"failed to count follower").Wrap(err)
|
|
|
|
return nil, e
|
|
}
|
|
|
|
return &tweeting.FollowCountResp{
|
|
Uid: in.GetUid(),
|
|
Total: followerCount,
|
|
}, nil
|
|
}
|