Move Follow to a component
This commit is contained in:
@@ -29,6 +29,14 @@ import fr.dcproject.component.comment.generic.routes.createCommentChildren
|
||||
import fr.dcproject.component.comment.generic.routes.editComment
|
||||
import fr.dcproject.component.comment.generic.routes.getChildrenComments
|
||||
import fr.dcproject.component.comment.generic.routes.getOneComment
|
||||
import fr.dcproject.component.follow.routes.article.FollowArticle.followArticle
|
||||
import fr.dcproject.component.follow.routes.article.GetFollowArticle.getFollowArticle
|
||||
import fr.dcproject.component.follow.routes.article.GetMyFollowsArticle.getMyFollowsArticle
|
||||
import fr.dcproject.component.follow.routes.article.UnfollowArticle.unfollowArticle
|
||||
import fr.dcproject.component.follow.routes.constitution.FollowConstitution.followConstitution
|
||||
import fr.dcproject.component.follow.routes.constitution.GetFollowConstitution.getFollowConstitution
|
||||
import fr.dcproject.component.follow.routes.constitution.GetMyFollowsConstitution.getMyFollowsConstitution
|
||||
import fr.dcproject.component.follow.routes.constitution.UnfollowConstitution.unfollowConstitution
|
||||
import fr.dcproject.component.views.ConfigViews
|
||||
import fr.dcproject.component.workgroup.routes.CreateWorkgroup.createWorkgroup
|
||||
import fr.dcproject.component.workgroup.routes.DeleteWorkgroup.deleteWorkgroup
|
||||
@@ -43,8 +51,6 @@ import fr.dcproject.event.EventSubscriber
|
||||
import fr.dcproject.routes.commentConstitution
|
||||
import fr.dcproject.routes.constitution
|
||||
import fr.dcproject.routes.definition
|
||||
import fr.dcproject.routes.followArticle
|
||||
import fr.dcproject.routes.followConstitution
|
||||
import fr.dcproject.routes.notificationArticle
|
||||
import fr.dcproject.routes.opinionArticle
|
||||
import fr.dcproject.routes.opinionChoice
|
||||
@@ -186,10 +192,18 @@ fun Application.module(env: Env = PROD) {
|
||||
addMemberToWorkgroup(get(), get())
|
||||
deleteMemberOfWorkgroup(get(), get())
|
||||
updateMemberOfWorkgroup(get(), get())
|
||||
/* TODO */
|
||||
constitution(get(), get())
|
||||
/* Follows */
|
||||
followArticle(get(), get())
|
||||
followConstitution(get(), get())
|
||||
unfollowArticle(get(), get())
|
||||
unfollowConstitution(get(), get())
|
||||
getFollowArticle(get(), get())
|
||||
getFollowConstitution(get(), get())
|
||||
getMyFollowsArticle(get(), get())
|
||||
getMyFollowsConstitution(get(), get())
|
||||
|
||||
/* TODO */
|
||||
constitution(get(), get())
|
||||
commentConstitution(get(), get())
|
||||
voteArticle(get(), get(), get(), get())
|
||||
voteConstitution(get(), get())
|
||||
|
||||
@@ -17,6 +17,7 @@ import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.component.citizen.CitizenVoter
|
||||
import fr.dcproject.component.comment.article.CommentArticleRepository
|
||||
import fr.dcproject.component.comment.generic.CommentVoter
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.component.workgroup.WorkgroupRepository
|
||||
import fr.dcproject.component.workgroup.WorkgroupVoter
|
||||
import fr.dcproject.event.publisher.Publisher
|
||||
@@ -24,7 +25,6 @@ import fr.dcproject.messages.Mailer
|
||||
import fr.dcproject.messages.NotificationEmailSender
|
||||
import fr.dcproject.repository.CommentConstitutionRepository
|
||||
import fr.dcproject.security.voter.ConstitutionVoter
|
||||
import fr.dcproject.security.voter.FollowVoter
|
||||
import fr.dcproject.security.voter.OpinionChoiceVoter
|
||||
import fr.dcproject.security.voter.OpinionVoter
|
||||
import fr.dcproject.security.voter.VoteVoter
|
||||
@@ -41,9 +41,9 @@ import org.elasticsearch.client.RestClient
|
||||
import org.koin.core.qualifier.named
|
||||
import org.koin.dsl.module
|
||||
import fr.dcproject.component.comment.generic.CommentRepository as CommentGenericRepository
|
||||
import fr.dcproject.component.follow.FollowArticleRepository as FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository as FollowConstitutionRepository
|
||||
import fr.dcproject.repository.Constitution as ConstitutionRepository
|
||||
import fr.dcproject.repository.FollowArticle as FollowArticleRepository
|
||||
import fr.dcproject.repository.FollowConstitution as FollowConstitutionRepository
|
||||
import fr.dcproject.repository.OpinionArticle as OpinionArticleRepository
|
||||
import fr.dcproject.repository.OpinionChoice as OpinionChoiceRepository
|
||||
import fr.dcproject.repository.VoteArticle as VoteArticleRepository
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package fr.dcproject.entity
|
||||
package fr.dcproject.component.follow
|
||||
|
||||
import fr.dcproject.component.citizen.CitizenBasic
|
||||
import fr.dcproject.component.citizen.CitizenBasicI
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.entity.ExtraI
|
||||
import fr.dcproject.entity.HasTarget
|
||||
import fr.dcproject.entity.TargetI
|
||||
import fr.postgresjson.entity.EntityCreatedAt
|
||||
import fr.postgresjson.entity.EntityCreatedAtImp
|
||||
import fr.postgresjson.entity.EntityCreatedBy
|
||||
@@ -1,12 +1,10 @@
|
||||
package fr.dcproject.repository
|
||||
package fr.dcproject.component.follow
|
||||
|
||||
import fr.dcproject.component.article.ArticleForView
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.entity.ConstitutionRef
|
||||
import fr.dcproject.entity.FollowForUpdate
|
||||
import fr.dcproject.entity.FollowSimple
|
||||
import fr.dcproject.entity.TargetRef
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
@@ -15,10 +13,10 @@ import fr.postgresjson.repository.RepositoryI
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import java.util.UUID
|
||||
import fr.dcproject.component.follow.Follow as FollowEntity
|
||||
import fr.dcproject.entity.Constitution as ConstitutionEntity
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
|
||||
sealed class Follow<IN : TargetRef, OUT : TargetRef>(override var requester: Requester) : RepositoryI {
|
||||
sealed class FollowRepository<IN : TargetRef, OUT : TargetRef>(override var requester: Requester) : RepositoryI {
|
||||
open fun findByCitizen(
|
||||
citizen: CitizenI,
|
||||
page: Int = 1,
|
||||
@@ -93,7 +91,7 @@ sealed class Follow<IN : TargetRef, OUT : TargetRef>(override var requester: Req
|
||||
): Paginated<FollowSimple<IN, CitizenRef>>
|
||||
}
|
||||
|
||||
class FollowArticle(requester: Requester) : Follow<ArticleRef, ArticleForView>(requester) {
|
||||
class FollowArticleRepository(requester: Requester) : FollowRepository<ArticleRef, ArticleForView>(requester) {
|
||||
override fun findByCitizen(
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
@@ -124,7 +122,7 @@ class FollowArticle(requester: Requester) : Follow<ArticleRef, ArticleForView>(r
|
||||
}
|
||||
}
|
||||
|
||||
class FollowConstitution(requester: Requester) : Follow<ConstitutionRef, ConstitutionEntity>(requester) {
|
||||
class FollowConstitutionRepository(requester: Requester) : FollowRepository<ConstitutionRef, ConstitutionEntity>(requester) {
|
||||
override fun findByCitizen(
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
@@ -1,10 +1,9 @@
|
||||
package fr.dcproject.security.voter
|
||||
package fr.dcproject.component.follow
|
||||
|
||||
import fr.dcproject.component.citizen.CitizenI
|
||||
import fr.dcproject.entity.FollowI
|
||||
import fr.dcproject.voter.Voter
|
||||
import fr.dcproject.voter.VoterResponse
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
import fr.dcproject.component.follow.Follow as FollowEntity
|
||||
|
||||
class FollowVoter : Voter() {
|
||||
fun canCreate(subject: FollowI, citizen: CitizenI?): VoterResponse {
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.dcproject.component.follow.routes.article
|
||||
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowForUpdate
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object FollowArticle {
|
||||
@Location("/articles/{article}/follows")
|
||||
class ArticleFollowRequest(val article: ArticleRef)
|
||||
|
||||
fun Route.followArticle(repo: FollowArticleRepository, voter: FollowVoter) {
|
||||
post<ArticleFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.article, createdBy = this.citizen)
|
||||
voter.assert { canCreate(follow, citizenOrNull) }
|
||||
repo.follow(follow)
|
||||
call.respond(HttpStatusCode.Created)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package fr.dcproject.component.follow.routes.article
|
||||
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object GetFollowArticle {
|
||||
@Location("/articles/{article}/follows")
|
||||
class ArticleFollowRequest(val article: ArticleRef)
|
||||
|
||||
fun Route.getFollowArticle(repo: FollowArticleRepository, voter: FollowVoter) {
|
||||
get<ArticleFollowRequest> {
|
||||
repo.findFollow(citizen, it.article)?.let { follow ->
|
||||
voter.assert { canView(follow, citizenOrNull) }
|
||||
call.respond(follow)
|
||||
} ?: call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package fr.dcproject.component.follow.routes.article
|
||||
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.citizen.Citizen
|
||||
import fr.dcproject.component.follow.FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object GetMyFollowsArticle {
|
||||
@Location("/citizens/{citizen}/follows/articles")
|
||||
class CitizenFollowArticleRequest(val citizen: Citizen)
|
||||
|
||||
fun Route.getMyFollowsArticle(repo: FollowArticleRepository, voter: FollowVoter) {
|
||||
get<CitizenFollowArticleRequest> {
|
||||
val follows = repo.findByCitizen(it.citizen)
|
||||
voter.assert { canView(follows.result, citizenOrNull) }
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.dcproject.component.follow.routes.article
|
||||
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowForUpdate
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.delete
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object UnfollowArticle {
|
||||
@Location("/articles/{article}/follows")
|
||||
class ArticleFollowRequest(val article: ArticleRef)
|
||||
|
||||
fun Route.unfollowArticle(repo: FollowArticleRepository, voter: FollowVoter) {
|
||||
delete<ArticleFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.article, createdBy = this.citizen)
|
||||
voter.assert { canDelete(follow, citizenOrNull) }
|
||||
repo.unfollow(follow)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.dcproject.component.follow.routes.constitution
|
||||
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository
|
||||
import fr.dcproject.component.follow.FollowForUpdate
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.entity.ConstitutionRef
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object FollowConstitution {
|
||||
@Location("/constitutions/{constitution}/follows")
|
||||
class ConstitutionFollowRequest(val constitution: ConstitutionRef)
|
||||
|
||||
fun Route.followConstitution(repo: FollowConstitutionRepository, voter: FollowVoter) {
|
||||
post<ConstitutionFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.constitution, createdBy = this.citizen)
|
||||
voter.assert { canCreate(follow, citizenOrNull) }
|
||||
repo.follow(follow)
|
||||
call.respond(HttpStatusCode.Created)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package fr.dcproject.component.follow.routes.constitution
|
||||
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.entity.ConstitutionRef
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object GetFollowConstitution {
|
||||
@Location("/constitutions/{constitution}/follows")
|
||||
class ConstitutionFollowRequest(val constitution: ConstitutionRef)
|
||||
|
||||
fun Route.getFollowConstitution(repo: FollowConstitutionRepository, voter: FollowVoter) {
|
||||
get<ConstitutionFollowRequest> {
|
||||
repo.findFollow(citizen, it.constitution)?.let { follow ->
|
||||
voter.assert { canView(follow, citizenOrNull) }
|
||||
call.respond(follow)
|
||||
} ?: call.respond(HttpStatusCode.NotFound)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package fr.dcproject.component.follow.routes.constitution
|
||||
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object GetMyFollowsConstitution {
|
||||
@Location("/citizens/{citizen}/follows/constitutions")
|
||||
class CitizenFollowConstitutionRequest(val citizen: CitizenRef)
|
||||
|
||||
fun Route.getMyFollowsConstitution(repo: FollowConstitutionRepository, voter: FollowVoter) {
|
||||
get<CitizenFollowConstitutionRequest> {
|
||||
val follows = repo.findByCitizen(it.citizen)
|
||||
voter.assert { canView(follows.result, citizenOrNull) }
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.dcproject.component.follow.routes.constitution
|
||||
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.follow.FollowConstitutionRepository
|
||||
import fr.dcproject.component.follow.FollowForUpdate
|
||||
import fr.dcproject.component.follow.FollowVoter
|
||||
import fr.dcproject.entity.ConstitutionRef
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.delete
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object UnfollowConstitution {
|
||||
@Location("/constitutions/{constitution}/follows")
|
||||
class ConstitutionUnfollowRequest(val constitution: ConstitutionRef)
|
||||
|
||||
fun Route.unfollowConstitution(repo: FollowConstitutionRepository, voter: FollowVoter) {
|
||||
delete<ConstitutionUnfollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.constitution, createdBy = this.citizen)
|
||||
voter.assert { canDelete(follow, citizenOrNull) }
|
||||
repo.unfollow(follow)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,11 @@ import com.rabbitmq.client.Envelope
|
||||
import fr.dcproject.application.Configuration
|
||||
import fr.dcproject.component.article.ArticleForView
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.entity.FollowSimple
|
||||
import fr.dcproject.component.follow.FollowRepository
|
||||
import fr.dcproject.component.follow.FollowSimple
|
||||
import fr.dcproject.entity.TargetRef
|
||||
import fr.dcproject.event.publisher.Publisher
|
||||
import fr.dcproject.messages.NotificationEmailSender
|
||||
import fr.dcproject.repository.Follow
|
||||
import fr.postgresjson.serializer.deserialize
|
||||
import io.ktor.application.ApplicationCall
|
||||
import io.ktor.application.EventDefinition
|
||||
@@ -27,7 +27,7 @@ import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import fr.dcproject.repository.FollowArticle as FollowArticleRepository
|
||||
import fr.dcproject.component.follow.FollowArticleRepository as FollowArticleRepository
|
||||
|
||||
class ArticleUpdate(
|
||||
target: ArticleForView
|
||||
@@ -120,7 +120,7 @@ class EventNotification(
|
||||
val repo = when (event.type) {
|
||||
"article" -> followRepo
|
||||
else -> error("event '${event.type}' not implemented")
|
||||
} as Follow<*, *>
|
||||
} as FollowRepository<*, *>
|
||||
|
||||
repo
|
||||
.findFollowsByTarget(event.target)
|
||||
|
||||
@@ -8,7 +8,7 @@ import fr.dcproject.component.article.ArticleWithTitleI
|
||||
import fr.dcproject.component.citizen.CitizenBasicI
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.component.citizen.CitizenRepository
|
||||
import fr.dcproject.entity.FollowSimple
|
||||
import fr.dcproject.component.follow.FollowSimple
|
||||
import fr.dcproject.entity.TargetRef
|
||||
import fr.postgresjson.entity.UuidEntityI
|
||||
import java.util.UUID
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package fr.dcproject.routes
|
||||
|
||||
import fr.dcproject.component.article.ArticleRef
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.citizen.Citizen
|
||||
import fr.dcproject.entity.FollowForUpdate
|
||||
import fr.dcproject.security.voter.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.delete
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import fr.dcproject.repository.FollowArticle as FollowArticleRepository
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object FollowArticlePaths {
|
||||
@Location("/articles/{article}/follows")
|
||||
class ArticleFollowRequest(val article: ArticleRef)
|
||||
|
||||
@Location("/citizens/{citizen}/follows/articles")
|
||||
class CitizenFollowArticleRequest(val citizen: Citizen)
|
||||
}
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.followArticle(repo: FollowArticleRepository, voter: FollowVoter) {
|
||||
post<FollowArticlePaths.ArticleFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.article, createdBy = this.citizen)
|
||||
voter.assert { canCreate(follow, citizenOrNull) }
|
||||
repo.follow(follow)
|
||||
call.respond(HttpStatusCode.Created)
|
||||
}
|
||||
|
||||
delete<FollowArticlePaths.ArticleFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.article, createdBy = this.citizen)
|
||||
voter.assert { canDelete(follow, citizenOrNull) }
|
||||
repo.unfollow(follow)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get<FollowArticlePaths.ArticleFollowRequest> {
|
||||
repo.findFollow(citizen, it.article)?.let { follow ->
|
||||
voter.assert { canView(follow, citizenOrNull) }
|
||||
call.respond(follow)
|
||||
} ?: call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get<FollowArticlePaths.CitizenFollowArticleRequest> {
|
||||
val follows = repo.findByCitizen(it.citizen)
|
||||
if (follows.result.isNotEmpty()) {
|
||||
voter.assert { canView(follows.result, citizenOrNull) }
|
||||
}
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
package fr.dcproject.routes
|
||||
|
||||
import fr.dcproject.component.auth.citizen
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import fr.dcproject.component.citizen.CitizenRef
|
||||
import fr.dcproject.entity.ConstitutionRef
|
||||
import fr.dcproject.entity.FollowForUpdate
|
||||
import fr.dcproject.security.voter.FollowVoter
|
||||
import fr.dcproject.voter.assert
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
import io.ktor.locations.delete
|
||||
import io.ktor.locations.get
|
||||
import io.ktor.locations.post
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import fr.dcproject.repository.FollowConstitution as FollowConstitutionRepository
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object FollowConstitutionPaths {
|
||||
@Location("/constitutions/{constitution}/follows")
|
||||
class ConstitutionFollowRequest(val constitution: ConstitutionRef)
|
||||
|
||||
@Location("/citizens/{citizen}/follows/constitutions")
|
||||
class CitizenFollowConstitutionRequest(val citizen: CitizenRef)
|
||||
}
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.followConstitution(repo: FollowConstitutionRepository, voter: FollowVoter) {
|
||||
post<FollowConstitutionPaths.ConstitutionFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.constitution, createdBy = this.citizen)
|
||||
voter.assert { canCreate(follow, citizenOrNull) }
|
||||
repo.follow(follow)
|
||||
call.respond(HttpStatusCode.Created)
|
||||
}
|
||||
|
||||
delete<FollowConstitutionPaths.ConstitutionFollowRequest> {
|
||||
val follow = FollowForUpdate(target = it.constitution, createdBy = this.citizen)
|
||||
voter.assert { canDelete(follow, citizenOrNull) }
|
||||
repo.unfollow(follow)
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get<FollowConstitutionPaths.ConstitutionFollowRequest> {
|
||||
repo.findFollow(citizen, it.constitution)?.let { follow ->
|
||||
voter.assert { canView(follow, citizenOrNull) }
|
||||
call.respond(follow)
|
||||
} ?: call.respond(HttpStatusCode.NotFound)
|
||||
}
|
||||
|
||||
get<FollowConstitutionPaths.CitizenFollowConstitutionRequest> {
|
||||
val follows = repo.findByCitizen(it.citizen)
|
||||
voter.assert { canView(follows.result, citizenOrNull) }
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user