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

main #37

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

main #37

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
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FakeQuestionRepository : QuestionRepository {

override suspend fun addQuestion(newQuestionData: QuestionData): Question? {
val maxId = questions.maxOf { it.postId }
val now = Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault())
val now = Clock.System.now()
val question = Question(
maxId + 1,
postType = newQuestionData.postType ?: PostType.QUESTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime

fun rightNow() = Clock.System.now().toLocalDateTime(TimeZone.UTC)
fun rightNow() = Clock.System.now()

fun daoToDto(dao: UserDAO): User {
return User(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.datetime.LocalDate
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.JoinType.INNER
import org.jetbrains.exposed.sql.JoinType.LEFT
import org.jetbrains.exposed.sql.SortOrder.ASC
import org.jetbrains.exposed.sql.SqlExpressionBuilder.coalesce
import org.jetbrains.exposed.sql.kotlin.datetime.date
Expand Down Expand Up @@ -86,7 +87,7 @@ class PostgresUserRepository {
CommentTable
.join(ContentTable, INNER, CommentTable.data, ContentTable.id)
.join(UserTable, INNER, ContentTable.author, UserTable.id)
.join(VoteTable, INNER, VoteTable.content, ContentTable.id)
.join(VoteTable, LEFT, VoteTable.content, ContentTable.id)
.slice(CommentTable.id, text, createdAt, author, votes)
.select { contentFilters(ids, queryParams.fromDate, queryParams.toDate) }
.groupBy(CommentTable.id)
Expand Down Expand Up @@ -125,7 +126,7 @@ class PostgresUserRepository {
QuestionTable
.join(ContentTable, INNER, QuestionTable.data, ContentTable.id)
.join(UserTable, INNER, ContentTable.author, UserTable.id)
.join(VoteTable, INNER, VoteTable.content, ContentTable.id)
.join(VoteTable, LEFT, VoteTable.content, ContentTable.id)
.slice(QuestionTable.id, questionTitle, text, createdAt, author, votes)
.select { contentFilters(ids, queryParams.fromDate, queryParams.toDate) }
.groupBy(QuestionTable.id)
Expand Down Expand Up @@ -165,7 +166,7 @@ class PostgresUserRepository {
AnswerTable
.join(ContentTable, INNER, AnswerTable.data, ContentTable.id)
.join(UserTable, INNER, ContentTable.author, UserTable.id)
.join(VoteTable, INNER, VoteTable.content, ContentTable.id)
.join(VoteTable, LEFT, VoteTable.content, ContentTable.id)
.slice(AnswerTable.id, text, createdAt, author, votes)
.select { contentFilters(ids, queryParams.fromDate, queryParams.toDate) }
.groupBy(AnswerTable.id)
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/io/ktor/answers/db/postgres/schema.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.ktor.answers.db.postgres

import kotlinx.datetime.toKotlinLocalDateTime
import kotlinx.datetime.Clock
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.ReferenceOption
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.kotlin.datetime.datetime
import java.time.LocalDateTime
import org.jetbrains.exposed.sql.kotlin.datetime.timestamp

object UserTable : LongIdTable("users") {
val name = varchar("name", 50).uniqueIndex()
val passwordHash = varchar("password_hash", 100)
val active = bool("active").default(false)
val email = text("email").uniqueIndex()
val createdAt = datetime("created_at").clientDefault { LocalDateTime.now().toKotlinLocalDateTime() }
val createdAt = timestamp("created_at").clientDefault { Clock.System.now() }
val displayName = varchar("display_name", 80).uniqueIndex()
val location = text("location").nullable()
val aboutMe = text("about_me").nullable()
Expand All @@ -34,15 +33,15 @@ object UserRole : Table("user_role") {
object ContentTable : LongIdTable("content") {
val text = text("text")
val author = reference("author_id", UserTable, onDelete = ReferenceOption.CASCADE).index()
val createdAt = datetime("created_at").clientDefault { LocalDateTime.now().toKotlinLocalDateTime() }.index()
val createdAt = timestamp("created_at").clientDefault { Clock.System.now() }

}

object VoteTable : LongIdTable("vote") {
val voter = reference("voter", UserTable, onDelete = ReferenceOption.CASCADE).index()
val content = reference("content", ContentTable, onDelete = ReferenceOption.CASCADE).index()
val value = short("value")
val createdAt = datetime("created_at").clientDefault { LocalDateTime.now().toKotlinLocalDateTime() }
val createdAt = timestamp("created_at").clientDefault { Clock.System.now() }
}

object QuestionTable : LongIdTable("question") {
Expand Down
27 changes: 14 additions & 13 deletions src/main/kotlin/io/ktor/answers/model/DTOClasses.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.ktor.answers.model

import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable

Expand All @@ -20,9 +21,9 @@ enum class UserType {
sealed interface Post {
val postId: Int
val postType: PostType
val creationDate: LocalDateTime
val lastActivityDate: LocalDateTime
val lastEditDate: LocalDateTime
val creationDate: Instant
val lastActivityDate: Instant
val lastEditDate: Instant
val link: String
val title: String
val body: String
Expand All @@ -36,9 +37,9 @@ sealed interface Post {
data class Question(
override val postId: Int,
override val postType: PostType,
override val creationDate: LocalDateTime,
override val lastActivityDate: LocalDateTime,
override val lastEditDate: LocalDateTime,
override val creationDate: Instant,
override val lastActivityDate: Instant,
override val lastEditDate: Instant,
override val link: String,
override val title: String,
override val body: String,
Expand All @@ -55,9 +56,9 @@ data class Question(
data class Answer(
override val postId: Int,
override val postType: PostType,
override val creationDate: LocalDateTime,
override val lastActivityDate: LocalDateTime,
override val lastEditDate: LocalDateTime,
override val creationDate: Instant,
override val lastActivityDate: Instant,
override val lastEditDate: Instant,
override val link: String,
override val title: String,
override val body: String,
Expand All @@ -82,7 +83,7 @@ data class User(
val userId: Int,
val userType: UserType,
val displayName: String,
val creationDate: LocalDateTime,
val creationDate: Instant,
val link: String,
val location: String? = null,
val aboutMe: String? = null
Expand All @@ -94,9 +95,9 @@ data class User(
data class QuestionData(
val postId: Int? = null,
val postType: PostType? = null,
val creationDate: LocalDateTime? = null,
val lastActivityDate: LocalDateTime? = null,
val lastEditDate: LocalDateTime? = null,
val creationDate: Instant? = null,
val lastActivityDate: Instant? = null,
val lastEditDate: Instant? = null,
val link: String? = null,
val title: String? = null,
val body: String? = null,
Expand Down
11 changes: 4 additions & 7 deletions src/main/kotlin/io/ktor/answers/model/old/DTOClasses.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package io.ktor.answers.model.old

import kotlinx.datetime.Clock
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.TimeZone
import kotlinx.datetime.toLocalDateTime
import kotlinx.datetime.*
import kotlinx.serialization.Serializable

@Serializable
Expand All @@ -23,7 +20,7 @@ data class UserDTO(
data class AnswerDTO(
val id: Long,
val text: String,
val createdAt: LocalDateTime,
val createdAt: Instant,
val authorId: Long,
val votes: Int
)
Expand All @@ -33,7 +30,7 @@ data class QuestionDTO(
val id: Long,
val title: String,
val text: String,
val createdAt: LocalDateTime,
val createdAt: Instant,
val authorId: Long,
val votes: Int
)
Expand All @@ -42,7 +39,7 @@ data class QuestionDTO(
data class CommentDTO(
val value: Long,
val text: String,
val createdAt: LocalDateTime,
val createdAt: Instant,
val authorId: Long,
val votes: Int
)
63 changes: 63 additions & 0 deletions src/test/kotlin/io/ktor/answers/DbTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.jetbrains.exposed.sql.deleteAll
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.testcontainers.junit.jupiter.Testcontainers
import kotlin.properties.Delegates.notNull
import kotlin.random.Random
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
Expand Down Expand Up @@ -207,6 +208,68 @@ class DbTest : AbstractDbTest() {
assertEquals("answer1", sortedByCreation[1].text)
assertEquals("answer3", sortedByCreation[2].text)
}

@Test
fun `question without votes should be visible`() = runTest {
var questionAuthor: UserDAO by notNull()
suspendTransaction {
questionAuthor = createRandomUser()
QuestionDAO.new {
title = "q1"
data = Content.new {
text = "q1"
author = questionAuthor
}
}
}
assertEquals(1, userRepository.questionsByIds(listOf(questionAuthor.id.value)).size)
}

@Test
fun `answer without votes should be visible`() = runTest {
var answerAuthor: UserDAO by notNull()
suspendTransaction {
val q = QuestionDAO.new {
title = "q1"
data = Content.new {
text = "q1"
author = createRandomUser()
}
}
answerAuthor = createRandomUser()
AnswerDAO.new {
data = Content.new {
text = "a1"
author = answerAuthor
}
question = q
}
}
assertEquals(1, userRepository.answersByIds(listOf(answerAuthor.id.value)).size)
}

@Test
fun `comment without votes should be visible`() = runTest {
var commentAuthor: UserDAO by notNull()
suspendTransaction {
val q = QuestionDAO.new {
title = "q1"
data = Content.new {
text = "q1"
author = createRandomUser()
}
}
commentAuthor = createRandomUser()
CommentDAO.new {
data = Content.new {
text = "q1"
author = commentAuthor
}
parent = q.data
}
}
assertEquals(1, userRepository.commentsByIds(listOf(commentAuthor.id.value)).size)
}
}

val charPool = (('a'..'z') + ('A'..'Z') + ('0'..'9')).toCharArray()
Expand Down