feature #14: Add routes for get follows of one citizen
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
package fr.dcproject
|
||||
|
||||
import fr.dcproject.repository.FollowArticleRepository
|
||||
import fr.dcproject.repository.FollowConstitutionRepository
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
@@ -10,6 +8,8 @@ import org.koin.dsl.module
|
||||
import fr.dcproject.repository.Article as ArticleRepository
|
||||
import fr.dcproject.repository.Citizen as CitizenRepository
|
||||
import fr.dcproject.repository.Constitution as ConstitutionRepository
|
||||
import fr.dcproject.repository.FollowArticle as FollowArticleRepository
|
||||
import fr.dcproject.repository.FollowConstitution as FollowConstitutionRepository
|
||||
|
||||
val config = Config()
|
||||
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
package fr.dcproject.entity
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import java.util.*
|
||||
|
||||
class Follow <T: EntityI<UUID>> (
|
||||
class Follow <T: UuidEntity> (
|
||||
id: UUID = UUID.randomUUID(),
|
||||
citizen: Citizen,
|
||||
override var target: T
|
||||
): Extra<T>(id, citizen)
|
||||
|
||||
typealias FollowArticleEntity = Follow<Article>
|
||||
typealias FollowConstitutionEntity = Follow<Constitution>
|
||||
@@ -1,16 +1,37 @@
|
||||
package fr.dcproject.repository
|
||||
|
||||
import fr.postgresjson.connexion.Paginated
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import fr.dcproject.entity.Article as ArticleEntity
|
||||
import fr.dcproject.entity.Citizen as CitizenEntity
|
||||
import fr.dcproject.entity.Constitution as ConstitutionEntity
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
|
||||
open class Follow <T: EntityI<UUID>>(override var requester: Requester): RepositoryI<FollowEntity<T>> {
|
||||
open class Follow <T: UuidEntity>(override var requester: Requester): RepositoryI<FollowEntity<T>> {
|
||||
override val entityName = FollowEntity::class as KClass<FollowEntity<T>>
|
||||
open fun findByCitizenId(
|
||||
citizen: CitizenEntity,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<FollowEntity<T>> =
|
||||
findByCitizenId(citizen.id ?: error("The citizen must have an id"), page, limit)
|
||||
|
||||
open fun findByCitizenId(
|
||||
citizenId: UUID,
|
||||
page: Int = 1,
|
||||
limit: Int = 50
|
||||
): Paginated<FollowEntity<T>> {
|
||||
return requester.run {
|
||||
getFunction("find_follows_by_citizen")
|
||||
.select(page, limit,
|
||||
"citizen_id" to citizenId
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun follow(follow: FollowEntity<T>) {
|
||||
val reference = follow.target::class.simpleName!!.toLowerCase()
|
||||
@@ -34,5 +55,33 @@ open class Follow <T: EntityI<UUID>>(override var requester: Requester): Reposit
|
||||
)
|
||||
}
|
||||
}
|
||||
class FollowArticleRepository(override var requester: Requester): Follow<ArticleEntity>(requester)
|
||||
class FollowConstitutionRepository(override var requester: Requester): Follow<ConstitutionEntity>(requester)
|
||||
|
||||
class FollowArticle (requester: Requester): Follow<ArticleEntity>(requester) {
|
||||
override fun findByCitizenId(
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowEntity<ArticleEntity>> {
|
||||
return requester.run {
|
||||
getFunction("find_follows_article_by_citizen")
|
||||
.select(page, limit,
|
||||
"citizen_id" to citizenId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class FollowConstitution (requester: Requester): Follow<ConstitutionEntity>(requester) {
|
||||
override fun findByCitizenId(
|
||||
citizenId: UUID,
|
||||
page: Int,
|
||||
limit: Int
|
||||
): Paginated<FollowEntity<ConstitutionEntity>> {
|
||||
return requester.run {
|
||||
getFunction("find_follows_constitution_by_citizen")
|
||||
.select(page, limit,
|
||||
"citizen_id" to citizenId
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,17 +3,18 @@ package fr.dcproject.routes
|
||||
import Paths
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.entity.User
|
||||
import fr.dcproject.repository.FollowArticleRepository
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
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 org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
import fr.dcproject.repository.FollowArticle as FollowArticleRepository
|
||||
|
||||
// TODO get current citizen
|
||||
val currentCitizen = Citizen(
|
||||
@@ -34,4 +35,9 @@ fun Route.followArticle(repo: FollowArticleRepository) {
|
||||
repo.unfollow(FollowEntity(target = it.article, citizen = currentCitizen))
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get<Paths.CitizenFollowArticleRequest> {
|
||||
val follows = repo.findByCitizenId(it.citizen)
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,18 @@ package fr.dcproject.routes
|
||||
import Paths
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.entity.User
|
||||
import fr.dcproject.repository.FollowConstitutionRepository
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
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 org.joda.time.DateTime
|
||||
import java.util.*
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
import fr.dcproject.repository.FollowConstitution as FollowConstitutionRepository
|
||||
|
||||
// TODO get current citizen
|
||||
val currentCitizen2 = Citizen(
|
||||
@@ -34,4 +35,9 @@ fun Route.followConstitution(repo: FollowConstitutionRepository) {
|
||||
repo.unfollow(FollowEntity(target = it.constitution, citizen = currentCitizen2))
|
||||
call.respond(HttpStatusCode.NoContent)
|
||||
}
|
||||
|
||||
get<Paths.CitizenFollowConstitutionRequest> {
|
||||
val follows = repo.findByCitizenId(it.citizen)
|
||||
call.respond(follows)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import fr.dcproject.entity.Article
|
||||
import fr.dcproject.entity.Citizen
|
||||
import fr.dcproject.entity.Constitution
|
||||
import fr.dcproject.entity.Follow
|
||||
import fr.postgresjson.repository.RepositoryI.Direction
|
||||
import io.ktor.locations.KtorExperimentalLocationsAPI
|
||||
import io.ktor.locations.Location
|
||||
@@ -31,4 +30,6 @@ object Paths {
|
||||
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
||||
}
|
||||
@Location("/citizens/{citizen}") class CitizenRequest(val citizen: Citizen)
|
||||
@Location("/citizens/{citizen}/follows/articles") class CitizenFollowArticleRequest(val citizen: Citizen)
|
||||
@Location("/citizens/{citizen}/follows/constitutions") class CitizenFollowConstitutionRequest(val citizen: Citizen)
|
||||
}
|
||||
Reference in New Issue
Block a user