diff --git a/src/main/kotlin/fr/dcproject/Application.kt b/src/main/kotlin/fr/dcproject/Application.kt index eb37ef9..b15a730 100644 --- a/src/main/kotlin/fr/dcproject/Application.kt +++ b/src/main/kotlin/fr/dcproject/Application.kt @@ -79,7 +79,7 @@ fun Application.module(env: Env = PROD) { decode { values, _ -> values.singleOrNull()?.let { get().findById(UUID.fromString(it)) - ?: throw InternalError("Article $values not found") + ?: throw NotFoundException("Article $values not found") } ?: throw NotFoundException("Article $values not found") } } @@ -91,6 +91,14 @@ fun Application.module(env: Env = PROD) { } } + convert { + decode { values, _ -> + values.singleOrNull()?.let { + CommentRef(UUID.fromString(it)) + } ?: throw NotFoundException("Comment $values not found") + } + } + convert { decode { values, _ -> val id = values.singleOrNull()?.let { UUID.fromString(it) } diff --git a/src/main/kotlin/fr/dcproject/entity/Comment.kt b/src/main/kotlin/fr/dcproject/entity/Comment.kt index f53dc1a..7661790 100644 --- a/src/main/kotlin/fr/dcproject/entity/Comment.kt +++ b/src/main/kotlin/fr/dcproject/entity/Comment.kt @@ -1,7 +1,6 @@ package fr.dcproject.entity -import fr.postgresjson.entity.immutable.EntityUpdatedAt -import fr.postgresjson.entity.immutable.EntityUpdatedAtImp +import fr.postgresjson.entity.immutable.* import fr.postgresjson.entity.mutable.EntityDeletedAt import fr.postgresjson.entity.mutable.EntityDeletedAtImp import java.util.* @@ -9,13 +8,16 @@ import java.util.* open class Comment( id: UUID = UUID.randomUUID(), override val createdBy: CitizenBasic, - target: T, + override var target: T, var content: String, val responses: List>? = null, var parent: Comment? = null, val parentsIds: List? = null, val childrenCount: Int? = null -) : Extra(id, createdBy, target), +) : ExtraI, + CommentRef(id), + EntityCreatedAt by EntityCreatedAtImp(), + EntityCreatedBy by EntityCreatedByImp(createdBy), EntityUpdatedAt by EntityUpdatedAtImp(), EntityDeletedAt by EntityDeletedAtImp(), Votable by VotableImp(), @@ -33,3 +35,7 @@ open class Comment( override val reference get() = TargetI.getReference(this) } + +open class CommentRef(id: UUID = UUID.randomUUID()) : CommentS(id) + +sealed class CommentS(id: UUID) : UuidEntity(id) diff --git a/src/main/kotlin/fr/dcproject/entity/Extra.kt b/src/main/kotlin/fr/dcproject/entity/Extra.kt index 1c69c43..5d32250 100644 --- a/src/main/kotlin/fr/dcproject/entity/Extra.kt +++ b/src/main/kotlin/fr/dcproject/entity/Extra.kt @@ -1,6 +1,9 @@ package fr.dcproject.entity -import fr.postgresjson.entity.immutable.* +import fr.postgresjson.entity.immutable.EntityCreatedAt +import fr.postgresjson.entity.immutable.EntityCreatedBy +import fr.postgresjson.entity.immutable.UuidEntity +import fr.postgresjson.entity.immutable.UuidEntityI import java.util.* import kotlin.reflect.KClass import kotlin.reflect.full.isSuperclassOf @@ -9,19 +12,9 @@ interface ExtraI : UuidEntityI, EntityCreatedAt, EntityCreatedBy { - var target: T + val target: T } -abstract class Extra( - id: UUID = UUID.randomUUID(), - override val createdBy: CitizenBasic, - override var target: T -) : - ExtraI, - UuidEntity(id), - EntityCreatedAt by EntityCreatedAtImp(), - EntityCreatedBy by EntityCreatedByImp(createdBy) - open class TargetRef(id: UUID = UUID.randomUUID()) : TargetI, UuidEntity(id) { override val reference: String = "" get() { diff --git a/src/main/kotlin/fr/dcproject/entity/Follow.kt b/src/main/kotlin/fr/dcproject/entity/Follow.kt index 8b3ea4f..13eb5d5 100644 --- a/src/main/kotlin/fr/dcproject/entity/Follow.kt +++ b/src/main/kotlin/fr/dcproject/entity/Follow.kt @@ -1,9 +1,13 @@ package fr.dcproject.entity +import fr.postgresjson.entity.immutable.* import java.util.* class Follow( id: UUID = UUID.randomUUID(), override val createdBy: CitizenBasic, - target: T -) : Extra(id, createdBy, target) + override var target: T +) : ExtraI, + UuidEntity(id), + EntityCreatedAt by EntityCreatedAtImp(), + EntityCreatedBy by EntityCreatedByImp(createdBy) diff --git a/src/main/kotlin/fr/dcproject/entity/Vote.kt b/src/main/kotlin/fr/dcproject/entity/Vote.kt index e026cc3..e5e70ed 100644 --- a/src/main/kotlin/fr/dcproject/entity/Vote.kt +++ b/src/main/kotlin/fr/dcproject/entity/Vote.kt @@ -1,16 +1,18 @@ package fr.dcproject.entity -import fr.postgresjson.entity.immutable.EntityUpdatedAt -import fr.postgresjson.entity.immutable.EntityUpdatedAtImp +import fr.postgresjson.entity.immutable.* import java.util.* open class Vote( id: UUID = UUID.randomUUID(), override val createdBy: CitizenBasic, - target: T, + override var target: T, var note: Int, var anonymous: Boolean = true -) : Extra(id, createdBy, target), +) : ExtraI, + UuidEntity(id), + EntityCreatedAt by EntityCreatedAtImp(), + EntityCreatedBy by EntityCreatedByImp(createdBy), EntityUpdatedAt by EntityUpdatedAtImp() { init { if (note > 1 && note < -1) { diff --git a/src/main/kotlin/fr/dcproject/routes/Comment.kt b/src/main/kotlin/fr/dcproject/routes/Comment.kt index 3c8d246..4231711 100644 --- a/src/main/kotlin/fr/dcproject/routes/Comment.kt +++ b/src/main/kotlin/fr/dcproject/routes/Comment.kt @@ -2,6 +2,7 @@ package fr.dcproject.routes import fr.dcproject.citizen import fr.dcproject.entity.Comment +import fr.dcproject.entity.CommentRef import fr.dcproject.routes.CommentPaths.CreateCommentRequest.Content import fr.dcproject.security.voter.CommentVoter.Action.* import fr.dcproject.security.voter.assertCan @@ -19,9 +20,8 @@ import fr.dcproject.repository.CommentGeneric as CommentRepository @KtorExperimentalLocationsAPI object CommentPaths { - // TODO: change UUID by entity converter @Location("/comments/{comment}") - class CommentRequest(val comment: UUID) + class CommentRequest(val comment: CommentRef) @Location("/comments/{comment}/children") class CommentChildrenRequest( @@ -35,7 +35,7 @@ object CommentPaths { } @Location("/comments/{comment}/children") - class CreateCommentRequest(val comment: UUID) { + class CreateCommentRequest(val comment: CommentRef) { class Content(val content: String) } } @@ -44,7 +44,7 @@ object CommentPaths { @KtorExperimentalLocationsAPI fun Route.comment(repo: CommentRepository) { get { - val comment = repo.findById(it.comment)!! + val comment = repo.findById(it.comment.id)!! assertCan(VIEW, comment) call.respond(HttpStatusCode.OK, comment) @@ -64,7 +64,7 @@ fun Route.comment(repo: CommentRepository) { } post { - val parent = repo.findById(it.comment) ?: throw NotFoundException("Comment not found") + val parent = repo.findById(it.comment.id) ?: throw NotFoundException("Comment not found") val newComment = Comment( content = call.receive().content, createdBy = citizen, @@ -78,7 +78,7 @@ fun Route.comment(repo: CommentRepository) { } put { - val comment = repo.findById(it.comment)!! + val comment = repo.findById(it.comment.id)!! assertCan(UPDATE, comment) comment.content = call.receiveText()