@@ -2,15 +2,17 @@ package app.simplecloud.controller.runtime.server
22
33import app.simplecloud.controller.runtime.LoadableRepository
44import app.simplecloud.controller.runtime.database.Database
5- import app.simplecloud.controller.shared.db.Tables.CLOUD_SERVERS
6- import app.simplecloud.controller.shared.db.Tables.CLOUD_SERVER_PROPERTIES
75import app.simplecloud.controller.shared.db.tables.records.CloudServersRecord
6+ import app.simplecloud.controller.shared.db.tables.references.CLOUD_SERVERS
7+ import app.simplecloud.controller.shared.db.tables.references.CLOUD_SERVER_PROPERTIES
88import app.simplecloud.controller.shared.server.Server
99import build.buf.gen.simplecloud.controller.v1.ServerState
1010import build.buf.gen.simplecloud.controller.v1.ServerType
1111import kotlinx.coroutines.Dispatchers
12+ import kotlinx.coroutines.flow.toCollection
13+ import kotlinx.coroutines.reactive.asFlow
14+ import kotlinx.coroutines.reactive.awaitFirstOrNull
1215import kotlinx.coroutines.withContext
13- import org.jooq.Result
1416import org.jooq.exception.DataAccessException
1517import java.time.LocalDateTime
1618
@@ -20,107 +22,82 @@ class ServerRepository(
2022) : LoadableRepository<Server, String> {
2123
2224 override suspend fun find (identifier : String ): Server ? {
23- val query = withContext(Dispatchers .IO ) {
24- database.context.select()
25- .from(CLOUD_SERVERS )
26- .where(CLOUD_SERVERS .UNIQUE_ID .eq(identifier))
27- .limit(1 )
28- .fetchInto(
29- CLOUD_SERVERS
30- )
31- }
32- return toList(query).firstOrNull()
25+ return database.context.selectFrom(CLOUD_SERVERS )
26+ .where(CLOUD_SERVERS .UNIQUE_ID .eq(identifier))
27+ .limit(1 )
28+ .awaitFirstOrNull()
29+ ?.let { record -> mapCloudServersRecordToServer(record) }
3330 }
3431
3532
3633 suspend fun findServerByNumerical (group : String , id : Int ): Server ? {
37- val query = withContext(Dispatchers .IO ) {
38- database.context.select().from(CLOUD_SERVERS )
39- .where(
40- CLOUD_SERVERS .GROUP_NAME .eq(group)
41- .and (CLOUD_SERVERS .NUMERICAL_ID .eq(id))
42- )
43- .limit(1 )
44- .fetchInto(CLOUD_SERVERS )
45- }
46- return toList(query).firstOrNull()
34+ return database.context.selectFrom(CLOUD_SERVERS )
35+ .where(
36+ CLOUD_SERVERS .GROUP_NAME .eq(group)
37+ .and (CLOUD_SERVERS .NUMERICAL_ID .eq(id))
38+ )
39+ .limit(1 )
40+ .awaitFirstOrNull()
41+ ?.let { record -> mapCloudServersRecordToServer(record) }
4742 }
4843
4944 override suspend fun getAll (): List <Server > {
50- val query = withContext(Dispatchers .IO ) {
51- database.context.select()
52- .from(CLOUD_SERVERS )
53- .fetchInto(CLOUD_SERVERS )
54- }
55- return toList(query)
56-
45+ return database.context.selectFrom(CLOUD_SERVERS )
46+ .asFlow()
47+ .toCollection(mutableListOf ())
48+ .map { record -> mapCloudServersRecordToServer(record) }
5749 }
5850
5951 suspend fun findServersByHostId (id : String ): List <Server > {
60- val query = withContext(Dispatchers .IO ) {
61- database.context.select()
62- .from(CLOUD_SERVERS )
63- .where(CLOUD_SERVERS .HOST_ID .eq(id))
64- .fetchInto(
65- CLOUD_SERVERS
66- )
67- }
68- return toList(query)
52+ return database.context.selectFrom(CLOUD_SERVERS )
53+ .where(CLOUD_SERVERS .HOST_ID .eq(id))
54+ .asFlow()
55+ .toCollection(mutableListOf ())
56+ .map { record -> mapCloudServersRecordToServer(record) }
6957 }
7058
7159 suspend fun findServersByGroup (group : String ): List <Server > {
72- val query = withContext(Dispatchers .IO ) {
73- database.context.select()
74- .from(CLOUD_SERVERS )
75- .where(CLOUD_SERVERS .GROUP_NAME .eq(group))
76- .fetchInto(
77- CLOUD_SERVERS
78- )
79- }
80- return toList(query)
60+ return database.context.selectFrom(CLOUD_SERVERS )
61+ .where(CLOUD_SERVERS .GROUP_NAME .eq(group))
62+ .asFlow()
63+ .toCollection(mutableListOf ())
64+ .map { record -> mapCloudServersRecordToServer(record) }
8165 }
8266
8367 suspend fun findServersByType (type : ServerType ): List <Server > {
84- val query = withContext(Dispatchers .IO ) {
85- database.context.select()
86- .from(CLOUD_SERVERS )
87- .where(CLOUD_SERVERS .TYPE .eq(type.toString()))
88- .fetchInto(CLOUD_SERVERS )
89- }
90- return toList(query)
68+ return database.context.selectFrom(CLOUD_SERVERS )
69+ .where(CLOUD_SERVERS .TYPE .eq(type.toString()))
70+ .asFlow()
71+ .toCollection(mutableListOf ())
72+ .map { record -> mapCloudServersRecordToServer(record) }
9173 }
9274
93- private fun toList (query : Result <CloudServersRecord >): List <Server > {
94- val result = mutableListOf<Server >()
95- query.map {
96- val propertiesQuery =
97- database.context.select()
98- .from(CLOUD_SERVER_PROPERTIES )
99- .where(CLOUD_SERVER_PROPERTIES .SERVER_ID .eq(it.uniqueId))
100- .fetchInto(CLOUD_SERVER_PROPERTIES )
101- result.add(
102- Server (
103- it.uniqueId,
104- ServerType .valueOf(it.type),
105- it.groupName,
106- it.hostId,
107- it.numericalId,
108- it.ip,
109- it.port.toLong(),
110- it.minimumMemory.toLong(),
111- it.maximumMemory.toLong(),
112- it.maxPlayers.toLong(),
113- it.playerCount.toLong(),
114- propertiesQuery.map { item ->
115- item.key to item.value
116- }.toMap().toMutableMap(),
117- ServerState .valueOf(it.state),
118- it.createdAt,
119- it.updatedAt
120- )
121- )
122- }
123- return result
75+ private fun mapCloudServersRecordToServer (record : CloudServersRecord ): Server {
76+ val propertiesQuery =
77+ database.context.select()
78+ .from(CLOUD_SERVER_PROPERTIES )
79+ .where(CLOUD_SERVER_PROPERTIES .SERVER_ID .eq(record.uniqueId))
80+ .fetchInto(CLOUD_SERVER_PROPERTIES )
81+
82+ return Server (
83+ record.uniqueId!! ,
84+ ServerType .valueOf(record.type!! ),
85+ record.groupName!! ,
86+ record.hostId!! ,
87+ record.numericalId!! ,
88+ record.ip!! ,
89+ record.port!! .toLong(),
90+ record.minimumMemory!! .toLong(),
91+ record.maximumMemory!! .toLong(),
92+ record.maxPlayers!! .toLong(),
93+ record.playerCount!! .toLong(),
94+ propertiesQuery.map { item ->
95+ item.key!! to item.value!!
96+ }.toMap().toMutableMap(),
97+ ServerState .valueOf(record.state!! ),
98+ record.createdAt!! ,
99+ record.updatedAt!!
100+ )
124101 }
125102
126103 override suspend fun delete (element : Server ): Boolean {
@@ -220,14 +197,15 @@ class ServerRepository(
220197 }
221198
222199 override fun load (): List <Server > {
223- val query = database.context.select()
224- .from(CLOUD_SERVERS )
200+ val cloudServerList = database.context.selectFrom(CLOUD_SERVERS )
225201 .fetchInto(CLOUD_SERVERS )
226- val list = toList(query)
227- list.forEach {
202+ .map { record -> mapCloudServersRecordToServer(record) }
203+
204+ cloudServerList.forEach {
228205 numericalIdRepository.saveNumericalId(it.group, it.numericalId)
229206 }
230- return list
207+
208+ return cloudServerList
231209 }
232210
233211}
0 commit comments