generated from Kord-Extensions/template
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/org/quiltmc/community/database/collections/LinkedMessagesCollection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.quiltmc.community.database.collections | ||
|
||
import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent | ||
import dev.kord.common.entity.Snowflake | ||
import org.koin.core.component.inject | ||
import org.litote.kmongo.contains | ||
import org.litote.kmongo.eq | ||
import org.quiltmc.community.database.Collection | ||
import org.quiltmc.community.database.Database | ||
import org.quiltmc.community.database.entities.LinkedMessages | ||
|
||
class LinkedMessagesCollection : KordExKoinComponent { | ||
private val database: Database by inject() | ||
private val col = database.mongo.getCollection<LinkedMessages>(name) | ||
|
||
suspend fun getBySource(id: Snowflake) = | ||
col.findOne(LinkedMessages::_id eq id) | ||
|
||
fun getByTarget(id: Snowflake) = | ||
col.find(LinkedMessages::targets contains id).toFlow() | ||
|
||
suspend fun set(linkedMessages: LinkedMessages) = | ||
col.save(linkedMessages) | ||
|
||
companion object : Collection("linked-messages") | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/org/quiltmc/community/database/entities/LinkedMessages.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
@file:Suppress("DataClassShouldBeImmutable", "DataClassContainsFunctions") // Well, yes, but actually no. | ||
|
||
package org.quiltmc.community.database.entities | ||
|
||
import com.kotlindiscord.kord.extensions.utils.getKoin | ||
import dev.kord.common.entity.Snowflake | ||
import kotlinx.serialization.Serializable | ||
import org.quiltmc.community.database.Entity | ||
import org.quiltmc.community.database.collections.LinkedMessagesCollection | ||
|
||
@Serializable | ||
data class LinkedMessages( | ||
override val _id: Snowflake, | ||
|
||
val targets: MutableList<Snowflake> = mutableListOf() | ||
) : Entity<Snowflake> { | ||
suspend fun save() { | ||
val collection = getKoin().get<LinkedMessagesCollection>() | ||
|
||
collection.set(this) | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/org/quiltmc/community/database/migrations/v22.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.quiltmc.community.database.migrations | ||
|
||
import org.litote.kmongo.coroutine.CoroutineDatabase | ||
import org.quiltmc.community.database.collections.LinkedMessagesCollection | ||
|
||
suspend fun v22(db: CoroutineDatabase) { | ||
db.createCollection(LinkedMessagesCollection.name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters