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

com.antwerkz.critter.test.criteria.UserCriteria.kt Maven / Gradle / Ivy

There is a newer version: 3.0.4
Show newest version
package com.antwerkz.critter.test.criteria

import com.antwerkz.critter.TypeSafeFieldEnd
import com.antwerkz.critter.test.User
import com.mongodb.WriteConcern
import com.mongodb.WriteResult
import kotlin.String
import kotlin.Suppress
import org.mongodb.morphia.Datastore
import org.mongodb.morphia.query.Criteria
import org.mongodb.morphia.query.CriteriaContainer
import org.mongodb.morphia.query.Query
import org.mongodb.morphia.query.UpdateOperations
import org.mongodb.morphia.query.UpdateResults

@Suppress("UNCHECKED_CAST")
class UserCriteria(
    private val ds: Datastore,
    private val query: Query<*>,
    fieldName: String?
) {
    private var prefix: String = fieldName?.let { fieldName + "." } ?: "" 

    constructor(ds: Datastore, fieldName: String? = null) : this(ds, ds.find(User::class.java), fieldName)

    fun query(): Query = query as Query
    fun delete(wc: WriteConcern = ds.defaultWriteConcern): WriteResult = ds.delete(query, wc)
    fun or(vararg criteria: Criteria): CriteriaContainer = query.or(*criteria)
    fun and(vararg criteria: Criteria): CriteriaContainer = query.and(*criteria)
    fun email(): TypeSafeFieldEnd = TypeSafeFieldEnd(this, query, email)
    fun email(__newValue: String?): Criteria = TypeSafeFieldEnd(this, query, email).equal(__newValue)
    fun age(): TypeSafeFieldEnd = TypeSafeFieldEnd(this, query, age)
    fun age(__newValue: Long): Criteria = TypeSafeFieldEnd(this, query, age).equal(__newValue)
    fun updater(): UserUpdater = UserUpdater(ds, query, ds.createUpdateOperations(User::class.java),
    if(prefix.isNotEmpty()) prefix else null)
    companion object {
        val email: String = "email"

        val age: String = "age"
    }

    class UserUpdater(
        private val ds: Datastore,
        private val query: Query<*>,
        private val updateOperations: UpdateOperations<*>,
        fieldName: String?
    ) {
        private var prefix: String = fieldName?.let { fieldName + "." } ?: "" 

        fun updateAll(wc: WriteConcern = ds.defaultWriteConcern): UpdateResults = ds.update(query as Query, updateOperations as UpdateOperations, false, wc)
        fun updateFirst(wc: WriteConcern = ds.defaultWriteConcern): UpdateResults = ds.updateFirst(query as Query, updateOperations as UpdateOperations, false, wc)
        fun upsert(wc: WriteConcern = ds.defaultWriteConcern): UpdateResults = ds.update(query as Query, updateOperations as UpdateOperations, true, wc)
        fun remove(wc: WriteConcern = ds.defaultWriteConcern): WriteResult = ds.delete(query, wc)
        fun email(__newValue: String?): UserUpdater {
                                        updateOperations.set(prefix + email, __newValue)
                                        return this}

        fun unsetEmail(): UserUpdater {

                                        updateOperations.unset(prefix + email)
                                        return this
                                        }

        fun removeFirstFromEmail(): UserUpdater {
            updateOperations.removeFirst(prefix + email)
            return this }

        fun removeLastFromEmail(): UserUpdater {
            updateOperations.removeLast(prefix + email)
            return this }

        fun removeFromEmail(__newValue: String?): UserUpdater {
            updateOperations.removeAll(prefix + email, __newValue)
            return this }

        fun removeAllFromEmail(values: String?): UserUpdater {
            updateOperations.removeAll(prefix + email, values)
            return this }

        fun age(__newValue: Long): UserUpdater {
                                        updateOperations.set(prefix + age, __newValue)
                                        return this}

        fun unsetAge(): UserUpdater {

                                        updateOperations.unset(prefix + age)
                                        return this
                                        }

        fun incAge(__newValue: Long = 1.toLong()): UserUpdater {
            updateOperations.inc(prefix + age, __newValue)
            return this}

        fun removeFirstFromAge(): UserUpdater {
            updateOperations.removeFirst(prefix + age)
            return this }

        fun removeLastFromAge(): UserUpdater {
            updateOperations.removeLast(prefix + age)
            return this }

        fun removeFromAge(__newValue: Long): UserUpdater {
            updateOperations.removeAll(prefix + age, __newValue)
            return this }

        fun removeAllFromAge(values: Long): UserUpdater {
            updateOperations.removeAll(prefix + age, values)
            return this }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy