Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed Dec 19, 2023
1 parent 4f98e08 commit 108a298
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 73 deletions.
1 change: 0 additions & 1 deletion backend/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ dependencies {
"implementation"("com.hexagonkt:http_server_jetty:$hexagonVersion")
"implementation"("com.hexagonkt:logging_slf4j_jul:$hexagonVersion")
"implementation"("com.hexagonkt:rest:$hexagonVersion")
"implementation"("com.hexagonkt.extra:converters:$hexagonExtraVersion")
"implementation"("com.hexagonkt.extra:store_mongodb:$hexagonExtraVersion")
"implementation"("com.auth0:java-jwt:$javaJwtVersion")

Expand Down
148 changes: 76 additions & 72 deletions backend/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.hexagonkt.realworld

import com.hexagonkt.converters.ConvertersManager
import com.hexagonkt.converters.convert
import com.hexagonkt.core.*
import com.hexagonkt.realworld.domain.model.Article
import com.hexagonkt.realworld.domain.model.Comment
Expand All @@ -28,87 +26,93 @@ internal fun createJwt(settings: Settings): Jwt =
Jwt(urlOf(settings.keyStoreResource), settings.keyStorePassword, settings.keyPairAlias)

private fun createUserStore(settings: Settings): Store<User, String> {
val userStore = MongoDbStore(User::class, User::username, settings.mongodbUrl)
val userStore = MongoDbStore(
type = User::class,
key = User::username,
database = MongoDbStore.database(settings.mongodbUrl),
encoder = {
fieldsMapOfNotNull(
User::username to it.username,
User::email to it.email,
User::password to it.password,
User::bio to it.bio,
User::image to it.image,
User::following to it.following,
)
},
decoder = {
User(
username = it.requireString(User::username),
email = it.requireString(User::email),
password = it.requireString(User::password),
bio = it.getString(User::bio),
image = it.getString(User::image)?.let(::urlOf),
following = it.getStringsOrEmpty(User::following).toSet(),
)
},
)

val indexField = User::email.name
val indexOptions = IndexOptions().unique(true).background(true).name(indexField)
userStore.collection.createIndex(Indexes.ascending(indexField), indexOptions)

ConvertersManager.register(User::class to Map::class) {
fieldsMapOfNotNull(
User::username to it.username,
User::email to it.email,
User::password to it.password,
User::bio to it.bio,
User::image to it.image,
User::following to it.following,
)
}
ConvertersManager.register(Map::class to User::class) {
User(
username = it.requireString(User::username),
email = it.requireString(User::email),
password = it.requireString(User::password),
bio = it.getString(User::bio),
image = it.getString(User::image)?.let(::urlOf),
following = it.getStringsOrEmpty(User::following).toSet(),
)
}

return userStore
}

private fun createArticleStore(settings: Settings): Store<Article, String> {
val articleStore = MongoDbStore(Article::class, Article::slug, settings.mongodbUrl)
val articleStore = MongoDbStore(
type = Article::class,
key = Article::slug,
database = MongoDbStore.database(settings.mongodbUrl),
encoder = {
fieldsMapOfNotNull(
Article::slug to it.slug,
Article::author to it.author,
Article::title to it.title,
Article::description to it.description,
Article::body to it.body,
Article::tagList to it.tagList,
Article::createdAt to it.createdAt,
Article::updatedAt to it.updatedAt,
Article::favoritedBy to it.favoritedBy,
Article::comments to it.comments.map { m ->
fieldsMapOfNotNull(
Comment::id to m.id,
Comment::author to m.author,
Comment::body to m.body,
Comment::createdAt to m.createdAt,
Comment::updatedAt to m.updatedAt,
)
},
)
},
decoder = {
Article(
slug = it.requireString(Article::slug),
author = it.requireString(Article::author),
title = it.requireString(Article::title),
description = it.requireString(Article::description),
body = it.requireString(Article::body),
tagList = it.getStringsOrEmpty(Article::tagList).let(::LinkedHashSet),
createdAt = it.requireKey(Comment::createdAt),
updatedAt = it.requireKey(Comment::updatedAt),
favoritedBy = it.getStringsOrEmpty(Article::favoritedBy).toSet(),
comments = it.getMapsOrEmpty(Article::comments).map { m ->
Comment(
id = m.requireInt(Comment::id),
author = m.requireString(Comment::author),
body = m.requireString(Comment::body),
createdAt = m.requireKey(Comment::createdAt),
updatedAt = m.requireKey(Comment::updatedAt),
)
},
)
},
)

val indexField = Article::author.name
val indexOptions = IndexOptions().unique(false).background(true).name(indexField)
articleStore.collection.createIndex(Indexes.ascending(indexField), indexOptions)

ConvertersManager.register(Comment::class to Map::class) {
fieldsMapOfNotNull(
Comment::id to it.id,
Comment::author to it.author,
Comment::body to it.body,
Comment::createdAt to it.createdAt,
Comment::updatedAt to it.updatedAt,
)
}
ConvertersManager.register(Map::class to Comment::class) {
Comment(
id = it.requireInt(Comment::id),
author = it.requireString(Comment::author),
body = it.requireString(Comment::body),
createdAt = it.requireKey(Comment::createdAt),
updatedAt = it.requireKey(Comment::updatedAt),
)
}
ConvertersManager.register(Article::class to Map::class) {
fieldsMapOfNotNull(
Article::slug to it.slug,
Article::author to it.author,
Article::title to it.title,
Article::description to it.description,
Article::body to it.body,
Article::tagList to it.tagList,
Article::createdAt to it.createdAt,
Article::updatedAt to it.updatedAt,
Article::favoritedBy to it.favoritedBy,
Article::comments to it.comments.map { m -> m.convert(Map::class) },
)
}
ConvertersManager.register(Map::class to Article::class) {
Article(
slug = it.requireString(Article::slug),
author = it.requireString(Article::author),
title = it.requireString(Article::title),
description = it.requireString(Article::description),
body = it.requireString(Article::body),
tagList = it.getStringsOrEmpty(Article::tagList).let(::LinkedHashSet),
createdAt = it.requireKey(Comment::createdAt),
updatedAt = it.requireKey(Comment::updatedAt),
favoritedBy = it.getStringsOrEmpty(Article::favoritedBy).toSet(),
comments = it.getMapsOrEmpty(Article::comments).map { m -> m.convert(Comment::class) },
)
}

return articleStore
}

0 comments on commit 108a298

Please sign in to comment.