Refactor: Remove deprecated gRPC service files and implement new API structure
- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`. - Added new API server implementations for objectstory, player, and shop services. - Introduced configuration files for new APIs in `etc/*.yaml`. - Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`. - Removed unused user update handler and user API files. - Added utility functions for context management and HTTP header parsing. - Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddWalletTransactionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddWalletTransactionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddWalletTransactionsLogic {
|
||||
return &AddWalletTransactionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------walletTransactions-----------------------
|
||||
func (l *AddWalletTransactionsLogic) AddWalletTransactions(in *pb.AddWalletTransactionsReq) (*pb.AddWalletTransactionsResp, error) {
|
||||
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
createdAt := time.Now()
|
||||
if in.CreatedAt > 0 {
|
||||
createdAt = time.Unix(in.CreatedAt, 0)
|
||||
}
|
||||
|
||||
searchText := in.SearchText
|
||||
if searchText == "" {
|
||||
searchText = in.Type + " " + in.Description
|
||||
}
|
||||
|
||||
amount, err := decimal.NewFromString(in.GetAmount())
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid amount")
|
||||
}
|
||||
balanceAfter, err := decimal.NewFromString(in.GetBalanceAfter())
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid balanceAfter")
|
||||
}
|
||||
|
||||
tx, err := l.svcCtx.WalletModelsRW.Tx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = tx.WalletTransactions.Create().
|
||||
SetID(strconv.FormatInt(idResp.Id, 10)).
|
||||
SetUserID(in.UserId).
|
||||
SetType(in.Type).
|
||||
SetAmount(amount).
|
||||
SetBalanceAfter(balanceAfter).
|
||||
SetDescription([]string{in.Description}).
|
||||
SetOrderID(in.OrderId).
|
||||
SetCreatedAt(createdAt).
|
||||
SetSearchText(searchText).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.AddWalletTransactionsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/models"
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AddWalletsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewAddWalletsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddWalletsLogic {
|
||||
return &AddWalletsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------wallets-----------------------
|
||||
func (l *AddWalletsLogic) AddWallets(in *pb.AddWalletsReq) (*pb.AddWalletsResp, error) {
|
||||
updatedAt := time.Now()
|
||||
if in.UpdatedAt > 0 {
|
||||
updatedAt = time.Unix(in.UpdatedAt, 0)
|
||||
}
|
||||
|
||||
balance, err := decimal.NewFromString(in.GetBalance())
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid balance")
|
||||
}
|
||||
frozenBalance, err := decimal.NewFromString(in.GetFrozenBalance())
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid frozenBalance")
|
||||
}
|
||||
|
||||
tx, err := l.svcCtx.WalletModelsRW.Tx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = tx.Wallet.Create().
|
||||
SetUserID(in.UserId).
|
||||
SetBalance(balance).
|
||||
SetFrozenBalance(frozenBalance).
|
||||
SetVersion(1).
|
||||
SetUpdatedAt(updatedAt).
|
||||
Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
if models.IsConstraintError(err) {
|
||||
return &pb.AddWalletsResp{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.AddWalletsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DelWalletTransactionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDelWalletTransactionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelWalletTransactionsLogic {
|
||||
return &DelWalletTransactionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DelWalletTransactionsLogic) DelWalletTransactions(in *pb.DelWalletTransactionsReq) (*pb.DelWalletTransactionsResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &pb.DelWalletTransactionsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DelWalletsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewDelWalletsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelWalletsLogic {
|
||||
return &DelWalletsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DelWalletsLogic) DelWallets(in *pb.DelWalletsReq) (*pb.DelWalletsResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &pb.DelWalletsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetWalletTransactionsByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetWalletTransactionsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWalletTransactionsByIdLogic {
|
||||
return &GetWalletTransactionsByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWalletTransactionsByIdLogic) GetWalletTransactionsById(in *pb.GetWalletTransactionsByIdReq) (*pb.GetWalletTransactionsByIdResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &pb.GetWalletTransactionsByIdResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/wallet/rpc/internal/models"
|
||||
"juwan-backend/app/wallet/rpc/internal/models/wallet"
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetWalletsByIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGetWalletsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWalletsByIdLogic {
|
||||
return &GetWalletsByIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWalletsByIdLogic) GetWalletsById(in *pb.GetWalletsByIdReq) (*pb.GetWalletsByIdResp, error) {
|
||||
item, err := l.svcCtx.WalletModelsRO.Wallet.Query().
|
||||
Where(wallet.UserIDEQ(in.Id)).
|
||||
First(l.ctx)
|
||||
if err != nil {
|
||||
if models.IsNotFound(err) {
|
||||
return &pb.GetWalletsByIdResp{}, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updatedAt := int64(0)
|
||||
if !item.UpdatedAt.IsZero() {
|
||||
updatedAt = item.UpdatedAt.Unix()
|
||||
}
|
||||
|
||||
return &pb.GetWalletsByIdResp{
|
||||
Wallets: &pb.Wallets{
|
||||
UserId: item.UserID,
|
||||
Balance: item.Balance.String(),
|
||||
FrozenBalance: item.FrozenBalance.String(),
|
||||
UpdatedAt: updatedAt,
|
||||
Version: int64(item.Version),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/models/predicate"
|
||||
"juwan-backend/app/wallet/rpc/internal/models/wallettransactions"
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchWalletTransactionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchWalletTransactionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchWalletTransactionsLogic {
|
||||
return &SearchWalletTransactionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchWalletTransactionsLogic) SearchWalletTransactions(in *pb.SearchWalletTransactionsReq) (*pb.SearchWalletTransactionsResp, error) {
|
||||
if in.Limit <= 0 {
|
||||
in.Limit = 20
|
||||
}
|
||||
|
||||
preds := make([]predicate.WalletTransactions, 0, 8)
|
||||
if in.Id > 0 {
|
||||
preds = append(preds, wallettransactions.IDEQ(strconv.FormatInt(in.Id, 10)))
|
||||
}
|
||||
if in.UserId != nil {
|
||||
preds = append(preds, wallettransactions.UserIDEQ(in.GetUserId()))
|
||||
}
|
||||
if in.Type != nil && in.GetType() != "" {
|
||||
preds = append(preds, wallettransactions.TypeEQ(in.GetType()))
|
||||
}
|
||||
if in.Amount != nil {
|
||||
amount, perr := decimal.NewFromString(in.GetAmount())
|
||||
if perr != nil {
|
||||
return nil, errors.New("invalid amount")
|
||||
}
|
||||
preds = append(preds, wallettransactions.AmountEQ(amount))
|
||||
}
|
||||
if in.BalanceAfter != nil {
|
||||
balanceAfter, perr := decimal.NewFromString(in.GetBalanceAfter())
|
||||
if perr != nil {
|
||||
return nil, errors.New("invalid balanceAfter")
|
||||
}
|
||||
preds = append(preds, wallettransactions.BalanceAfterEQ(balanceAfter))
|
||||
}
|
||||
if in.OrderId != nil {
|
||||
preds = append(preds, wallettransactions.OrderIDEQ(in.GetOrderId()))
|
||||
}
|
||||
if in.CreatedAt != nil && in.GetCreatedAt() > 0 {
|
||||
preds = append(preds, wallettransactions.CreatedAtGTE(time.Unix(in.GetCreatedAt(), 0)))
|
||||
}
|
||||
if in.SearchText != nil && in.GetSearchText() != "" {
|
||||
preds = append(preds, wallettransactions.SearchTextContainsFold(in.GetSearchText()))
|
||||
}
|
||||
|
||||
query := l.svcCtx.WalletModelsRO.WalletTransactions.Query()
|
||||
if len(preds) > 0 {
|
||||
query = query.Where(wallettransactions.And(preds...))
|
||||
}
|
||||
|
||||
list, err := query.
|
||||
Offset(int(in.Page * in.Limit)).
|
||||
Limit(int(in.Limit)).
|
||||
All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]*pb.WalletTransactions, 0, len(list))
|
||||
for _, item := range list {
|
||||
id, _ := strconv.ParseInt(item.ID, 10, 64)
|
||||
description := ""
|
||||
if len(item.Description) > 0 {
|
||||
description = item.Description[0]
|
||||
}
|
||||
result = append(result, &pb.WalletTransactions{
|
||||
Id: id,
|
||||
UserId: item.UserID,
|
||||
Type: item.Type,
|
||||
Amount: item.Amount.String(),
|
||||
BalanceAfter: item.BalanceAfter.String(),
|
||||
Description: description,
|
||||
OrderId: item.OrderID,
|
||||
CreatedAt: item.CreatedAt.Unix(),
|
||||
SearchText: item.SearchText,
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.SearchWalletTransactionsResp{WalletTransactions: result}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SearchWalletsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewSearchWalletsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchWalletsLogic {
|
||||
return &SearchWalletsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SearchWalletsLogic) SearchWallets(in *pb.SearchWalletsReq) (*pb.SearchWalletsResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &pb.SearchWalletsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateWalletTransactionsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateWalletTransactionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWalletTransactionsLogic {
|
||||
return &UpdateWalletTransactionsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateWalletTransactionsLogic) UpdateWalletTransactions(in *pb.UpdateWalletTransactionsReq) (*pb.UpdateWalletTransactionsResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &pb.UpdateWalletTransactionsResp{}, nil
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/wallet/rpc/internal/models/wallet"
|
||||
"juwan-backend/app/wallet/rpc/internal/svc"
|
||||
"juwan-backend/app/wallet/rpc/pb"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateWalletsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewUpdateWalletsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWalletsLogic {
|
||||
return &UpdateWalletsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateWalletsLogic) UpdateWallets(in *pb.UpdateWalletsReq) (*pb.UpdateWalletsResp, error) {
|
||||
if in.Version == nil {
|
||||
return nil, errors.New("version is required")
|
||||
}
|
||||
|
||||
tx, err := l.svcCtx.WalletModelsRW.Tx(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
updater := tx.Wallet.Update().
|
||||
Where(wallet.UserIDEQ(in.UserId), wallet.VersionEQ(int(in.GetVersion()))).
|
||||
AddVersion(1)
|
||||
|
||||
if in.Balance != nil {
|
||||
parsedBalance, perr := decimal.NewFromString(in.GetBalance())
|
||||
if perr != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, errors.New("invalid balance")
|
||||
}
|
||||
updater = updater.SetBalance(parsedBalance)
|
||||
}
|
||||
if in.FrozenBalance != nil {
|
||||
parsedFrozenBalance, perr := decimal.NewFromString(in.GetFrozenBalance())
|
||||
if perr != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, errors.New("invalid frozenBalance")
|
||||
}
|
||||
updater = updater.SetFrozenBalance(parsedFrozenBalance)
|
||||
}
|
||||
if in.UpdatedAt != nil && in.GetUpdatedAt() > 0 {
|
||||
updater = updater.SetUpdatedAt(time.Unix(in.GetUpdatedAt(), 0))
|
||||
} else {
|
||||
updater = updater.SetUpdatedAt(time.Now())
|
||||
}
|
||||
|
||||
affected, err := updater.Save(l.ctx)
|
||||
if err != nil {
|
||||
_ = tx.Rollback()
|
||||
return nil, err
|
||||
}
|
||||
if affected == 0 {
|
||||
exist, qerr := tx.Wallet.Query().Where(wallet.UserIDEQ(in.UserId)).Exist(l.ctx)
|
||||
_ = tx.Rollback()
|
||||
if qerr != nil {
|
||||
return nil, qerr
|
||||
}
|
||||
if !exist {
|
||||
return nil, errors.New("wallet not found")
|
||||
}
|
||||
return nil, errors.New("wallet update conflict")
|
||||
}
|
||||
|
||||
if err = tx.Commit(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.UpdateWalletsResp{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user