Remove Extra class, and create CommentRef

This commit is contained in:
2020-01-29 16:58:14 +01:00
parent 41a98f23b8
commit 3d2d3c2e14
6 changed files with 42 additions and 29 deletions

View File

@@ -79,7 +79,7 @@ fun Application.module(env: Env = PROD) {
decode { values, _ -> decode { values, _ ->
values.singleOrNull()?.let { values.singleOrNull()?.let {
get<RepositoryArticle>().findById(UUID.fromString(it)) get<RepositoryArticle>().findById(UUID.fromString(it))
?: throw InternalError("Article $values not found") ?: throw NotFoundException("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<CommentRef> {
decode { values, _ ->
values.singleOrNull()?.let {
CommentRef(UUID.fromString(it))
} ?: throw NotFoundException("Comment $values not found")
}
}
convert<Constitution> { convert<Constitution> {
decode { values, _ -> decode { values, _ ->
val id = values.singleOrNull()?.let { UUID.fromString(it) } val id = values.singleOrNull()?.let { UUID.fromString(it) }

View File

@@ -1,7 +1,6 @@
package fr.dcproject.entity package fr.dcproject.entity
import fr.postgresjson.entity.immutable.EntityUpdatedAt import fr.postgresjson.entity.immutable.*
import fr.postgresjson.entity.immutable.EntityUpdatedAtImp
import fr.postgresjson.entity.mutable.EntityDeletedAt import fr.postgresjson.entity.mutable.EntityDeletedAt
import fr.postgresjson.entity.mutable.EntityDeletedAtImp import fr.postgresjson.entity.mutable.EntityDeletedAtImp
import java.util.* import java.util.*
@@ -9,13 +8,16 @@ import java.util.*
open class Comment<T : TargetI>( open class Comment<T : TargetI>(
id: UUID = UUID.randomUUID(), id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic, override val createdBy: CitizenBasic,
target: T, override var target: T,
var content: String, var content: String,
val responses: List<Comment<T>>? = null, val responses: List<Comment<T>>? = null,
var parent: Comment<T>? = null, var parent: Comment<T>? = null,
val parentsIds: List<UUID>? = null, val parentsIds: List<UUID>? = null,
val childrenCount: Int? = null val childrenCount: Int? = null
) : Extra<T>(id, createdBy, target), ) : ExtraI<T>,
CommentRef(id),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<CitizenBasicI> by EntityCreatedByImp(createdBy),
EntityUpdatedAt by EntityUpdatedAtImp(), EntityUpdatedAt by EntityUpdatedAtImp(),
EntityDeletedAt by EntityDeletedAtImp(), EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp(), Votable by VotableImp(),
@@ -33,3 +35,7 @@ open class Comment<T : TargetI>(
override val reference get() = TargetI.getReference(this) override val reference get() = TargetI.getReference(this)
} }
open class CommentRef(id: UUID = UUID.randomUUID()) : CommentS(id)
sealed class CommentS(id: UUID) : UuidEntity(id)

View File

@@ -1,6 +1,9 @@
package fr.dcproject.entity 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 java.util.*
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.full.isSuperclassOf import kotlin.reflect.full.isSuperclassOf
@@ -9,19 +12,9 @@ interface ExtraI<T : TargetI> :
UuidEntityI, UuidEntityI,
EntityCreatedAt, EntityCreatedAt,
EntityCreatedBy<CitizenBasicI> { EntityCreatedBy<CitizenBasicI> {
var target: T val target: T
} }
abstract class Extra<T : TargetI>(
id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic,
override var target: T
) :
ExtraI<T>,
UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<CitizenBasicI> by EntityCreatedByImp(createdBy)
open class TargetRef(id: UUID = UUID.randomUUID()) : TargetI, UuidEntity(id) { open class TargetRef(id: UUID = UUID.randomUUID()) : TargetI, UuidEntity(id) {
override val reference: String = "" override val reference: String = ""
get() { get() {

View File

@@ -1,9 +1,13 @@
package fr.dcproject.entity package fr.dcproject.entity
import fr.postgresjson.entity.immutable.*
import java.util.* import java.util.*
class Follow<T : TargetI>( class Follow<T : TargetI>(
id: UUID = UUID.randomUUID(), id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic, override val createdBy: CitizenBasic,
target: T override var target: T
) : Extra<T>(id, createdBy, target) ) : ExtraI<T>,
UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<CitizenBasicI> by EntityCreatedByImp(createdBy)

View File

@@ -1,16 +1,18 @@
package fr.dcproject.entity package fr.dcproject.entity
import fr.postgresjson.entity.immutable.EntityUpdatedAt import fr.postgresjson.entity.immutable.*
import fr.postgresjson.entity.immutable.EntityUpdatedAtImp
import java.util.* import java.util.*
open class Vote<T : TargetI>( open class Vote<T : TargetI>(
id: UUID = UUID.randomUUID(), id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic, override val createdBy: CitizenBasic,
target: T, override var target: T,
var note: Int, var note: Int,
var anonymous: Boolean = true var anonymous: Boolean = true
) : Extra<T>(id, createdBy, target), ) : ExtraI<T>,
UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<CitizenBasicI> by EntityCreatedByImp(createdBy),
EntityUpdatedAt by EntityUpdatedAtImp() { EntityUpdatedAt by EntityUpdatedAtImp() {
init { init {
if (note > 1 && note < -1) { if (note > 1 && note < -1) {

View File

@@ -2,6 +2,7 @@ package fr.dcproject.routes
import fr.dcproject.citizen import fr.dcproject.citizen
import fr.dcproject.entity.Comment import fr.dcproject.entity.Comment
import fr.dcproject.entity.CommentRef
import fr.dcproject.routes.CommentPaths.CreateCommentRequest.Content import fr.dcproject.routes.CommentPaths.CreateCommentRequest.Content
import fr.dcproject.security.voter.CommentVoter.Action.* import fr.dcproject.security.voter.CommentVoter.Action.*
import fr.dcproject.security.voter.assertCan import fr.dcproject.security.voter.assertCan
@@ -19,9 +20,8 @@ import fr.dcproject.repository.CommentGeneric as CommentRepository
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
object CommentPaths { object CommentPaths {
// TODO: change UUID by entity converter
@Location("/comments/{comment}") @Location("/comments/{comment}")
class CommentRequest(val comment: UUID) class CommentRequest(val comment: CommentRef)
@Location("/comments/{comment}/children") @Location("/comments/{comment}/children")
class CommentChildrenRequest( class CommentChildrenRequest(
@@ -35,7 +35,7 @@ object CommentPaths {
} }
@Location("/comments/{comment}/children") @Location("/comments/{comment}/children")
class CreateCommentRequest(val comment: UUID) { class CreateCommentRequest(val comment: CommentRef) {
class Content(val content: String) class Content(val content: String)
} }
} }
@@ -44,7 +44,7 @@ object CommentPaths {
@KtorExperimentalLocationsAPI @KtorExperimentalLocationsAPI
fun Route.comment(repo: CommentRepository) { fun Route.comment(repo: CommentRepository) {
get<CommentPaths.CommentRequest> { get<CommentPaths.CommentRequest> {
val comment = repo.findById(it.comment)!! val comment = repo.findById(it.comment.id)!!
assertCan(VIEW, comment) assertCan(VIEW, comment)
call.respond(HttpStatusCode.OK, comment) call.respond(HttpStatusCode.OK, comment)
@@ -64,7 +64,7 @@ fun Route.comment(repo: CommentRepository) {
} }
post<CommentPaths.CreateCommentRequest> { post<CommentPaths.CreateCommentRequest> {
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( val newComment = Comment(
content = call.receive<Content>().content, content = call.receive<Content>().content,
createdBy = citizen, createdBy = citizen,
@@ -78,7 +78,7 @@ fun Route.comment(repo: CommentRepository) {
} }
put<CommentPaths.CommentRequest> { put<CommentPaths.CommentRequest> {
val comment = repo.findById(it.comment)!! val comment = repo.findById(it.comment.id)!!
assertCan(UPDATE, comment) assertCan(UPDATE, comment)
comment.content = call.receiveText() comment.content = call.receiveText()