45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package grpc
|
|
|
|
import (
|
|
"context"
|
|
|
|
walletv1 "hyapp/api/proto/wallet/v1"
|
|
"hyapp/pkg/xerr"
|
|
"hyapp/services/wallet-service/internal/domain/ledger"
|
|
walletservice "hyapp/services/wallet-service/internal/service/wallet"
|
|
)
|
|
|
|
// Server 把 wallet-service 用例层适配为 gRPC 接口。
|
|
type Server struct {
|
|
walletv1.UnimplementedWalletServiceServer
|
|
|
|
svc *walletservice.Service
|
|
}
|
|
|
|
// NewServer 创建 wallet gRPC server。
|
|
func NewServer(svc *walletservice.Service) *Server {
|
|
return &Server{svc: svc}
|
|
}
|
|
|
|
// DebitGift 只负责 protobuf 转换和错误映射,扣费规则在 service/repository。
|
|
func (s *Server) DebitGift(ctx context.Context, req *walletv1.DebitGiftRequest) (*walletv1.DebitGiftResponse, error) {
|
|
receipt, err := s.svc.DebitGift(ctx, ledger.DebitGiftCommand{
|
|
CommandID: req.GetCommandId(),
|
|
RoomID: req.GetRoomId(),
|
|
SenderUserID: req.GetSenderUserId(),
|
|
TargetUserID: req.GetTargetUserId(),
|
|
GiftID: req.GetGiftId(),
|
|
GiftCount: req.GetGiftCount(),
|
|
GiftUnitValue: req.GetGiftUnitValue(),
|
|
})
|
|
if err != nil {
|
|
return nil, xerr.ToGRPCError(err)
|
|
}
|
|
|
|
return &walletv1.DebitGiftResponse{
|
|
BillingReceiptId: receipt.BillingReceiptID,
|
|
TotalGiftValue: receipt.TotalGiftValue,
|
|
BalanceAfter: receipt.BalanceAfter,
|
|
}, nil
|
|
}
|