Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lecture07 communication with database #147

Open
wants to merge 2 commits into
base: lecture07
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lecture07/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ dependencies {
compile(spring("web"))
compile(spring("actuator"))

testCompile("junit", "junit", "4.12")
testCompile("org.testng", "testng", "6.14.3")
testCompile(spring("test"))


ktlint("com.github.shyiko", "ktlint", "0.28.0")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.rybalkinsd.kotlinbootcamp

fun main() {
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.rybalkinsd.kotlinbootcamp.dao

import org.jetbrains.exposed.sql.Op

interface Dao<T> {
/**
Expand All @@ -10,7 +11,7 @@ interface Dao<T> {
/**
* SELECT * ... WHERE cond0 AND ... AND condN
*/
fun getAllWhere(vararg conditions: String): List<T>
fun getAllWhere(vararg where: Op<Boolean>): List<T>

/**
* INSERT INTO ...
Expand All @@ -21,5 +22,5 @@ interface Dao<T> {
* SELECT * from ... WHERE id=
* @return Optional.empty() if nothing found
*/
fun findById(id: Int): T? = getAllWhere("id=$id").firstOrNull()
fun findById(id: Int): T
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
package io.rybalkinsd.kotlinbootcamp.dao

import io.rybalkinsd.kotlinbootcamp.model.Message
import io.rybalkinsd.kotlinbootcamp.model.toMessage
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction

class MessageDao : Dao<Message> {
override fun findById(id: Int) = transaction {
Messages.select { Messages.id eq id }
.firstOrNull()
?.toMessage()
?: throw IllegalStateException("Message with $id not found in database")
}

override val all: List<Message>
get() = TODO("not implemented")
get() = transaction {
Messages.selectAll()
.toList()
.map(ResultRow::toMessage)
}

override fun getAllWhere(vararg conditions: String): List<Message> {
TODO("not implemented")
override fun getAllWhere(vararg where: Op<Boolean>): List<Message> = transaction {
Messages.select(where.reduce { resultExpr, condition -> resultExpr and condition })
.toList()
.map(ResultRow::toMessage)
}

override fun insert(t: Message) {
TODO("not implemented")
transaction {
Messages.insert {
it[id] = t.id
it[user] = t.user.id
it[time] = t.timestamp
it[value] = t.value
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
package io.rybalkinsd.kotlinbootcamp.dao

import io.rybalkinsd.kotlinbootcamp.model.User
import io.rybalkinsd.kotlinbootcamp.model.toUser
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction

class UserDao : Dao<User> {
override fun findById(id: Int) = transaction {
Users.select { Users.id eq id }
.firstOrNull()
?.toUser()
?: throw IllegalStateException("User with $id not found in database")
}

override val all: List<User>
get() = TODO("not implemented")
get() = transaction {
Users.selectAll()
.toList()
.map(ResultRow::toUser)
}

override fun getAllWhere(vararg conditions: String): List<User> {
TODO("not implemented")
override fun getAllWhere(vararg where: Op<Boolean>) = transaction {
Users.select(where.reduce { resultExpr, condition -> resultExpr and condition })
.toList()
.map(ResultRow::toUser)
}

override fun insert(t: User) {
TODO("not implemented")
transaction {
Users.insert {
it[id] = t.id
it[login] = t.login
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ object Messages : Table("chat.message") {
val user = integer("user") references Users.id
val time = datetime("time")
val value = text("value")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package io.rybalkinsd.kotlinbootcamp.db
import io.rybalkinsd.kotlinbootcamp.util.logger
import org.jetbrains.exposed.sql.Database


object DbConnector {
private val log = logger()

private val host = "54.224.37.210"
private val port = 5432
private val dbName = "chatdb_atom49"
private val user = "atom49"
private val password = "atom49"
private val dbName = "chatdb_atom6"
private val user = "atom6"
private val password = "atom6"

init {
Database.connect(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
package io.rybalkinsd.kotlinbootcamp.model

import io.rybalkinsd.kotlinbootcamp.dao.Messages
import io.rybalkinsd.kotlinbootcamp.dao.UserDao
import io.rybalkinsd.kotlinbootcamp.dao.Users
import org.jetbrains.exposed.sql.ResultRow

internal fun ResultRow.toUser() = User(this[Users.id], this[Users.login])
internal fun ResultRow.toUser() = User(this[Users.id], this[Users.login])

internal fun ResultRow.toMessage() = Message(
this[Messages.id],
UserDao().findById(this[Messages.user]),
this[Messages.time],
this[Messages.value]
)
8 changes: 8 additions & 0 deletions lecture07/src/main/resources/sql/data/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ select *
from chat.message
where time > '2017-03-25';


select login, value
from chat.user, chat.message
where "user"=44;

select value, chat.user.login
from chat.message
left join chat.user on chat.message.user = chat.user.id;
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.transaction
import org.joda.time.DateTime
import org.junit.Test
import org.testng.annotations.Test

@Test(enabled = false)
class MessageDaoTest {

@Test
Expand All @@ -18,10 +19,9 @@ class MessageDaoTest {
addLogger(StdOutSqlLogger)

Messages.insert {
it[id] = 0
it[user] = 42
it[user] = 44
it[time] = DateTime.now()
it[value] = "My first message"
it[value] = "My first haha"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ package io.rybalkinsd.kotlinbootcamp.dao

import io.rybalkinsd.kotlinbootcamp.db.DbConnector
import io.rybalkinsd.kotlinbootcamp.model.toUser
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.StdOutSqlLogger
import org.jetbrains.exposed.sql.addLogger
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.junit.Before
import org.junit.Test
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test

@Test(enabled = false)
class UserDaoTest {

@Before
@BeforeClass
fun setUp() {
DbConnector
}
Expand All @@ -23,7 +25,7 @@ class UserDaoTest {
addLogger(StdOutSqlLogger)

val id = Users.insert {
it[id] = 44
it[id] = 1
it[login] = "login44"
} get Users.id

Expand All @@ -32,17 +34,13 @@ class UserDaoTest {
}

@Test
fun `select * from User`() {
fun `select from User`() {
transaction {
addLogger(StdOutSqlLogger)

val users = Users.selectAll().map {
it.toUser()
}
val users = Users.selectAll().map(ResultRow::toUser)

println(users)
}
}


}
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
package io.rybalkinsd.kotlinbootcamp.practice

import io.rybalkinsd.kotlinbootcamp.dao.MessageDao
import io.rybalkinsd.kotlinbootcamp.dao.Messages
import io.rybalkinsd.kotlinbootcamp.db.DbConnector
import io.rybalkinsd.kotlinbootcamp.model.Message
import junit.framework.TestCase.assertTrue
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import io.rybalkinsd.kotlinbootcamp.practice.ChatUserDaoTest.Companion.admin
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.exposed.sql.Op
import org.joda.time.DateTime
import org.testng.annotations.BeforeClass
import org.testng.annotations.Test


@Ignore
class ChatMessageDaoTest {

val messageDao = MessageDao()

@Before
@BeforeClass
fun setUp() {
DbConnector
}

@Test
fun `select * from message`() {
assertTrue(messageDao.all.isNotEmpty())
}

@Test
fun `insert message`() {
fun insertMessage() {
val before = messagesNumber()
messageDao.insert(dummyMessage())
messageDao.insert(dummyMessage(before))
val after = messagesNumber()
assertTrue(before + 1 >= after)
assertThat(before + 1).isEqualTo(after)
}

@Test(dependsOnMethods = ["insertMessage"])
fun selectAll() {
assertThat(messageDao.all)
.isNotEmpty
}

@Test(dependsOnMethods = ["selectAll"])
fun getMessageById() {
val id = messageDao.all.firstOrNull()?.id
?: return Assertions.fail("Failed to load id from database")
assertThat(messageDao.findById(id).id).isEqualTo(id)
}

@Test(dependsOnMethods = ["insertMessage"])
fun getHelloMessage() {
assertThat(messageDao.getAllWhere(Op.build { Messages.value like "Hello %" }))
.isNotEmpty
}

fun messagesNumber(): Long = TODO()
fun dummyMessage(): Message = TODO()
private fun messagesNumber() = messageDao.all.size
private fun dummyMessage(messageAmunt: Int) = Message(messageAmunt, admin, DateTime(0), "Hello guys")
}
Loading