feature #14: create route for follow article
This commit is contained in:
@@ -10,6 +10,7 @@ import fr.dcproject.entity.Article
|
||||
import fr.dcproject.entity.Constitution
|
||||
import fr.dcproject.routes.article
|
||||
import fr.dcproject.routes.constitution
|
||||
import fr.dcproject.routes.followArticle
|
||||
import fr.postgresjson.migration.Migrations
|
||||
import io.ktor.application.Application
|
||||
import io.ktor.application.install
|
||||
@@ -31,7 +32,7 @@ import java.util.*
|
||||
import fr.dcproject.repository.Article as RepositoryArticle
|
||||
import fr.dcproject.repository.Constitution as RepositoryConstitution
|
||||
|
||||
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
|
||||
fun main(args: Array<String>): Unit = io.ktor.server.jetty.EngineMain.main(args)
|
||||
|
||||
@KtorExperimentalAPI
|
||||
@KtorExperimentalLocationsAPI
|
||||
@@ -106,6 +107,7 @@ fun Application.module() {
|
||||
install(Routing) {
|
||||
article(get())
|
||||
constitution(get())
|
||||
followArticle(get())
|
||||
}
|
||||
|
||||
// TODO move to postgresJson lib
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package fr.dcproject
|
||||
|
||||
import fr.dcproject.repository.FollowArticleRepository
|
||||
import fr.postgresjson.connexion.Connection
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.migration.Migrations
|
||||
@@ -25,6 +26,7 @@ val Module = module {
|
||||
// TODO: create generic declaration
|
||||
single { ArticleRepository(get()) }
|
||||
single { ConstitutionRepository(get()) }
|
||||
single { FollowArticleRepository(get()) }
|
||||
|
||||
single { Migrations(connection = get(), directory = config.sqlFiles) }
|
||||
}
|
||||
|
||||
22
src/main/kotlin/fr/dcproject/entity/Extra.kt
Normal file
22
src/main/kotlin/fr/dcproject/entity/Extra.kt
Normal file
@@ -0,0 +1,22 @@
|
||||
package fr.dcproject.entity
|
||||
|
||||
import fr.postgresjson.entity.EntityCreatedAt
|
||||
import fr.postgresjson.entity.EntityCreatedAtImp
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.entity.UuidEntity
|
||||
import java.util.*
|
||||
|
||||
interface ExtraI <T: EntityI<UUID>>:
|
||||
EntityI<UUID>,
|
||||
EntityCreatedAt {
|
||||
var citizen: Citizen
|
||||
var target: T
|
||||
}
|
||||
|
||||
abstract class Extra<T: EntityI<UUID>>(
|
||||
id: UUID? = UUID.randomUUID(),
|
||||
override var citizen: Citizen
|
||||
):
|
||||
ExtraI<T>,
|
||||
UuidEntity(id),
|
||||
EntityCreatedAt by EntityCreatedAtImp()
|
||||
11
src/main/kotlin/fr/dcproject/entity/Follow.kt
Normal file
11
src/main/kotlin/fr/dcproject/entity/Follow.kt
Normal file
@@ -0,0 +1,11 @@
|
||||
package fr.dcproject.entity
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import java.util.*
|
||||
|
||||
class Follow <T: EntityI<UUID>> (
|
||||
id: UUID = UUID.randomUUID(),
|
||||
citizen: Citizen,
|
||||
override var target: T
|
||||
): Extra<T>(id, citizen)
|
||||
|
||||
typealias FollowArticleEntity = Follow<Article>
|
||||
25
src/main/kotlin/fr/dcproject/repository/Follow.kt
Normal file
25
src/main/kotlin/fr/dcproject/repository/Follow.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package fr.dcproject.repository
|
||||
|
||||
import fr.postgresjson.connexion.Requester
|
||||
import fr.postgresjson.entity.EntityI
|
||||
import fr.postgresjson.repository.RepositoryI
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import fr.dcproject.entity.Article as ArticleEntity
|
||||
import fr.dcproject.entity.Follow as FollowEntity
|
||||
|
||||
open class Follow <T: EntityI<UUID>>(override var requester: Requester): RepositoryI<FollowEntity<T>> {
|
||||
override val entityName = FollowEntity::class as KClass<FollowEntity<T>>
|
||||
|
||||
fun follow(follow: FollowEntity<T>) {
|
||||
val reference = follow.target::class.simpleName!!.toLowerCase()
|
||||
requester
|
||||
.getFunction("follow")
|
||||
.sendQuery(
|
||||
"reference" to reference,
|
||||
"target_id" to follow.target.id,
|
||||
"citizen_id" to follow.citizen.id
|
||||
)
|
||||
}
|
||||
}
|
||||
class FollowArticleRepository(override var requester: Requester): Follow<ArticleEntity>(requester)
|
||||
31
src/main/kotlin/fr/dcproject/routes/Follow.kt
Normal file
31
src/main/kotlin/fr/dcproject/routes/Follow.kt
Normal file
@@ -0,0 +1,31 @@
|
||||
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.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
|
||||
|
||||
// TODO get current citizen
|
||||
val currentCitizen = Citizen(
|
||||
id = UUID.fromString("64b7b379-2298-43ec-b428-ba134930cabd"),
|
||||
name = Citizen.Name("todo", "todo"),
|
||||
birthday = DateTime.now(),
|
||||
user = User(username = "plop", plainPassword = "plip")
|
||||
)
|
||||
|
||||
@KtorExperimentalLocationsAPI
|
||||
fun Route.followArticle(repo: FollowArticleRepository) {
|
||||
post<Paths.ArticleFollowRequest> {
|
||||
repo.follow(FollowEntity(target = it.article, citizen = currentCitizen))
|
||||
call.respond(HttpStatusCode.Created)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import fr.dcproject.entity.Article
|
||||
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
|
||||
@@ -11,6 +12,7 @@ object Paths {
|
||||
val limit: Int = if (limit > 50) 50 else if (limit < 1) 1 else limit
|
||||
}
|
||||
@Location("/articles/{article}") class ArticleRequest(val article: Article)
|
||||
@Location("/articles/{article}/follow") class ArticleFollowRequest(val article: Article)
|
||||
@Location("/articles") class PostArticleRequest
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user