Skip to content

Commit a6a9b28

Browse files
committed
add get by numerical & by group
1 parent 7e0f186 commit a6a9b28

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

api/src/main/java/app/simplecloud/api/internal/server/ServerApiImpl.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,55 @@ public CompletableFuture<Server> getServerById(String id) {
5858
});
5959
}
6060

61+
@Override
62+
public CompletableFuture<Server> getServerByNumericalId(String groupName, int numericalId) {
63+
return CompletableFuture.supplyAsync(() -> {
64+
try {
65+
ServerQuery query = ServerQuery.create()
66+
.filterByServerGroupName(groupName)
67+
.filterByNumericalId(numericalId);
68+
ModelsListServersResponse serversResponse = executeQuery(query);
69+
70+
List<ModelsServerSummary> servers = serversResponse.getServers();
71+
if (servers == null) {
72+
return null;
73+
}
74+
75+
for (ModelsServerSummary summary : servers) {
76+
if (summary.getNumericalId() != null && summary.getNumericalId() == numericalId) {
77+
return new ServerImpl(summary);
78+
}
79+
}
80+
throw new RuntimeException("Server not found in group " + groupName + " with numerical ID " + numericalId);
81+
82+
} catch (ApiException e) {
83+
throw new RuntimeException(e);
84+
}
85+
});
86+
}
87+
88+
@Override
89+
public CompletableFuture<List<Server>> getServersByGroup(String groupName) {
90+
return CompletableFuture.supplyAsync(() -> {
91+
try {
92+
ServerQuery query = ServerQuery.create()
93+
.filterByServerGroupName(groupName);
94+
ModelsListServersResponse serversResponse = executeQuery(query);
95+
96+
List<ModelsServerSummary> servers = serversResponse.getServers();
97+
if (servers == null) {
98+
return List.of();
99+
}
100+
101+
return servers.stream()
102+
.<Server>map(ServerImpl::new)
103+
.toList();
104+
} catch (ApiException e) {
105+
throw new RuntimeException(e);
106+
}
107+
});
108+
}
109+
61110
@Override
62111
public CompletableFuture<List<Server>> getAllServers(@Nullable ServerQuery query) {
63112
return CompletableFuture.supplyAsync(() -> {

api/src/main/java/app/simplecloud/api/server/ServerApi.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ public interface ServerApi {
2222
*/
2323
CompletableFuture<Server> getServerById(String id);
2424

25+
/**
26+
* Retrieves a server by its numerical ID.
27+
*
28+
* @param groupName the name of the server group
29+
* @param numericalId the numerical server ID
30+
* @return a CompletableFuture that completes with the server, or fails if not found
31+
*/
32+
CompletableFuture<Server> getServerByNumericalId(String groupName, int numericalId);
33+
34+
/**
35+
* Retrieves all servers belonging to a specific group.
36+
*
37+
* @param groupName the name of the server group
38+
* @return a CompletableFuture that completes with a list of servers in the specified group
39+
*/
40+
CompletableFuture<List<Server>> getServersByGroup(String groupName);
41+
2542
/**
2643
* Retrieves all servers, optionally filtered by a query.
2744
*

0 commit comments

Comments
 (0)