https://kotlinlang.org logo
#exposed
Title
# exposed
g

Gunslingor

04/11/2020, 5:29 PM
Is there a way to collapse the Report.new down so this takes up less lines... this.url = url and 10 more seems redundant and wasteful, is there some kind of map function?
Copy code
if(notFound) {
    val outlet = transaction { Outlet.find { outlet eq outletEntityID }.limit(1).toList()[0] }
    val category = transaction { Category.find { Categories.category eq outletData[Outlets.defaultCategory] }.limit(1).toList()[0] }
    val title = it.title
    val summary = it.description.value.take(255)
    val content = it.contents.joinToString()
    val discovered = DateTime.now(DateTimeZone.UTC)
    val authored = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy").parseDateTime("${it.publishedDate}")
    val thumbnailUrl = getReportFeatureImage(it) ?: outletData[defaultReportThumbnailUrl]

    transaction {
        Report.new {
            this.url = url
            this.outlet =outlet
            this.category = category
            this.title = title
            this.summary = summary
            this.content = content
            this.discovered = discovered
            this.authored = authored
            this.thumbnailUrl = thumbnailUrl
        }
    }
    counts.added += 1
} else {
    counts.existing += 1
}
t

tapac

04/11/2020, 9:46 PM
You could give a try to https://github.com/TouK/krush
g

Gunslingor

04/11/2020, 9:47 PM
thanks
t

tapac

04/11/2020, 9:48 PM
Also, there are draft (not public) of native support of data classes in Exposed. Please look at https://github.com/JetBrains/Exposed/issues/24#issuecomment-566725685 and comment if you like it or not.
g

Gunslingor

04/11/2020, 9:52 PM
Here is how I did the data classes in my app, to be inline with the way the record rows are implemented, or entities I guess they are called:
Copy code
//UserData: Can hold the record prior to insert or for external libraries like klaxon
class UserData {
    lateinit var username: String
    lateinit var email: String
    lateinit var created: String
    lateinit var passwordHash: String
    lateinit var passwordSalt: String
    lateinit var avatar: String
    lateinit var type: String

    fun new(init: UserData.() -> Unit): UserData {
        init()
        return this
    }
    fun dbCreateRecord() {
        User.new {
            this.username = EntityID(this@UserData.username, Users)
            this.email = <mailto:this@UserData.email|this@UserData.email>
            this.created = DateTime.parse(this@UserData.created)
            this.passwordHash = this@UserData.passwordHash
            this.passwordSalt = this@UserData.passwordSalt
            this.avatar = this@UserData.avatar
            this.type = this@UserData.type
        }
    }
}

//UserRestResponse: services for restful string responses
/*object UserRestResponse {

}*/

//defaultUsers: List of default users to insert
val defaultUsers = mutableMapOf(
    EntityID("admin", Users) to UserData().new {
        username = "admin"
        email = "<mailto:admin@newsworthy.app|admin@newsworthy.app>"
    },
    EntityID("system", Users) to UserData().new {
        username = "system"
        email = "<mailto:system@newsworthy.app|system@newsworthy.app>"
    }
)