fix: api descript

This commit is contained in:
wwweww
2026-02-28 05:33:16 +08:00
parent 5930fb0dde
commit d2f33b4b96
243 changed files with 37065 additions and 780 deletions
+16
View File
@@ -0,0 +1,16 @@
package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
zrpc.RpcServerConf
SnowflakeRpcConf zrpc.RpcClientConf
DB struct {
Master string
Slaves string
}
CacheConf cache.CacheConf
}
@@ -0,0 +1,64 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/snowflake/rpc/snowflake"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type AddGamesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGamesLogic {
return &AddGamesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// -----------------------games-----------------------
func (l *AddGamesLogic) AddGames(in *pb.AddGamesReq) (*pb.AddGamesResp, error) {
idResp, err := l.svcCtx.Snowflake.NextId(l.ctx, &snowflake.NextIdReq{})
if err != nil {
logx.Errorf("AddGamesLogic.addGames err:%v", err)
return nil, errors.New("create game id failed")
}
_, err = l.svcCtx.GameModelRW.Games.Create().
SetID(idResp.Id).
SetName(in.Name).
SetIcon(in.Icon).
SetCategory(in.Category).
SetSortOrder(int(in.SortOrder)).
Save(l.ctx)
if err != nil {
logx.Errorf("AddGamesLogic.addGames err:%v", err)
return nil, errors.New("add game failed")
}
return &pb.AddGamesResp{}, nil
}
/*
type AddGamesReq struct {
state protoimpl.MessageState `protogen:"open.v1"`
Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` //name
Icon string `protobuf:"bytes,2,opt,name=icon,proto3" json:"icon,omitempty"` //icon
Category string `protobuf:"bytes,3,opt,name=category,proto3" json:"category,omitempty"` //category
SortOrder int64 `protobuf:"varint,4,opt,name=sortOrder,proto3" json:"sortOrder,omitempty"` //sortOrder
IsActive bool `protobuf:"varint,5,opt,name=isActive,proto3" json:"isActive,omitempty"` //isActive
CreatedAt int64 `protobuf:"varint,6,opt,name=createdAt,proto3" json:"createdAt,omitempty"` //createdAt
UpdatedAt int64 `protobuf:"varint,7,opt,name=updatedAt,proto3" json:"updatedAt,omitempty"` //updatedAt
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
*/
@@ -0,0 +1,33 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelGamesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelGamesLogic {
return &DelGamesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelGamesLogic) DelGames(in *pb.DelGamesReq) (*pb.DelGamesResp, error) {
err := l.svcCtx.GameModelRW.Games.DeleteOneID(in.Id).Exec(l.ctx)
if err != nil {
logx.Errorf("delete games failed, %s", err.Error())
return nil, errors.New("delete games failed")
}
return &pb.DelGamesResp{}, nil
}
@@ -0,0 +1,46 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/game/rpc/internal/models/games"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
type GetGamesByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetGamesByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGamesByIdLogic {
return &GetGamesByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetGamesByIdLogic) GetGamesById(in *pb.GetGamesByIdReq) (*pb.GetGamesByIdResp, error) {
game, err := l.svcCtx.GameModelRO.Games.Query().Where(games.IDEQ(in.Id)).First(l.ctx)
if err != nil {
logx.WithContext(l.ctx).Errorf("GetGamesByIdLogic err: %v", err)
return nil, errors.New("get games by id failed")
}
pbGame := pb.Games{}
err = copier.Copy(&pbGame, &game)
if err != nil {
logx.WithContext(l.ctx).Errorf("GetGamesByIdLogic copier err: %v", err)
return nil, errors.New("get games by id failed")
}
pbGame.CreatedAt = game.CreatedAt.Unix()
pbGame.UpdatedAt = game.UpdatedAt.Unix()
return &pb.GetGamesByIdResp{
Games: &pbGame,
}, nil
}
@@ -0,0 +1,107 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/game/rpc/internal/models/games"
"juwan-backend/app/game/rpc/internal/models/predicate"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchGamesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchGamesLogic {
return &SearchGamesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchGamesLogic) SearchGames(in *pb.SearchGamesReq) (*pb.SearchGamesResp, error) {
if in.Limit > 1000 {
return nil, errors.New("limit too large")
}
preds := make([]predicate.Games, 0, 8)
if in.IdOpt != nil {
preds = append(preds, games.IDEQ(*in.IdOpt))
} else if in.Id != 0 {
preds = append(preds, games.IDEQ(in.Id))
}
if in.NameOpt != nil {
if *in.NameOpt != "" {
preds = append(preds, games.NameContainsFold(*in.NameOpt))
}
} else if in.Name != "" {
preds = append(preds, games.NameContainsFold(in.Name))
}
if in.IconOpt != nil {
if *in.IconOpt != "" {
preds = append(preds, games.IconContainsFold(*in.IconOpt))
}
} else if in.Icon != "" {
preds = append(preds, games.IconContainsFold(in.Icon))
}
if in.CategoryOpt != nil {
if *in.CategoryOpt != "" {
preds = append(preds, games.CategoryContainsFold(*in.CategoryOpt))
}
} else if in.Category != "" {
preds = append(preds, games.CategoryContainsFold(in.Category))
}
if in.SortOrderOpt != nil {
preds = append(preds, games.SortOrderEQ(int(*in.SortOrderOpt)))
} else if in.SortOrder != 0 {
preds = append(preds, games.SortOrderEQ(int(in.SortOrder)))
}
if in.IsActiveOpt != nil {
preds = append(preds, games.IsActiveEQ(*in.IsActiveOpt))
} else if in.IsActive {
preds = append(preds, games.IsActiveEQ(true))
}
query := l.svcCtx.GameModelRO.Games.Query()
if len(preds) > 0 {
if in.MatchMode == pb.MatchMode_MATCH_MODE_AND {
query = query.Where(games.And(preds...))
} else {
query = query.Where(games.Or(preds...))
}
}
all, err := query.
Offset(int(in.Page * in.Limit)).
Limit(int(in.Limit)).
All(l.ctx)
if err != nil {
logx.Errorf("search games failed, %s", err.Error())
return nil, errors.New("search games failed")
}
list := make([]*pb.Games, 0, len(all))
for _, v := range all {
temp := &pb.Games{}
err = copier.Copy(temp, &v)
if err != nil {
logx.Errorf("search games failed, %s", err.Error())
continue
}
temp.CreatedAt = v.CreatedAt.Unix()
temp.UpdatedAt = v.UpdatedAt.Unix()
list = append(list, temp)
}
return &pb.SearchGamesResp{
Games: list,
}, nil
}
@@ -0,0 +1,85 @@
package logic
import (
"context"
"errors"
"time"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateGamesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateGamesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGamesLogic {
return &UpdateGamesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateGamesLogic) UpdateGames(in *pb.UpdateGamesReq) (*pb.UpdateGamesResp, error) {
update := l.svcCtx.GameModelRW.Games.UpdateOneID(in.Id)
updated := false
if in.NameOpt != nil {
update.SetNillableName(in.NameOpt)
updated = true
} else if in.Name != "" {
update.SetName(in.Name)
updated = true
}
if in.IconOpt != nil {
update.SetNillableIcon(in.IconOpt)
updated = true
} else if in.Icon != "" {
update.SetIcon(in.Icon)
updated = true
}
if in.CategoryOpt != nil {
update.SetNillableCategory(in.CategoryOpt)
updated = true
} else if in.Category != "" {
update.SetCategory(in.Category)
updated = true
}
if in.SortOrderOpt != nil {
sortOrder := int(*in.SortOrderOpt)
update.SetNillableSortOrder(&sortOrder)
updated = true
} else if in.SortOrder != 0 {
sortOrder := int(in.SortOrder)
update.SetSortOrder(sortOrder)
updated = true
}
if in.IsActiveOpt != nil {
update.SetNillableIsActive(in.IsActiveOpt)
updated = true
} else if in.IsActive {
update.SetIsActive(true)
updated = true
}
if !updated {
return nil, errors.New("no fields to update")
}
update.SetUpdatedAt(time.Now())
if err := update.Exec(l.ctx); err != nil {
logx.WithContext(l.ctx).Errorf("UpdateGamesLogic err: %v", err)
return nil, errors.New("update games failed")
}
return &pb.UpdateGamesResp{}, nil
}
@@ -0,0 +1,32 @@
package schema
import (
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
)
// Games holds the schema definition for the Games entity.
type Games struct {
ent.Schema
}
// Fields of the Games.
func (Games) Fields() []ent.Field {
return []ent.Field{
field.Int64("id").Immutable().Unique(),
field.String("name").MaxLen(100).Unique(),
field.String("icon"),
field.String("category").MaxLen(50),
field.Int("sort_order").Default(0),
field.Bool("is_active").Optional().Default(true),
field.Time("created_at").Default(time.Now).Immutable(),
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now),
}
}
// Edges of the Games.
func (Games) Edges() []ent.Edge {
return nil
}
@@ -0,0 +1,180 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
// Source: game.proto
package server
import (
"context"
"juwan-backend/app/game/rpc/internal/logic"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
)
type GamePublicServer struct {
svcCtx *svc.ServiceContext
pb.UnimplementedGamePublicServer
}
func NewGamePublicServer(svcCtx *svc.ServiceContext) *GamePublicServer {
return &GamePublicServer{
svcCtx: svcCtx,
}
}
// -----------------------games-----------------------
func (s *GamePublicServer) AddGames(ctx context.Context, in *pb.AddGamesReq) (*pb.AddGamesResp, error) {
l := logic.NewAddGamesLogic(ctx, s.svcCtx)
return l.AddGames(in)
}
func (s *GamePublicServer) UpdateGames(ctx context.Context, in *pb.UpdateGamesReq) (*pb.UpdateGamesResp, error) {
l := logic.NewUpdateGamesLogic(ctx, s.svcCtx)
return l.UpdateGames(in)
}
func (s *GamePublicServer) DelGames(ctx context.Context, in *pb.DelGamesReq) (*pb.DelGamesResp, error) {
l := logic.NewDelGamesLogic(ctx, s.svcCtx)
return l.DelGames(in)
}
func (s *GamePublicServer) GetGamesById(ctx context.Context, in *pb.GetGamesByIdReq) (*pb.GetGamesByIdResp, error) {
l := logic.NewGetGamesByIdLogic(ctx, s.svcCtx)
return l.GetGamesById(in)
}
func (s *GamePublicServer) SearchGames(ctx context.Context, in *pb.SearchGamesReq) (*pb.SearchGamesResp, error) {
l := logic.NewSearchGamesLogic(ctx, s.svcCtx)
return l.SearchGames(in)
}
// -----------------------playerServices-----------------------
func (s *GamePublicServer) AddPlayerServices(ctx context.Context, in *pb.AddPlayerServicesReq) (*pb.AddPlayerServicesResp, error) {
l := logic.NewAddPlayerServicesLogic(ctx, s.svcCtx)
return l.AddPlayerServices(in)
}
func (s *GamePublicServer) UpdatePlayerServices(ctx context.Context, in *pb.UpdatePlayerServicesReq) (*pb.UpdatePlayerServicesResp, error) {
l := logic.NewUpdatePlayerServicesLogic(ctx, s.svcCtx)
return l.UpdatePlayerServices(in)
}
func (s *GamePublicServer) DelPlayerServices(ctx context.Context, in *pb.DelPlayerServicesReq) (*pb.DelPlayerServicesResp, error) {
l := logic.NewDelPlayerServicesLogic(ctx, s.svcCtx)
return l.DelPlayerServices(in)
}
func (s *GamePublicServer) GetPlayerServicesById(ctx context.Context, in *pb.GetPlayerServicesByIdReq) (*pb.GetPlayerServicesByIdResp, error) {
l := logic.NewGetPlayerServicesByIdLogic(ctx, s.svcCtx)
return l.GetPlayerServicesById(in)
}
func (s *GamePublicServer) SearchPlayerServices(ctx context.Context, in *pb.SearchPlayerServicesReq) (*pb.SearchPlayerServicesResp, error) {
l := logic.NewSearchPlayerServicesLogic(ctx, s.svcCtx)
return l.SearchPlayerServices(in)
}
// -----------------------players-----------------------
func (s *GamePublicServer) AddPlayers(ctx context.Context, in *pb.AddPlayersReq) (*pb.AddPlayersResp, error) {
l := logic.NewAddPlayersLogic(ctx, s.svcCtx)
return l.AddPlayers(in)
}
func (s *GamePublicServer) UpdatePlayers(ctx context.Context, in *pb.UpdatePlayersReq) (*pb.UpdatePlayersResp, error) {
l := logic.NewUpdatePlayersLogic(ctx, s.svcCtx)
return l.UpdatePlayers(in)
}
func (s *GamePublicServer) DelPlayers(ctx context.Context, in *pb.DelPlayersReq) (*pb.DelPlayersResp, error) {
l := logic.NewDelPlayersLogic(ctx, s.svcCtx)
return l.DelPlayers(in)
}
func (s *GamePublicServer) GetPlayersById(ctx context.Context, in *pb.GetPlayersByIdReq) (*pb.GetPlayersByIdResp, error) {
l := logic.NewGetPlayersByIdLogic(ctx, s.svcCtx)
return l.GetPlayersById(in)
}
func (s *GamePublicServer) SearchPlayers(ctx context.Context, in *pb.SearchPlayersReq) (*pb.SearchPlayersResp, error) {
l := logic.NewSearchPlayersLogic(ctx, s.svcCtx)
return l.SearchPlayers(in)
}
// -----------------------shopInvitations-----------------------
func (s *GamePublicServer) AddShopInvitations(ctx context.Context, in *pb.AddShopInvitationsReq) (*pb.AddShopInvitationsResp, error) {
l := logic.NewAddShopInvitationsLogic(ctx, s.svcCtx)
return l.AddShopInvitations(in)
}
func (s *GamePublicServer) UpdateShopInvitations(ctx context.Context, in *pb.UpdateShopInvitationsReq) (*pb.UpdateShopInvitationsResp, error) {
l := logic.NewUpdateShopInvitationsLogic(ctx, s.svcCtx)
return l.UpdateShopInvitations(in)
}
func (s *GamePublicServer) DelShopInvitations(ctx context.Context, in *pb.DelShopInvitationsReq) (*pb.DelShopInvitationsResp, error) {
l := logic.NewDelShopInvitationsLogic(ctx, s.svcCtx)
return l.DelShopInvitations(in)
}
func (s *GamePublicServer) GetShopInvitationsById(ctx context.Context, in *pb.GetShopInvitationsByIdReq) (*pb.GetShopInvitationsByIdResp, error) {
l := logic.NewGetShopInvitationsByIdLogic(ctx, s.svcCtx)
return l.GetShopInvitationsById(in)
}
func (s *GamePublicServer) SearchShopInvitations(ctx context.Context, in *pb.SearchShopInvitationsReq) (*pb.SearchShopInvitationsResp, error) {
l := logic.NewSearchShopInvitationsLogic(ctx, s.svcCtx)
return l.SearchShopInvitations(in)
}
// -----------------------shopPlayers-----------------------
func (s *GamePublicServer) AddShopPlayers(ctx context.Context, in *pb.AddShopPlayersReq) (*pb.AddShopPlayersResp, error) {
l := logic.NewAddShopPlayersLogic(ctx, s.svcCtx)
return l.AddShopPlayers(in)
}
func (s *GamePublicServer) UpdateShopPlayers(ctx context.Context, in *pb.UpdateShopPlayersReq) (*pb.UpdateShopPlayersResp, error) {
l := logic.NewUpdateShopPlayersLogic(ctx, s.svcCtx)
return l.UpdateShopPlayers(in)
}
func (s *GamePublicServer) DelShopPlayers(ctx context.Context, in *pb.DelShopPlayersReq) (*pb.DelShopPlayersResp, error) {
l := logic.NewDelShopPlayersLogic(ctx, s.svcCtx)
return l.DelShopPlayers(in)
}
func (s *GamePublicServer) GetShopPlayersById(ctx context.Context, in *pb.GetShopPlayersByIdReq) (*pb.GetShopPlayersByIdResp, error) {
l := logic.NewGetShopPlayersByIdLogic(ctx, s.svcCtx)
return l.GetShopPlayersById(in)
}
func (s *GamePublicServer) SearchShopPlayers(ctx context.Context, in *pb.SearchShopPlayersReq) (*pb.SearchShopPlayersResp, error) {
l := logic.NewSearchShopPlayersLogic(ctx, s.svcCtx)
return l.SearchShopPlayers(in)
}
// -----------------------shops-----------------------
func (s *GamePublicServer) AddShops(ctx context.Context, in *pb.AddShopsReq) (*pb.AddShopsResp, error) {
l := logic.NewAddShopsLogic(ctx, s.svcCtx)
return l.AddShops(in)
}
func (s *GamePublicServer) UpdateShops(ctx context.Context, in *pb.UpdateShopsReq) (*pb.UpdateShopsResp, error) {
l := logic.NewUpdateShopsLogic(ctx, s.svcCtx)
return l.UpdateShops(in)
}
func (s *GamePublicServer) DelShops(ctx context.Context, in *pb.DelShopsReq) (*pb.DelShopsResp, error) {
l := logic.NewDelShopsLogic(ctx, s.svcCtx)
return l.DelShops(in)
}
func (s *GamePublicServer) GetShopsById(ctx context.Context, in *pb.GetShopsByIdReq) (*pb.GetShopsByIdResp, error) {
l := logic.NewGetShopsByIdLogic(ctx, s.svcCtx)
return l.GetShopsById(in)
}
func (s *GamePublicServer) SearchShops(ctx context.Context, in *pb.SearchShopsReq) (*pb.SearchShopsResp, error) {
l := logic.NewSearchShopsLogic(ctx, s.svcCtx)
return l.SearchShops(in)
}
@@ -0,0 +1,50 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
// Source: game.proto
package server
import (
"context"
"juwan-backend/app/game/rpc/internal/logic"
"juwan-backend/app/game/rpc/internal/svc"
"juwan-backend/app/game/rpc/pb"
)
type PublicServer struct {
svcCtx *svc.ServiceContext
pb.UnimplementedPublicServer
}
func NewPublicServer(svcCtx *svc.ServiceContext) *PublicServer {
return &PublicServer{
svcCtx: svcCtx,
}
}
// -----------------------games-----------------------
func (s *PublicServer) AddGames(ctx context.Context, in *pb.AddGamesReq) (*pb.AddGamesResp, error) {
l := logic.NewAddGamesLogic(ctx, s.svcCtx)
return l.AddGames(in)
}
func (s *PublicServer) UpdateGames(ctx context.Context, in *pb.UpdateGamesReq) (*pb.UpdateGamesResp, error) {
l := logic.NewUpdateGamesLogic(ctx, s.svcCtx)
return l.UpdateGames(in)
}
func (s *PublicServer) DelGames(ctx context.Context, in *pb.DelGamesReq) (*pb.DelGamesResp, error) {
l := logic.NewDelGamesLogic(ctx, s.svcCtx)
return l.DelGames(in)
}
func (s *PublicServer) GetGamesById(ctx context.Context, in *pb.GetGamesByIdReq) (*pb.GetGamesByIdResp, error) {
l := logic.NewGetGamesByIdLogic(ctx, s.svcCtx)
return l.GetGamesById(in)
}
func (s *PublicServer) SearchGames(ctx context.Context, in *pb.SearchGamesReq) (*pb.SearchGamesResp, error) {
l := logic.NewSearchGamesLogic(ctx, s.svcCtx)
return l.SearchGames(in)
}
@@ -0,0 +1,48 @@
package svc
import (
"juwan-backend/app/game/rpc/internal/config"
"juwan-backend/app/game/rpc/internal/models"
"juwan-backend/app/snowflake/rpc/snowflake"
"juwan-backend/common/redisx"
"juwan-backend/common/snowflakex"
"juwan-backend/pkg/adapter"
"time"
"ariga.io/entcache"
"entgo.io/ent/dialect/sql"
"github.com/zeromicro/go-zero/core/logx"
)
type ServiceContext struct {
Config config.Config
Snowflake snowflake.SnowflakeServiceClient
GameModelRW *models.Client
GameModelRO *models.Client
}
func NewServiceContext(c config.Config) *ServiceContext {
RWConn, err := sql.Open("pgx", c.DB.Master)
if err != nil {
panic(err)
}
ROConn, err := sql.Open("pgx", c.DB.Slaves)
if err != nil {
panic(err)
}
redisCluster, err := redisx.ConnectMasterSlaveCluster(c.CacheConf, 5*time.Second)
if redisCluster == nil || err != nil {
logx.Errorf("failed to connect to Redis cluster: %v", err)
panic(err)
}
RWDrv := entcache.NewDriver(RWConn, entcache.TTL(30*time.Second), entcache.Levels(adapter.NewRedisCache(redisCluster.Client)))
RODrv := entcache.NewDriver(ROConn, entcache.TTL(30*time.Second), entcache.Levels(adapter.NewRedisCache(redisCluster.Client)))
return &ServiceContext{
Config: c,
GameModelRW: models.NewClient(models.Driver(RWDrv)),
GameModelRO: models.NewClient(models.Driver(RODrv)),
Snowflake: snowflakex.NewClient(c.SnowflakeRpcConf),
}
}