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

107-comments-crud #401

Merged
merged 2 commits into from
Jul 21, 2024
Merged
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
30 changes: 7 additions & 23 deletions src/commonMain/kotlin/com.github.ikovalyov.model/Article.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@ package com.github.ikovalyov.model
import com.benasher44.uuid.Uuid
import com.benasher44.uuid.uuidFrom
import com.github.ikovalyov.model.markers.IEditable
import com.github.ikovalyov.model.security.User
import com.github.ikovalyov.model.serializer.UuidSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class Article(
override val id: Uuid,
val name: String,
val abstract: String,
val body: String,
val author: User,
val tags: List<String>?,
val meta: List<String>?,
val template: Template?, // Template uuid
val userList: List<User>,
val templateList: List<Template>,
) : IEditable {
data class Article(override val id: Uuid, val name: String, val abstract: String, val body: String, val author: Uuid, val tags: List<String>?, val meta: List<String>?, val template: Uuid?) : IEditable {
override fun getMetadata(): List<IEditable.EditableMetadata<*, Article>> = listOf(
IEditable.EditableMetadata(
fieldType = IEditable.FieldType.Id,
Expand Down Expand Up @@ -102,12 +90,10 @@ data class Article(
fieldType = IEditable.FieldType.Author,
readOnly = false,
serialize = {
it.id.toString()
it.toString()
},
deserialize = { uuid ->
userList.first {
it.id.toString() == uuid
}
uuidFrom(uuid)
},
update = {
copy(author = it)
Expand All @@ -116,7 +102,7 @@ data class Article(
author
},
fieldName = "Author",
predefinedList = userList,
predefinedList = null,
),
IEditable.EditableMetadata(
fieldType = IEditable.FieldType.Tags,
Expand Down Expand Up @@ -158,12 +144,10 @@ data class Article(
fieldType = IEditable.FieldType.Template,
readOnly = false,
serialize = {
it.id.toString()
it.toString()
},
deserialize = { uuid ->
templateList.first {
it.id.toString() == uuid
}
uuidFrom(uuid)
},
update = {
copy(template = it)
Expand All @@ -172,7 +156,7 @@ data class Article(
template
},
fieldName = "Template",
predefinedList = templateList,
predefinedList = null,
),
)

Expand Down
23 changes: 9 additions & 14 deletions src/commonMain/kotlin/com.github.ikovalyov.model/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ package com.github.ikovalyov.model
import com.benasher44.uuid.Uuid
import com.benasher44.uuid.uuidFrom
import com.github.ikovalyov.model.markers.IEditable
import com.github.ikovalyov.model.security.User
import com.github.ikovalyov.model.serializer.UuidSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class Comment(override val id: Uuid, val body: String, val author: User, val article: Article, val userList: List<User>, val articleList: List<Article>) : IEditable {
data class Comment(override val id: Uuid, val body: String, val author: Uuid, val article: Uuid) : IEditable {
override fun getMetadata(): List<IEditable.EditableMetadata<*, out IEditable>> = listOf(
IEditable.EditableMetadata(
fieldType = IEditable.FieldType.Id,
Expand All @@ -35,7 +34,7 @@ data class Comment(override val id: Uuid, val body: String, val author: User, va
),
IEditable.EditableMetadata(
fieldType = IEditable.FieldType.Body,
readOnly = true,
readOnly = false,
serialize = {
it
},
Expand All @@ -55,12 +54,10 @@ data class Comment(override val id: Uuid, val body: String, val author: User, va
fieldType = IEditable.FieldType.Author,
readOnly = false,
serialize = {
it.id.toString()
it.toString()
},
deserialize = { uuid ->
userList.first {
it.id.toString() == uuid
}
uuidFrom(uuid)
},
update = {
copy(author = it)
Expand All @@ -69,27 +66,25 @@ data class Comment(override val id: Uuid, val body: String, val author: User, va
author
},
fieldName = "Author",
predefinedList = userList,
predefinedList = null,
),
IEditable.EditableMetadata(
fieldType = IEditable.FieldType.Article,
readOnly = false,
serialize = {
it.id.toString()
it.toString()
},
deserialize = { uuid ->
articleList.first {
it.id.toString() == uuid
}
uuidFrom(uuid)
},
update = {
copy(article = it)
},
get = {
article
},
fieldName = "Author",
predefinedList = articleList,
fieldName = "Article",
predefinedList = null,
),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.ikovalyov.model.markers

import com.benasher44.uuid.Uuid
import com.github.ikovalyov.model.security.User
import kotlinx.datetime.Instant

interface IEditable {
Expand All @@ -18,7 +17,7 @@ interface IEditable {

sealed class FieldType<T : Any> {
object Id : FieldType<Uuid>()
object Author : FieldType<User>()
object Author : FieldType<Uuid>()
object LastModified : FieldType<Instant>()
object Body : FieldType<String>()
object Name : FieldType<String>()
Expand All @@ -30,8 +29,8 @@ interface IEditable {
object StringListFiledType : FieldType<List<String>>()
object Tags : FieldType<List<String>>()
object Metadata : FieldType<List<String>>()
object Template : FieldType<com.github.ikovalyov.model.Template>()
object Article : FieldType<com.github.ikovalyov.model.Article>()
object Template : FieldType<Uuid>()
object Article : FieldType<Uuid>()
}

val id: Uuid
Expand Down
11 changes: 6 additions & 5 deletions src/commonMain/kotlin/com/github/ikovalyov/Api.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.github.ikovalyov

object Api {
const val ARTICLE_API_URL = "/application/api/article"
const val TAG_API_URL = "/application/api/tag"
const val TEMPLATE_API_URL = "/application/api/template"
const val ARTICLES_API_URL = "/application/api/articles"
const val COMMENTS_API_URL = "/application/api/comments"
const val TAGS_API_URL = "/application/api/tags"
const val TEMPLATES_API_URL = "/application/api/templates"
const val RECORDS_API_URL = "/application/api/records"
const val VIEWS_API_URL = "/application/api/views"
const val USER_ROLE_API_URL = "/application/api/user-role"
const val USER_API_URL = "/application/api/user"
const val USER_ROLES_API_URL = "/application/api/user-roles"
const val USERS_API_URL = "/application/api/users"
const val USER_URL = "/application/user"
const val BACKEND_ENDPOINT = "http://localhost:8082"
const val FRONTEND_ENDPOINT = "http://localhost:8080"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class TemplateService {
suspend fun getAllTemplates(): List<Template> {
val result = kotlin.runCatching {
val result = window.fetch(
Api.BACKEND_ENDPOINT + Api.TEMPLATE_API_URL,
Api.BACKEND_ENDPOINT + Api.TEMPLATES_API_URL,
RequestInit(
credentials = RequestCredentials.INCLUDE,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class UserService {
suspend fun getAllUsers(): List<User> {
val result = kotlin.runCatching {
val result = window.fetch(
Api.BACKEND_ENDPOINT + Api.USER_API_URL,
Api.BACKEND_ENDPOINT + Api.USERS_API_URL,
RequestInit(
credentials = RequestCredentials.INCLUDE,
),
Expand All @@ -36,7 +36,7 @@ class UserService {

suspend fun getUser(userId: Uuid): User {
val result = window.fetch(
Api.BACKEND_ENDPOINT + Api.USER_API_URL + "/$userId",
Api.BACKEND_ENDPOINT + Api.USERS_API_URL + "/$userId",
RequestInit(
credentials = RequestCredentials.INCLUDE,
),
Expand Down
33 changes: 25 additions & 8 deletions src/jsMain/kotlin/com.github.ikovalyov/routes/Index.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.ikovalyov.routes
import com.benasher44.uuid.uuid4
import com.github.ikovalyov.Api
import com.github.ikovalyov.model.Article
import com.github.ikovalyov.model.Comment
import com.github.ikovalyov.model.Tag
import com.github.ikovalyov.model.Template
import com.github.ikovalyov.model.security.Email
Expand Down Expand Up @@ -67,7 +68,7 @@ val Index = FC<IndexProps> { props ->
decodeItems = {
Json.decodeFromString(ListSerializer(Template.serializer()), it)
}
apiUri = Api.TEMPLATE_API_URL
apiUri = Api.TEMPLATES_API_URL
factory = {
Template(id = uuid4(), "", "")
}
Expand All @@ -80,7 +81,7 @@ val Index = FC<IndexProps> { props ->
decodeItems = {
Json.decodeFromString(ListSerializer(UserRole.serializer()), it)
}
apiUri = Api.USER_ROLE_API_URL
apiUri = Api.USER_ROLES_API_URL
factory = {
UserRole(uuid4(), Clock.System.now(), "")
}
Expand All @@ -93,7 +94,7 @@ val Index = FC<IndexProps> { props ->
decodeItems = {
Json.decodeFromString(ListSerializer(User.serializer()), it)
}
apiUri = Api.USER_API_URL
apiUri = Api.USERS_API_URL
factory = {
User(
uuid4(),
Expand All @@ -113,19 +114,17 @@ val Index = FC<IndexProps> { props ->
decodeItems = {
Json.decodeFromString(ListSerializer(Article.serializer()), it)
}
apiUri = Api.ARTICLE_API_URL
apiUri = Api.ARTICLES_API_URL
factory = {
Article(
id = uuid4(),
name = "",
abstract = "",
body = "",
author = currentUser,
author = currentUser.id,
tags = emptyList(),
meta = emptyList(),
template = null,
userList = props.userList,
templateList = props.templateList,
)
}
header = "Articles"
Expand All @@ -137,7 +136,7 @@ val Index = FC<IndexProps> { props ->
decodeItems = {
Json.decodeFromString(ListSerializer(Tag.serializer()), it)
}
apiUri = Api.TAG_API_URL
apiUri = Api.TAGS_API_URL
factory = {
Tag(
id = uuid4(),
Expand All @@ -146,6 +145,24 @@ val Index = FC<IndexProps> { props ->
}
header = "Tags"
}
crudComponent {
decodeItem = {
Json.decodeFromString(Comment.serializer(), it)
}
decodeItems = {
Json.decodeFromString(ListSerializer(Comment.serializer()), it)
}
apiUri = Api.COMMENTS_API_URL
factory = {
Comment(
id = uuid4(),
body = "",
author = currentUser.id,
article = uuid4(),
)
}
header = "Comments"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import io.micronaut.security.rules.SecurityRule
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json

@Controller(Api.ARTICLE_API_URL)
@Controller(Api.ARTICLES_API_URL)
@Secured(SecurityRule.IS_AUTHENTICATED)
class ArticlesController(private val articlesRepository: ArticlesRepository) {
@Get
Expand All @@ -32,9 +32,8 @@ class ArticlesController(private val articlesRepository: ArticlesRepository) {

@Post("/")
@Consumes(MediaType.APPLICATION_JSON)
suspend fun insert(@Body item: String): HttpResponse<Nothing> {
val article = Json.decodeFromString(Article.serializer(), item)
val result = articlesRepository.insert(article)
suspend fun insert(@Body item: Article): HttpResponse<Nothing> {
val result = articlesRepository.insert(item)
return if (result) {
HttpResponse.accepted()
} else {
Expand All @@ -53,9 +52,8 @@ class ArticlesController(private val articlesRepository: ArticlesRepository) {
}

@Patch
suspend fun update(@Body item: String): HttpResponse<Nothing> {
val article = Json.decodeFromString(Article.serializer(), item)
val result = articlesRepository.update(article)
suspend fun update(@Body item: Article): HttpResponse<Nothing> {
val result = articlesRepository.update(item)
return if (result) {
HttpResponse.accepted()
} else {
Expand Down
Loading