create SQL function for get citizen votes with multiple target ids
add updated_at field on vote table
This commit is contained in:
@@ -31,21 +31,6 @@ open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<
|
||||
)!!
|
||||
}
|
||||
|
||||
fun findByCitizen(
|
||||
citizen: CitizenEntity,
|
||||
target: String,
|
||||
typeReference: TypeReference<List<VoteEntity<T>>>,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<T>> =
|
||||
findByCitizen(
|
||||
citizen.id ?: error("The citizen must have an id"),
|
||||
target,
|
||||
typeReference,
|
||||
page,
|
||||
limit
|
||||
)
|
||||
|
||||
fun findByCitizen(
|
||||
citizenId: UUID,
|
||||
target: String,
|
||||
@@ -61,6 +46,21 @@ open class Vote <T: UuidEntity>(override var requester: Requester): RepositoryI<
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fun findCitizenVotesByTargets(
|
||||
citizen: CitizenEntity,
|
||||
targets: List<UUID>
|
||||
): List<VoteEntity<*>> {
|
||||
val typeReference = object: TypeReference<List<VoteEntity<UuidEntity>>>() {}
|
||||
return requester.run {
|
||||
val citizenId = citizen.id ?: error("The citizen must have an id")
|
||||
getFunction("find_citizen_votes_by_target_ids")
|
||||
.select(typeReference, mapOf(
|
||||
"citizen_id" to citizenId,
|
||||
"ids" to targets
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class VoteArticle (requester: Requester): Vote<Article>(requester) {
|
||||
@@ -70,7 +70,7 @@ class VoteArticle (requester: Requester): Vote<Article>(requester) {
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<Article>> =
|
||||
findByCitizen(
|
||||
citizen,
|
||||
citizen.id ?: error("The citizen must have an id"),
|
||||
"article",
|
||||
object: TypeReference<List<VoteEntity<Article>>>() {},
|
||||
page,
|
||||
@@ -85,7 +85,7 @@ class VoteConstitution (requester: Requester): Vote<Constitution>(requester) {
|
||||
limit: Int = 50
|
||||
): Paginated<VoteEntity<Constitution>> =
|
||||
findByCitizen(
|
||||
citizen,
|
||||
citizen.id ?: error("The citizen must have an id"),
|
||||
"constitution",
|
||||
object: TypeReference<List<VoteEntity<Constitution>>>() {},
|
||||
page,
|
||||
|
||||
@@ -15,6 +15,7 @@ import io.ktor.locations.put
|
||||
import io.ktor.request.receive
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.Route
|
||||
import java.util.*
|
||||
import fr.dcproject.entity.Article as ArticleEntity
|
||||
import fr.dcproject.entity.Vote as VoteEntity
|
||||
import fr.dcproject.repository.VoteArticle as VoteArticleRepository
|
||||
@@ -33,6 +34,14 @@ object VoteArticlePaths {
|
||||
limit: Int = 50,
|
||||
val search: String? = null
|
||||
): PaginatedRequestI by PaginatedRequest(page, limit)
|
||||
|
||||
@Location("/citizens/{citizen}/votes")
|
||||
class CitizenVotesByIdsRequest(val citizen: Citizen, id: List<String>) {
|
||||
val id: List<UUID> = id
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotBlank() }
|
||||
.map { UUID.fromString(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -55,4 +64,11 @@ fun Route.voteArticle(repo: VoteArticleRepository) {
|
||||
|
||||
call.respond(votes)
|
||||
}
|
||||
|
||||
get<VoteArticlePaths.CitizenVotesByIdsRequest> {
|
||||
val votes = repo.findCitizenVotesByTargets(it.citizen, it.id)
|
||||
assertCan(VIEW, votes)
|
||||
|
||||
call.respond(votes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user