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

io.github.dehuckakpyt.telegrambot.source.chain.JpaChainSource.kt Maven / Gradle / Ivy

There is a newer version: 0.11.4
Show newest version
package io.github.dehuckakpyt.telegrambot.source.chain

import io.github.dehuckakpyt.telegrambot.model.chain.JpaChain
import io.github.dehuckakpyt.telegrambot.repository.chain.JpaChainRepository
import io.github.dehuckakpyt.telegrambot.transaction.action.TransactionAction

open class JpaChainSource(
    private val transactional: TransactionAction,
    private val repository: JpaChainRepository,
) : ChainSource {

    override suspend fun save(chatId: Long, fromId: Long, step: String?, content: String?): Unit = transactional {
        val chain = repository.findFirstByChatIdAndFromId(chatId, fromId)?.apply {
            this.step = step
            this.content = content
        } ?: JpaChain(
            chatId = chatId,
            fromId = fromId,
            step = step,
            content = content,
        )

        repository.save(chain)
    }

    override suspend fun get(chatId: Long, fromId: Long): JpaChain? = transactional(readOnly = true) {
        repository.findFirstByChatIdAndFromId(chatId, fromId)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy