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

jvmMain.hu.simplexion.adaptive.email.service.EmailService.kt Maven / Gradle / Ivy

package hu.simplexion.adaptive.email.service

import hu.simplexion.adaptive.email.api.EmailApi
import hu.simplexion.adaptive.email.model.Email
import hu.simplexion.adaptive.email.model.EmailQueueEntry
import hu.simplexion.adaptive.email.store.EmailQueue
import hu.simplexion.adaptive.email.store.EmailTable
import hu.simplexion.adaptive.email.worker.EmailWorker
import hu.simplexion.adaptive.server.builtin.store
import hu.simplexion.adaptive.server.builtin.worker
import hu.simplexion.adaptive.service.ServiceContext
import hu.simplexion.adaptive.server.builtin.ServiceImpl
import org.jetbrains.exposed.sql.transactions.transaction

class EmailService : EmailApi, ServiceImpl {

    // FIXME too expensive server fragment lookup in service implementation
    private val emailTable by store()
    private val emailQueue by store()
    private val emailWorker by worker()

    // FIXME newInstance fragment copy
    override fun newInstance(serviceContext: ServiceContext): EmailService {
        return EmailService().also {
            it.fragment = this.fragment
        }
    }

    override suspend fun send(recipients: String, subject: String, contentText: String, contentType: String) {
        transaction {
            val email = Email(recipients, subject, contentText, contentType = contentType)

            emailTable.insert(email)

            val entry = EmailQueueEntry(email.uuid)

            emailQueue.insert(entry)
            emailWorker.normalQueue.trySend(entry)
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy