对接
This commit is contained in:
@@ -2,10 +2,14 @@ package gift
|
||||
|
||||
import (
|
||||
"context"
|
||||
kratoserrors "github.com/go-kratos/kratos/v3/errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
kratoserrors "github.com/go-kratos/kratos/v3/errors"
|
||||
|
||||
bizlive "kra/internal/biz/live"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -17,6 +21,24 @@ var (
|
||||
ErrSendNotAllowed = kratoserrors.BadRequest("GIFT_SEND_NOT_ALLOWED", "gift cannot be sent")
|
||||
)
|
||||
|
||||
// InsufficientBalanceError preserves the wallet snapshot needed by clients
|
||||
// to decide whether to open the recharge flow. It unwraps to the stable
|
||||
// ErrInsufficientBalance reason so existing errors.Is checks keep working.
|
||||
type InsufficientBalanceError struct {
|
||||
Required int64
|
||||
Balance int64
|
||||
Missing int64
|
||||
}
|
||||
|
||||
func (e *InsufficientBalanceError) Error() string {
|
||||
if e == nil {
|
||||
return ErrInsufficientBalance.Error()
|
||||
}
|
||||
return fmt.Sprintf("insufficient diamond balance: need %d, have %d, missing %d", e.Required, e.Balance, e.Missing)
|
||||
}
|
||||
|
||||
func (e *InsufficientBalanceError) Unwrap() error { return ErrInsufficientBalance }
|
||||
|
||||
type Gift struct {
|
||||
ID uint
|
||||
Name, Icon, Description string
|
||||
@@ -72,13 +94,21 @@ type Repo interface {
|
||||
type Notifier interface {
|
||||
GiftSent(context.Context, *Send)
|
||||
}
|
||||
type roomAvailability interface {
|
||||
IsLive(context.Context, uint) (bool, error)
|
||||
}
|
||||
type Usecase struct {
|
||||
repo Repo
|
||||
notifier Notifier
|
||||
rooms roomAvailability
|
||||
}
|
||||
|
||||
func NewUsecase(repo Repo, notifier Notifier) *Usecase {
|
||||
return &Usecase{repo: repo, notifier: notifier}
|
||||
func NewUsecase(repo Repo, notifier Notifier, rooms *bizlive.RoomUsecase) *Usecase {
|
||||
return newUsecase(repo, notifier, rooms)
|
||||
}
|
||||
|
||||
func newUsecase(repo Repo, notifier Notifier, rooms roomAvailability) *Usecase {
|
||||
return &Usecase{repo: repo, notifier: notifier, rooms: rooms}
|
||||
}
|
||||
func (uc *Usecase) List(ctx context.Context, f Filter, o ListOptions) ([]*Gift, int64, error) {
|
||||
f.Name = strings.TrimSpace(f.Name)
|
||||
@@ -115,6 +145,13 @@ func (uc *Usecase) Send(ctx context.Context, userID, roomID, giftID uint, quanti
|
||||
if userID == 0 || roomID == 0 || giftID == 0 || quantity < 1 || quantity > 9999 {
|
||||
return nil, ErrSendNotAllowed
|
||||
}
|
||||
if uc.rooms == nil {
|
||||
return nil, ErrSendNotAllowed
|
||||
}
|
||||
live, err := uc.rooms.IsLive(ctx, roomID)
|
||||
if err != nil || !live {
|
||||
return nil, ErrSendNotAllowed
|
||||
}
|
||||
result, err := uc.repo.Send(ctx, userID, roomID, giftID, quantity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user