From 786b0bc949430a3d5178515575370ab0588bccfa Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Sat, 20 Mar 2021 01:57:10 +0100 Subject: [PATCH] create comment.toOutput() --- .../fr/dcproject/common/dto/CreatedAt.kt | 12 ---- .../fr/dcproject/common/dto/CreatedBy.kt | 13 ---- .../fr/dcproject/common/dto/Versionable.kt | 14 ---- .../common/{dto => response}/Paginated.kt | 2 +- .../article/routes/FindArticleVersions.kt | 2 +- .../component/article/routes/FindArticles.kt | 1 - .../component/citizen/routes/FindCitizens.kt | 2 +- .../article/routes/CreateCommentArticle.kt | 31 +------- .../article/routes/GetArticleComments.kt | 33 +-------- .../routes/GetCitizenArticleComments.kt | 33 +-------- .../routes/CreateConstitutionComment.kt | 30 +------- .../routes/GetCitizenCommentConstitution.kt | 33 +-------- .../routes/GetConstitutionComment.kt | 33 +-------- .../generic/routes/CreateCommentChildren.kt | 3 +- .../comment/generic/routes/EditComment.kt | 32 +-------- .../generic/routes/GetCommentChildren.kt | 33 +-------- .../comment/generic/routes/GetOneComment.kt | 32 +-------- .../dcproject/component/comment/response.kt | 70 +++++++++++++++++++ .../constitution/routes/FindConstitutions.kt | 1 - 19 files changed, 94 insertions(+), 316 deletions(-) delete mode 100644 src/main/kotlin/fr/dcproject/common/dto/CreatedAt.kt delete mode 100644 src/main/kotlin/fr/dcproject/common/dto/CreatedBy.kt delete mode 100644 src/main/kotlin/fr/dcproject/common/dto/Versionable.kt rename src/main/kotlin/fr/dcproject/common/{dto => response}/Paginated.kt (93%) create mode 100644 src/main/kotlin/fr/dcproject/component/comment/response.kt diff --git a/src/main/kotlin/fr/dcproject/common/dto/CreatedAt.kt b/src/main/kotlin/fr/dcproject/common/dto/CreatedAt.kt deleted file mode 100644 index e50653c..0000000 --- a/src/main/kotlin/fr/dcproject/common/dto/CreatedAt.kt +++ /dev/null @@ -1,12 +0,0 @@ -package fr.dcproject.common.dto - -import org.joda.time.DateTime -import fr.dcproject.common.entity.CreatedAt as EntityCreatedAt - -interface CreatedAt { - val createdAt: DateTime - - class Imp(parent: EntityCreatedAt) : CreatedAt { - override val createdAt: DateTime = parent.createdAt - } -} diff --git a/src/main/kotlin/fr/dcproject/common/dto/CreatedBy.kt b/src/main/kotlin/fr/dcproject/common/dto/CreatedBy.kt deleted file mode 100644 index b7c2616..0000000 --- a/src/main/kotlin/fr/dcproject/common/dto/CreatedBy.kt +++ /dev/null @@ -1,13 +0,0 @@ -package fr.dcproject.common.dto - -import fr.dcproject.component.citizen.database.CitizenI -import java.util.UUID -import fr.dcproject.common.entity.CreatedBy as EntityCreatedBy - -interface CreatedBy { - val createdBy: UUID - - class Imp(parent: EntityCreatedBy) : CreatedBy { - override val createdBy: UUID = parent.createdBy.id - } -} diff --git a/src/main/kotlin/fr/dcproject/common/dto/Versionable.kt b/src/main/kotlin/fr/dcproject/common/dto/Versionable.kt deleted file mode 100644 index a4fd505..0000000 --- a/src/main/kotlin/fr/dcproject/common/dto/Versionable.kt +++ /dev/null @@ -1,14 +0,0 @@ -package fr.dcproject.common.dto - -import fr.dcproject.common.entity.Versionable as VersionableEntity -import java.util.UUID - -interface Versionable { - val versionId: UUID - val versionNumber: Int - - class Imp(parent: VersionableEntity) : Versionable { - override val versionNumber: Int = parent.versionNumber - override val versionId: UUID = parent.versionId - } -} diff --git a/src/main/kotlin/fr/dcproject/common/dto/Paginated.kt b/src/main/kotlin/fr/dcproject/common/response/Paginated.kt similarity index 93% rename from src/main/kotlin/fr/dcproject/common/dto/Paginated.kt rename to src/main/kotlin/fr/dcproject/common/response/Paginated.kt index 1c5e9aa..a3416cb 100644 --- a/src/main/kotlin/fr/dcproject/common/dto/Paginated.kt +++ b/src/main/kotlin/fr/dcproject/common/response/Paginated.kt @@ -1,4 +1,4 @@ -package fr.dcproject.common.dto +package fr.dcproject.common.response import fr.dcproject.common.entity.EntityI import fr.postgresjson.connexion.Paginated diff --git a/src/main/kotlin/fr/dcproject/component/article/routes/FindArticleVersions.kt b/src/main/kotlin/fr/dcproject/component/article/routes/FindArticleVersions.kt index 0667aba..d9deb0d 100644 --- a/src/main/kotlin/fr/dcproject/component/article/routes/FindArticleVersions.kt +++ b/src/main/kotlin/fr/dcproject/component/article/routes/FindArticleVersions.kt @@ -1,6 +1,6 @@ package fr.dcproject.component.article.routes -import fr.dcproject.common.dto.toOutput +import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.article.ArticleAccessControl import fr.dcproject.component.article.database.ArticleForListing diff --git a/src/main/kotlin/fr/dcproject/component/article/routes/FindArticles.kt b/src/main/kotlin/fr/dcproject/component/article/routes/FindArticles.kt index 8ada3c1..b0b5abd 100644 --- a/src/main/kotlin/fr/dcproject/component/article/routes/FindArticles.kt +++ b/src/main/kotlin/fr/dcproject/component/article/routes/FindArticles.kt @@ -1,6 +1,5 @@ package fr.dcproject.component.article.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.article.ArticleAccessControl diff --git a/src/main/kotlin/fr/dcproject/component/citizen/routes/FindCitizens.kt b/src/main/kotlin/fr/dcproject/component/citizen/routes/FindCitizens.kt index d3d970c..d3b1ef4 100644 --- a/src/main/kotlin/fr/dcproject/component/citizen/routes/FindCitizens.kt +++ b/src/main/kotlin/fr/dcproject/component/citizen/routes/FindCitizens.kt @@ -1,6 +1,6 @@ package fr.dcproject.component.citizen.routes -import fr.dcproject.common.dto.toOutput +import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.citizen.CitizenAccessControl diff --git a/src/main/kotlin/fr/dcproject/component/comment/article/routes/CreateCommentArticle.kt b/src/main/kotlin/fr/dcproject/component/comment/article/routes/CreateCommentArticle.kt index aabed2a..f4a103e 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/article/routes/CreateCommentArticle.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/article/routes/CreateCommentArticle.kt @@ -10,6 +10,7 @@ import fr.dcproject.component.comment.article.database.CommentArticleRepository import fr.dcproject.component.comment.article.routes.CreateCommentArticle.PostArticleCommentRequest.Input import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentForUpdate +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -17,7 +18,6 @@ import io.ktor.locations.Location import io.ktor.locations.post import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -39,35 +39,10 @@ object CreateCommentArticle { }.let { comment -> ac.assert { canCreate(comment, citizenOrNull) } repo.comment(comment) + call.respond( HttpStatusCode.Created, - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.createdAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = object { - val up: Int = 0 - val neutral: Int = 0 - val down: Int = 0 - val total: Int = 0 - val score: Int = 0 - } - } + comment.toOutput() ) } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt index 952ccbe..896cad6 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetArticleComments.kt @@ -1,12 +1,12 @@ package fr.dcproject.component.comment.article.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.article.database.ArticleRef import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.article.database.CommentArticleRepository import fr.dcproject.component.comment.generic.CommentAccessControl +import fr.dcproject.component.comment.toOutput import fr.dcproject.routes.PaginatedRequest import fr.dcproject.routes.PaginatedRequestI import io.ktor.application.call @@ -16,7 +16,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -42,35 +41,7 @@ object GetArticleComments { call.respond( HttpStatusCode.OK, comments.toOutput { comment -> - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() } ) } diff --git a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetCitizenArticleComments.kt b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetCitizenArticleComments.kt index 4c92845..3e4409f 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetCitizenArticleComments.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/article/routes/GetCitizenArticleComments.kt @@ -1,12 +1,12 @@ package fr.dcproject.component.comment.article.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.citizen.database.CitizenRef import fr.dcproject.component.comment.article.database.CommentArticleRepository import fr.dcproject.component.comment.generic.CommentAccessControl +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -14,7 +14,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -31,35 +30,7 @@ object GetCitizenArticleComments { call.respond( HttpStatusCode.OK, comments.toOutput { comment -> - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() } ) } diff --git a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/CreateConstitutionComment.kt b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/CreateConstitutionComment.kt index 93d4d66..e1034c7 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/CreateConstitutionComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/CreateConstitutionComment.kt @@ -9,6 +9,7 @@ import fr.dcproject.component.comment.constitution.database.CommentConstitutionR import fr.dcproject.component.comment.constitution.routes.CreateConstitutionComment.CreateConstitutionCommentRequest.Input import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentForUpdate +import fr.dcproject.component.comment.toOutput import fr.dcproject.component.constitution.database.ConstitutionRef import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -17,7 +18,6 @@ import io.ktor.locations.Location import io.ktor.locations.post import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -42,33 +42,7 @@ object CreateConstitutionComment { call.respond( HttpStatusCode.Created, - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.createdAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = object { - val up: Int = 0 - val neutral: Int = 0 - val down: Int = 0 - val total: Int = 0 - val score: Int = 0 - } - } + comment.toOutput() ) } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetCitizenCommentConstitution.kt b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetCitizenCommentConstitution.kt index b6d74ab..3296bea 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetCitizenCommentConstitution.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetCitizenCommentConstitution.kt @@ -1,12 +1,12 @@ package fr.dcproject.component.comment.constitution.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.citizen.database.CitizenRef import fr.dcproject.component.comment.constitution.database.CommentConstitutionRepository import fr.dcproject.component.comment.generic.CommentAccessControl +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.http.HttpStatusCode import io.ktor.locations.KtorExperimentalLocationsAPI @@ -14,7 +14,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -31,35 +30,7 @@ object GetCitizenCommentConstitution { call.respond( HttpStatusCode.OK, comments.toOutput { comment -> - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() } ) } diff --git a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetConstitutionComment.kt b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetConstitutionComment.kt index e92d3e8..40ae7ef 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetConstitutionComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/constitution/routes/GetConstitutionComment.kt @@ -1,11 +1,11 @@ package fr.dcproject.component.comment.constitution.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.constitution.database.CommentConstitutionRepository import fr.dcproject.component.comment.generic.CommentAccessControl +import fr.dcproject.component.comment.toOutput import fr.dcproject.component.constitution.database.ConstitutionRef import io.ktor.application.call import io.ktor.http.HttpStatusCode @@ -14,7 +14,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -31,35 +30,7 @@ object GetConstitutionComment { call.respond( HttpStatusCode.OK, comments.toOutput { comment -> - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() } ) } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/CreateCommentChildren.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/CreateCommentChildren.kt index ad54bbb..c05b973 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/CreateCommentChildren.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/CreateCommentChildren.kt @@ -8,6 +8,7 @@ import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentForUpdate import fr.dcproject.component.comment.generic.database.CommentRef import fr.dcproject.component.comment.generic.database.CommentRepository +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -38,7 +39,7 @@ object CreateCommentChildren { ac.assert { canCreate(newComment, citizenOrNull) } repo.comment(newComment) - call.respond(HttpStatusCode.Created, newComment) + call.respond(HttpStatusCode.Created, newComment.toOutput()) } } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt index cd5b180..305fa32 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/EditComment.kt @@ -7,6 +7,7 @@ import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentRef import fr.dcproject.component.comment.generic.database.CommentRepository +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -15,7 +16,6 @@ import io.ktor.locations.Location import io.ktor.locations.put import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -36,35 +36,7 @@ object EditComment { call.respond( HttpStatusCode.OK, - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() ) } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetCommentChildren.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetCommentChildren.kt index 2f654f5..52d1c1e 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetCommentChildren.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetCommentChildren.kt @@ -1,11 +1,11 @@ package fr.dcproject.component.comment.generic.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentRepository +import fr.dcproject.component.comment.toOutput import fr.dcproject.routes.PaginatedRequest import fr.dcproject.routes.PaginatedRequestI import io.ktor.application.call @@ -15,7 +15,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -42,35 +41,7 @@ object GetCommentChildren { call.respond( HttpStatusCode.OK, comments.toOutput { comment -> - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() } ) } diff --git a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt index a1b664c..ae3314c 100644 --- a/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt +++ b/src/main/kotlin/fr/dcproject/component/comment/generic/routes/GetOneComment.kt @@ -6,6 +6,7 @@ import fr.dcproject.component.auth.citizenOrNull import fr.dcproject.component.comment.generic.CommentAccessControl import fr.dcproject.component.comment.generic.database.CommentRef import fr.dcproject.component.comment.generic.database.CommentRepository +import fr.dcproject.component.comment.toOutput import io.ktor.application.call import io.ktor.features.NotFoundException import io.ktor.http.HttpStatusCode @@ -14,7 +15,6 @@ import io.ktor.locations.Location import io.ktor.locations.get import io.ktor.response.respond import io.ktor.routing.Route -import org.joda.time.DateTime import java.util.UUID @KtorExperimentalLocationsAPI @@ -31,35 +31,7 @@ object GetOneComment { call.respond( HttpStatusCode.OK, - object { - val id: UUID = comment.id - val content: String = comment.content - val childrenCount: Int = comment.childrenCount ?: 0 - val createdAt: DateTime = comment.createdAt - val updatedAt: DateTime = comment.updatedAt - val parent: Any? = comment.parent?.let { p -> - object { - val id: UUID = p.id - val reference: String = p.reference - } - } - val target: Any = comment.target.let { t -> - object { - val id: UUID = t.id - val reference: String = t.reference - } - } - val createdBy: Any = comment.createdBy.toOutput() - val votes: Any = comment.votes.let { v -> - object { - val up: Int = v.up - val neutral: Int = v.neutral - val down: Int = v.down - val total: Int = v.total - val score: Int = v.score - } - } - } + comment.toOutput() ) } } diff --git a/src/main/kotlin/fr/dcproject/component/comment/response.kt b/src/main/kotlin/fr/dcproject/component/comment/response.kt new file mode 100644 index 0000000..fc2dbcb --- /dev/null +++ b/src/main/kotlin/fr/dcproject/component/comment/response.kt @@ -0,0 +1,70 @@ +package fr.dcproject.component.comment + +import fr.dcproject.common.response.toOutput +import fr.dcproject.component.citizen.database.CitizenCreatorI +import fr.dcproject.component.comment.generic.database.CommentForUpdate +import fr.dcproject.component.comment.generic.database.CommentForView +import org.joda.time.DateTime +import java.util.UUID + +fun CommentForView<*, *>.toOutput(): Any = this.let { c -> + object { + val id: UUID = c.id + val content: String = c.content + val childrenCount: Int = c.childrenCount ?: 0 + val createdAt: DateTime = c.createdAt + val updatedAt: DateTime = c.updatedAt + val parent: Any? = c.parent?.let { p -> + object { + val id: UUID = p.id + val reference: String = p.reference + } + } + val target: Any = c.target.let { t -> + object { + val id: UUID = t.id + val reference: String = t.reference + } + } + val createdBy: Any = c.createdBy.toOutput() + val votes: Any = c.votes.let { v -> + object { + val up: Int = v.up + val neutral: Int = v.neutral + val down: Int = v.down + val total: Int = v.total + val score: Int = v.score + } + } + } +} + +fun CommentForUpdate<*, C>.toOutput(): Any = this.let { c -> + object { + val id: UUID = c.id + val content: String = c.content + val childrenCount: Int = 0 + val createdAt: DateTime = c.createdAt + val updatedAt: DateTime = c.createdAt + val parent: Any? = c.parent?.let { p -> + object { + val id: UUID = p.id + val reference: String = p.reference + } + } + val target: Any = c.target.let { t -> + object { + val id: UUID = t.id + val reference: String = t.reference + } + } + val createdBy: Any = c.createdBy.toOutput() + val votes: Any = object { + val up: Int = 0 + val neutral: Int = 0 + val down: Int = 0 + val total: Int = 0 + val score: Int = 0 + } + } +} diff --git a/src/main/kotlin/fr/dcproject/component/constitution/routes/FindConstitutions.kt b/src/main/kotlin/fr/dcproject/component/constitution/routes/FindConstitutions.kt index 8cfa782..f7184f2 100644 --- a/src/main/kotlin/fr/dcproject/component/constitution/routes/FindConstitutions.kt +++ b/src/main/kotlin/fr/dcproject/component/constitution/routes/FindConstitutions.kt @@ -1,6 +1,5 @@ package fr.dcproject.component.constitution.routes -import fr.dcproject.common.dto.toOutput import fr.dcproject.common.response.toOutput import fr.dcproject.common.security.assert import fr.dcproject.component.auth.citizenOrNull