All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.skillw.pouvoir.internal.feature.database.mongodb.MongoUserContainer.kt Maven / Gradle / Ivy

There is a newer version: 1.6.7-beta-6
Show newest version
package com.skillw.pouvoir.internal.feature.database.mongodb

import com.mongodb.client.MongoDatabase
import com.skillw.pouvoir.api.feature.database.UserBased
import org.bson.BsonDocument

/**
 * @className MongoContainer
 *
 * @author Glom
 * @date 2023/8/11 23:00 Copyright 2024 Glom.
 */
class MongoUserContainer(key: String, holder: MongoContainerHolder, database: MongoDatabase) :
    MongoContainer(key, holder, database), UserBased {

    override fun get(user: String, key: String): String? {
        val bson = BsonDocument.parse("{ user:$user }")
        val result = collection.find(bson)
        return result.first()?.get(key)?.toString()
    }

    override fun delete(user: String, key: String) {
        val bson = BsonDocument.parse("{ user:$user }")
        collection.deleteOne(bson)
    }

    override fun set(user: String, key: String, value: String?) {
        if (value == null) {
            delete(user, key)
            return
        }
        val target = BsonDocument.parse("{ user:$user }")
        val change = BsonDocument.parse("{ $key: $value }")
        collection.findOneAndUpdate(target, change)
    }

    override fun contains(user: String, key: String): Boolean {
        return get(user, key) != null
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy