Can get votes for one citizen
This commit is contained in:
@@ -3,10 +3,13 @@ package fr.dcproject.repository
|
||||
import fr.dcproject.entity.Article
|
||||
import fr.dcproject.entity.Constitution
|
||||
import fr.dcproject.entity.VoteAggregation
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import fr.dcproject.entity.Citizen as CitizenEntity
|
||||
import fr.dcproject.entity.Vote as VoteEntity
|
||||
|
||||
open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<VoteEntity<T>> {
|
||||
@@ -26,6 +29,26 @@ open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<
|
||||
"anonymous" to anonymous
|
||||
)!!
|
||||
}
|
||||
|
||||
open fun findByCitizen(
|
||||
citizen: CitizenEntity,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<T>> =
|
||||
findByCitizen(citizen.id ?: error("The citizen must have an id"), page, limit)
|
||||
|
||||
open fun findByCitizen(
|
||||
citizenId: UUID,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<T>> {
|
||||
return requester.run {
|
||||
getFunction("find_votes_by_citizen")
|
||||
.select(page, limit,
|
||||
"created_by_id" to citizenId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VoteArticle (requester: Requester): Vote<Article>(requester)
|
||||
|
||||
14
src/main/kotlin/fr/dcproject/routes/PaginatedRequest.kt
Normal file
14
src/main/kotlin/fr/dcproject/routes/PaginatedRequest.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package fr.dcproject.routes
|
||||
|
||||
interface PaginatedRequestI {
|
||||
val page: Int
|
||||
val limit: Int
|
||||
}
|
||||
|
||||
open class PaginatedRequest(
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): PaginatedRequestI {
|
||||
override val page: Int = if (page < 1) 1 else page
|
||||
override val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
||||
}
|
||||
@@ -4,11 +4,13 @@ import fr.dcproject.citizen
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.routes.VoteArticlePaths.ArticleVoteRequest.Content
|
||||
import fr.dcproject.security.voter.VoteVoter.Action.CREATE
|
||||
import fr.dcproject.security.voter.VoteVoter.Action.VIEW
|
||||
import fr.dcproject.security.voter.assertCan
|
||||
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.locations.put
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
@@ -19,10 +21,18 @@ import fr.dcproject.repository.VoteArticle as VoteArticleRepository
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
object VoteArticlePaths {
|
||||
@Location("/articles/{article}/vote") class ArticleVoteRequest(val article: ArticleEntity) {
|
||||
@Location("/articles/{article}/vote")
|
||||
class ArticleVoteRequest(val article: ArticleEntity) {
|
||||
data class Content(var note: Int)
|
||||
}
|
||||
@Location("/citizens/{citizen}/votes/articles") class CitizenVoteArticleRequest(val citizen: Citizen)
|
||||
|
||||
@Location("/citizens/{citizen}/votes/articles")
|
||||
class CitizenVoteArticleRequest(
|
||||
val citizen: Citizen,
|
||||
page: Int = 1,
|
||||
limit: Int = 50,
|
||||
val search: String? = null
|
||||
): PaginatedRequestI by PaginatedRequest(page, limit)
|
||||
}
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -38,4 +48,11 @@ fun Route.voteArticle(repo: VoteArticleRepository) {
|
||||
val votes = repo.vote(vote)
|
||||
call.respond(HttpStatusCode.Created, votes)
|
||||
}
|
||||
|
||||
get<VoteArticlePaths.CitizenVoteArticleRequest> {
|
||||
val votes = repo.findByCitizen(it.citizen)
|
||||
assertCan(VIEW, votes.result)
|
||||
|
||||
call.respond(votes)
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,10 @@ class VoteVoter: Voter {
|
||||
}
|
||||
|
||||
override fun supports(action: ActionI, call: ApplicationCall, subject: Any?): Boolean {
|
||||
return action is Action && subject is VoteEntity<*>?
|
||||
return action is Action && (
|
||||
subject is VoteEntity<*>?
|
||||
|| subject is List<*>
|
||||
)
|
||||
}
|
||||
|
||||
override fun vote(action: ActionI, call: ApplicationCall, subject: Any?): Vote {
|
||||
@@ -19,8 +22,25 @@ class VoteVoter: Voter {
|
||||
return Vote.GRANTED
|
||||
}
|
||||
|
||||
if (action == Action.VIEW) {
|
||||
return Vote.GRANTED
|
||||
if (action == Action.VIEW && user != null) {
|
||||
if (subject is VoteEntity<*>) {
|
||||
return if (subject.createdBy?.userId != user.id) {
|
||||
Vote.DENIED
|
||||
} else {
|
||||
Vote.GRANTED
|
||||
}
|
||||
}
|
||||
|
||||
if (subject is List<*>) {
|
||||
subject.forEach {
|
||||
if (it !is VoteEntity<*> || it.createdBy?.userId != user.id) {
|
||||
return Vote.DENIED
|
||||
}
|
||||
}
|
||||
return Vote.GRANTED
|
||||
}
|
||||
|
||||
return Vote.DENIED
|
||||
}
|
||||
|
||||
return Vote.ABSTAIN
|
||||
|
||||
Reference in New Issue
Block a user