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, _ ->
values.singleOrNull()?.let {
get<RepositoryArticle>().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<CommentRef> {
decode { values, _ ->
values.singleOrNull()?.let {
CommentRef(UUID.fromString(it))
} ?: throw NotFoundException("Comment $values not found")
}
}
convert<Constitution> {
decode { values, _ ->
val id = values.singleOrNull()?.let { UUID.fromString(it) }

View File

@@ -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<T : TargetI>(
id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic,
target: T,
override var target: T,
var content: String,
val responses: List<Comment<T>>? = null,
var parent: Comment<T>? = null,
val parentsIds: List<UUID>? = 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(),
EntityDeletedAt by EntityDeletedAtImp(),
Votable by VotableImp(),
@@ -33,3 +35,7 @@ open class Comment<T : TargetI>(
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
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<T : TargetI> :
UuidEntityI,
EntityCreatedAt,
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) {
override val reference: String = ""
get() {

View File

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

View File

@@ -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<T : TargetI>(
id: UUID = UUID.randomUUID(),
override val createdBy: CitizenBasic,
target: T,
override var target: T,
var note: Int,
var anonymous: Boolean = true
) : Extra<T>(id, createdBy, target),
) : ExtraI<T>,
UuidEntity(id),
EntityCreatedAt by EntityCreatedAtImp(),
EntityCreatedBy<CitizenBasicI> by EntityCreatedByImp(createdBy),
EntityUpdatedAt by EntityUpdatedAtImp() {
init {
if (note > 1 && note < -1) {

View File

@@ -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<CommentPaths.CommentRequest> {
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<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(
content = call.receive<Content>().content,
createdBy = citizen,
@@ -78,7 +78,7 @@ fun Route.comment(repo: CommentRepository) {
}
put<CommentPaths.CommentRequest> {
val comment = repo.findById(it.comment)!!
val comment = repo.findById(it.comment.id)!!
assertCan(UPDATE, comment)
comment.content = call.receiveText()