75 lines
2.5 KiB
Kotlin
75 lines
2.5 KiB
Kotlin
package com.wyattjmiller.repositories
|
|
|
|
import com.wyattjmiller.data.DatabaseManager
|
|
import com.wyattjmiller.data.DbAuthorTable
|
|
import com.wyattjmiller.data.DbUserTable
|
|
import com.wyattjmiller.data.DbUserTable.password
|
|
import com.wyattjmiller.data.DbUserTable.username
|
|
import com.wyattjmiller.entities.Author
|
|
import com.wyattjmiller.entities.User
|
|
import com.wyattjmiller.entities.UserDraft
|
|
import com.wyattjmiller.entities.UserLogin
|
|
import org.ktorm.dsl.*
|
|
import org.ktorm.entity.firstOrNull
|
|
import org.ktorm.entity.sequenceOf
|
|
import java.time.LocalDateTime
|
|
|
|
class UserDao : UserRepository {
|
|
private val db = DatabaseManager().database
|
|
|
|
fun usernameCheck(user: UserLogin) : UserLogin? {
|
|
return db.from(DbUserTable)
|
|
.select()
|
|
.where { username eq user.username }
|
|
.map {
|
|
// don't know what to do here
|
|
UserLogin(user.username, user.password)
|
|
}.firstOrNull()
|
|
}
|
|
|
|
fun passwordCheck(user: UserLogin) : UserLogin? {
|
|
return db.from(DbUserTable)
|
|
.select()
|
|
.where { password eq user.password }
|
|
.map {
|
|
// don't know what to do here
|
|
UserLogin(user.username, user.password)
|
|
}.firstOrNull()
|
|
}
|
|
|
|
fun getAllUsers() {
|
|
TODO("Joke's on you! This function is not implemented yet. -Wyatt")
|
|
}
|
|
|
|
override fun getUser(id: Int): User? {
|
|
return db
|
|
.sequenceOf(DbUserTable)
|
|
.firstOrNull {
|
|
it.id eq id
|
|
}
|
|
.let {
|
|
User(it!!.id, it.firstName, it.lastName, it.username, it.password, it.emailAddress, it.createdTimestamp.toString(), 0)
|
|
}
|
|
}
|
|
|
|
override fun createUser(user: UserDraft): User {
|
|
val key = db.insertAndGenerateKey(DbUserTable) {
|
|
set(DbUserTable.firstName, user.firstName)
|
|
set(DbUserTable.lastName, user.lastName)
|
|
set(DbUserTable.username, user.username)
|
|
set(DbUserTable.password, user.password)
|
|
set(DbUserTable.emailAddress, user.emailAddress)
|
|
set(DbUserTable.createdTimestamp, LocalDateTime.now())
|
|
} as Int
|
|
|
|
return User(key, user.firstName, user.lastName, user.username, user.password, user.emailAddress, LocalDateTime.now().toString(), 0)
|
|
}
|
|
|
|
override fun updateUser(id: Int, user: UserDraft): Boolean {
|
|
TODO("Not yet implemented")
|
|
}
|
|
|
|
override fun deleteUser(id: Int): Boolean {
|
|
TODO("Not yet implemented")
|
|
}
|
|
} |