Merge pull request #89 from flecomte/ArticleViewManager
ArticleViewRepository
This commit was merged in pull request #89.
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
package fr.dcproject.component.article
|
||||
package fr.dcproject.component.article.database
|
||||
|
||||
import fr.dcproject.common.entity.VersionableId
|
||||
import fr.dcproject.common.utils.contentToString
|
||||
import fr.dcproject.common.utils.getJsonField
|
||||
import fr.dcproject.common.utils.toIso
|
||||
import fr.dcproject.component.article.database.ArticleI
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.views.ViewManager
|
||||
import fr.dcproject.component.views.ViewRepository
|
||||
import fr.dcproject.component.views.entity.ViewAggregation
|
||||
import org.elasticsearch.client.Request
|
||||
import org.elasticsearch.client.Response
|
||||
import org.elasticsearch.client.RestClient
|
||||
import org.joda.time.DateTime
|
||||
import java.util.UUID
|
||||
@@ -17,11 +15,11 @@ import java.util.UUID
|
||||
/**
|
||||
* Wrapper for manage views with elasticsearch
|
||||
*/
|
||||
class ArticleViewManager <A> (private val restClient: RestClient) : ViewManager<A> where A : VersionableId, A : ArticleI {
|
||||
class ArticleViewRepository <A> (private val restClient: RestClient) : ViewRepository<A> where A : VersionableId, A : ArticleI {
|
||||
/**
|
||||
* Add view on article to elasticsearch
|
||||
*/
|
||||
override fun addView(ip: String, entity: A, citizen: CitizenI?, dateTime: DateTime): Response? {
|
||||
override fun addView(ip: String, entity: A, citizen: CitizenI?, dateTime: DateTime) {
|
||||
val isLogged = (citizen != null).toString()
|
||||
val ref = citizen?.id ?: UUID.nameUUIDFromBytes(ip.toByteArray())!!
|
||||
val request = Request(
|
||||
@@ -45,7 +43,7 @@ class ArticleViewManager <A> (private val restClient: RestClient) : ViewManager<
|
||||
)
|
||||
}
|
||||
|
||||
return restClient.performRequest(request)
|
||||
restClient.performRequest(request)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2,10 +2,10 @@ package fr.dcproject.component.article.routes
|
||||
|
||||
import fr.dcproject.common.security.assert
|
||||
import fr.dcproject.component.article.ArticleAccessControl
|
||||
import fr.dcproject.component.article.ArticleViewManager
|
||||
import fr.dcproject.component.article.database.ArticleForView
|
||||
import fr.dcproject.component.article.database.ArticleRef
|
||||
import fr.dcproject.component.article.database.ArticleRepository
|
||||
import fr.dcproject.component.article.database.ArticleViewRepository
|
||||
import fr.dcproject.component.auth.citizenOrNull
|
||||
import io.ktor.application.call
|
||||
import io.ktor.features.NotFoundException
|
||||
@@ -24,7 +24,7 @@ object GetOneArticle {
|
||||
val article = ArticleRef(article)
|
||||
}
|
||||
|
||||
fun Route.getOneArticle(viewManager: ArticleViewManager<ArticleForView>, ac: ArticleAccessControl, repo: ArticleRepository) {
|
||||
fun Route.getOneArticle(viewRepository: ArticleViewRepository<ArticleForView>, ac: ArticleAccessControl, repo: ArticleRepository) {
|
||||
get<ArticleRequest> {
|
||||
val article: ArticleForView = repo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found")
|
||||
ac.assert { canView(article, citizenOrNull) }
|
||||
@@ -64,7 +64,7 @@ object GetOneArticle {
|
||||
val total: Int = a.votes.total
|
||||
val score: Int = a.votes.score
|
||||
}
|
||||
val views: Any = viewManager.getViewsCount(article).let { v ->
|
||||
val views: Any = viewRepository.getViewsCount(article).let { v ->
|
||||
object {
|
||||
val total = v.total
|
||||
val unique = v.unique
|
||||
@@ -76,7 +76,7 @@ object GetOneArticle {
|
||||
)
|
||||
|
||||
launch {
|
||||
viewManager.addView(call.request.local.remoteHost, article, citizenOrNull)
|
||||
viewRepository.addView(call.request.local.remoteHost, article, citizenOrNull)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package fr.dcproject.component.views
|
||||
|
||||
import fr.dcproject.application.Configuration
|
||||
import fr.dcproject.component.article.ArticleViewManager
|
||||
import fr.dcproject.component.article.database.ArticleForView
|
||||
import fr.dcproject.component.article.database.ArticleViewRepository
|
||||
import org.apache.http.HttpHost
|
||||
import org.elasticsearch.client.RestClient
|
||||
import org.koin.dsl.module
|
||||
@@ -17,6 +17,6 @@ val viewKoinModule = module {
|
||||
).build().apply {
|
||||
createEsIndexForViews()
|
||||
}
|
||||
ArticleViewManager<ArticleForView>(esClient)
|
||||
ArticleViewRepository<ArticleForView>(esClient)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,13 @@ package fr.dcproject.component.views
|
||||
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
import fr.dcproject.component.views.entity.ViewAggregation
|
||||
import org.elasticsearch.client.Response
|
||||
import org.joda.time.DateTime
|
||||
|
||||
interface ViewManager <T> {
|
||||
interface ViewRepository <T> {
|
||||
/**
|
||||
* Add view to one entity
|
||||
*/
|
||||
fun addView(ip: String, entity: T, citizen: CitizenI? = null, dateTime: DateTime = DateTime.now()): Response?
|
||||
fun addView(ip: String, entity: T, citizen: CitizenI? = null, dateTime: DateTime = DateTime.now())
|
||||
|
||||
/**
|
||||
* Get Views aggregations
|
||||
@@ -2,8 +2,8 @@ package functional
|
||||
|
||||
import fr.dcproject.application.Env.TEST
|
||||
import fr.dcproject.application.module
|
||||
import fr.dcproject.component.article.ArticleViewManager
|
||||
import fr.dcproject.component.article.database.ArticleForView
|
||||
import fr.dcproject.component.article.database.ArticleViewRepository
|
||||
import fr.dcproject.component.auth.database.UserCreator
|
||||
import fr.dcproject.component.citizen.database.CitizenCreator
|
||||
import fr.dcproject.component.citizen.database.CitizenI
|
||||
@@ -44,33 +44,33 @@ class ViewTest {
|
||||
val citizenRef = CitizenRef()
|
||||
|
||||
withTestApplication({ module(TEST) }) {
|
||||
val viewManager: ArticleViewManager<ArticleForView> = application.get()
|
||||
val viewRepository: ArticleViewRepository<ArticleForView> = application.get()
|
||||
|
||||
/* Get view before */
|
||||
val startView = viewManager.getViewsCount(article)
|
||||
val startView = viewRepository.getViewsCount(article)
|
||||
|
||||
/* Add View */
|
||||
viewManager.addView(
|
||||
viewRepository.addView(
|
||||
"1.2.3.4",
|
||||
article,
|
||||
citizenRef
|
||||
)
|
||||
|
||||
/* Add View */
|
||||
viewManager.addView(
|
||||
viewRepository.addView(
|
||||
"10.10.10.10",
|
||||
article,
|
||||
citizenRef
|
||||
)
|
||||
|
||||
/* Add View */
|
||||
viewManager.addView(
|
||||
viewRepository.addView(
|
||||
"8.8.8.8",
|
||||
article
|
||||
)
|
||||
|
||||
/* Add View */
|
||||
viewManager.addView(
|
||||
viewRepository.addView(
|
||||
"1.1.1.1",
|
||||
article
|
||||
)
|
||||
@@ -79,7 +79,7 @@ class ViewTest {
|
||||
Thread.sleep(1000)
|
||||
|
||||
/* Get view */
|
||||
val afterView = viewManager.getViewsCount(article)
|
||||
val afterView = viewRepository.getViewsCount(article)
|
||||
|
||||
/* Check if view has increment */
|
||||
afterView.total `should be equal to` startView.total + 4
|
||||
|
||||
Reference in New Issue
Block a user