77 lines
3.1 KiB
Go
77 lines
3.1 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/topfans/backend/pkg/logger"
|
|
pbCommon "github.com/topfans/backend/pkg/proto/common"
|
|
pbUser "github.com/topfans/backend/pkg/proto/user"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type UserServiceClient interface {
|
|
UpdateCrystalBalance(ctx context.Context, userID, starID int64, delta int64) (int64, error)
|
|
AddExperience(ctx context.Context, userID, starID int64, delta int64) (int64, error)
|
|
GetFanProfile(ctx context.Context, userID, starID int64) (*pbUser.FanProfile, error)
|
|
}
|
|
|
|
type userServiceClient struct {
|
|
client pbUser.UserSocialService
|
|
}
|
|
|
|
func NewUserServiceClient(client pbUser.UserSocialService) UserServiceClient {
|
|
return &userServiceClient{client: client}
|
|
}
|
|
|
|
func (c *userServiceClient) UpdateCrystalBalance(ctx context.Context, userID, starID int64, delta int64) (int64, error) {
|
|
logger.Logger.Debug("Calling UserService.UpdateCrystalBalance",
|
|
zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int64("delta", delta))
|
|
resp, err := c.client.UpdateCrystalBalance(ctx, &pbUser.UpdateCrystalBalanceRequest{
|
|
UserId: userID, StarId: starID, Delta: delta,
|
|
})
|
|
if err != nil {
|
|
logger.Logger.Error("UserService.UpdateCrystalBalance failed", zap.Error(err))
|
|
return 0, err
|
|
}
|
|
if resp.Base.Code != pbCommon.StatusCode_STATUS_OK {
|
|
logger.Logger.Warn("UpdateCrystalBalance non-zero code", zap.Int32("code", int32(resp.Base.Code)))
|
|
return 0, fmt.Errorf("UpdateCrystalBalance failed with code: %d", resp.Base.Code)
|
|
}
|
|
return resp.NewBalance, nil
|
|
}
|
|
|
|
func (c *userServiceClient) AddExperience(ctx context.Context, userID, starID int64, delta int64) (int64, error) {
|
|
logger.Logger.Debug("Calling UserService.AddExperience",
|
|
zap.Int64("user_id", userID), zap.Int64("star_id", starID), zap.Int64("delta", delta))
|
|
resp, err := c.client.AddExperience(ctx, &pbUser.AddExperienceRequest{
|
|
UserId: userID, StarId: starID, Delta: delta,
|
|
})
|
|
if err != nil {
|
|
logger.Logger.Error("UserService.AddExperience failed", zap.Error(err))
|
|
return 0, err
|
|
}
|
|
if resp.Base.Code != pbCommon.StatusCode_STATUS_OK {
|
|
logger.Logger.Warn("AddExperience non-zero code", zap.Int32("code", int32(resp.Base.Code)))
|
|
return 0, fmt.Errorf("AddExperience failed with code: %d", resp.Base.Code)
|
|
}
|
|
return resp.NewExperience, nil
|
|
}
|
|
|
|
func (c *userServiceClient) GetFanProfile(ctx context.Context, userID, starID int64) (*pbUser.FanProfile, error) {
|
|
logger.Logger.Debug("Calling UserService.GetFanProfile",
|
|
zap.Int64("user_id", userID), zap.Int64("star_id", starID))
|
|
resp, err := c.client.GetFanProfile(ctx, &pbUser.GetFanProfileRequest{
|
|
UserId: userID,
|
|
StarId: starID,
|
|
})
|
|
if err != nil {
|
|
logger.Logger.Error("UserService.GetFanProfile failed", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
if resp.Base.Code != pbCommon.StatusCode_STATUS_OK {
|
|
logger.Logger.Warn("GetFanProfile non-zero code", zap.Int32("code", int32(resp.Base.Code)))
|
|
return nil, fmt.Errorf("GetFanProfile failed with code: %d", resp.Base.Code)
|
|
}
|
|
return resp.Profile, nil
|
|
}
|