package logic import ( "context" "git.cialloo.com/CiallooWeb/ServerStatistics/app/internal/svc" "git.cialloo.com/CiallooWeb/ServerStatistics/app/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type ServerListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // Get the list of monitored game servers func NewServerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ServerListLogic { return &ServerListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *ServerListLogic) ServerList(req *types.ServerListReq) (resp []types.ServerListResp, err error) { // Query to select all servers from the database query := `SELECT name, ip, port, category, bot_count, human_count, mapname FROM server ORDER BY id` rows, err := l.svcCtx.DB.Query(query) if err != nil { l.Logger.Errorf("Failed to query servers: %v", err) return nil, err } defer rows.Close() // Iterate through the results and build the response for rows.Next() { var server types.ServerListResp err = rows.Scan( &server.ServerName, &server.ServerIP, &server.ServerPort, &server.Category, &server.BotCount, &server.HumanCount, &server.MapName, ) if err != nil { l.Logger.Errorf("Failed to scan server row: %v", err) return nil, err } resp = append(resp, server) } // Check for any error that occurred during iteration if err = rows.Err(); err != nil { l.Logger.Errorf("Error iterating through server rows: %v", err) return nil, err } return resp, nil }