ArticleViewRepository #89

Merged
flecomte merged 2 commits from ArticleViewManager into master 2021-04-02 12:39:34 +02:00
5 changed files with 20 additions and 22 deletions
Showing only changes of commit db810ab0c6 - Show all commits

View File

@@ -6,10 +6,9 @@ import fr.dcproject.common.utils.getJsonField
import fr.dcproject.common.utils.toIso import fr.dcproject.common.utils.toIso
import fr.dcproject.component.article.database.ArticleI import fr.dcproject.component.article.database.ArticleI
import fr.dcproject.component.citizen.database.CitizenI 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 fr.dcproject.component.views.entity.ViewAggregation
import org.elasticsearch.client.Request import org.elasticsearch.client.Request
import org.elasticsearch.client.Response
import org.elasticsearch.client.RestClient import org.elasticsearch.client.RestClient
import org.joda.time.DateTime import org.joda.time.DateTime
import java.util.UUID import java.util.UUID
@@ -17,11 +16,11 @@ import java.util.UUID
/** /**
* Wrapper for manage views with elasticsearch * 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 * 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 isLogged = (citizen != null).toString()
val ref = citizen?.id ?: UUID.nameUUIDFromBytes(ip.toByteArray())!! val ref = citizen?.id ?: UUID.nameUUIDFromBytes(ip.toByteArray())!!
val request = Request( val request = Request(
@@ -45,7 +44,7 @@ class ArticleViewManager <A> (private val restClient: RestClient) : ViewManager<
) )
} }
return restClient.performRequest(request) restClient.performRequest(request)
} }
/** /**

View File

@@ -2,7 +2,7 @@ package fr.dcproject.component.article.routes
import fr.dcproject.common.security.assert import fr.dcproject.common.security.assert
import fr.dcproject.component.article.ArticleAccessControl import fr.dcproject.component.article.ArticleAccessControl
import fr.dcproject.component.article.ArticleViewManager import fr.dcproject.component.article.ArticleViewRepository
import fr.dcproject.component.article.database.ArticleForView import fr.dcproject.component.article.database.ArticleForView
import fr.dcproject.component.article.database.ArticleRef import fr.dcproject.component.article.database.ArticleRef
import fr.dcproject.component.article.database.ArticleRepository import fr.dcproject.component.article.database.ArticleRepository
@@ -24,7 +24,7 @@ object GetOneArticle {
val article = ArticleRef(article) 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> { get<ArticleRequest> {
val article: ArticleForView = repo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found") val article: ArticleForView = repo.findById(it.article.id) ?: throw NotFoundException("Article ${it.article.id} not found")
ac.assert { canView(article, citizenOrNull) } ac.assert { canView(article, citizenOrNull) }
@@ -64,7 +64,7 @@ object GetOneArticle {
val total: Int = a.votes.total val total: Int = a.votes.total
val score: Int = a.votes.score val score: Int = a.votes.score
} }
val views: Any = viewManager.getViewsCount(article).let { v -> val views: Any = viewRepository.getViewsCount(article).let { v ->
object { object {
val total = v.total val total = v.total
val unique = v.unique val unique = v.unique
@@ -76,7 +76,7 @@ object GetOneArticle {
) )
launch { launch {
viewManager.addView(call.request.local.remoteHost, article, citizenOrNull) viewRepository.addView(call.request.local.remoteHost, article, citizenOrNull)
} }
} }
} }

View File

@@ -1,7 +1,7 @@
package fr.dcproject.component.views package fr.dcproject.component.views
import fr.dcproject.application.Configuration import fr.dcproject.application.Configuration
import fr.dcproject.component.article.ArticleViewManager import fr.dcproject.component.article.ArticleViewRepository
import fr.dcproject.component.article.database.ArticleForView import fr.dcproject.component.article.database.ArticleForView
import org.apache.http.HttpHost import org.apache.http.HttpHost
import org.elasticsearch.client.RestClient import org.elasticsearch.client.RestClient
@@ -17,6 +17,6 @@ val viewKoinModule = module {
).build().apply { ).build().apply {
createEsIndexForViews() createEsIndexForViews()
} }
ArticleViewManager<ArticleForView>(esClient) ArticleViewRepository<ArticleForView>(esClient)
} }
} }

View File

@@ -2,14 +2,13 @@ package fr.dcproject.component.views
import fr.dcproject.component.citizen.database.CitizenI import fr.dcproject.component.citizen.database.CitizenI
import fr.dcproject.component.views.entity.ViewAggregation import fr.dcproject.component.views.entity.ViewAggregation
import org.elasticsearch.client.Response
import org.joda.time.DateTime import org.joda.time.DateTime
interface ViewManager <T> { interface ViewRepository <T> {
/** /**
* Add view to one entity * 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 * Get Views aggregations

View File

@@ -2,7 +2,7 @@ package functional
import fr.dcproject.application.Env.TEST import fr.dcproject.application.Env.TEST
import fr.dcproject.application.module import fr.dcproject.application.module
import fr.dcproject.component.article.ArticleViewManager import fr.dcproject.component.article.ArticleViewRepository
import fr.dcproject.component.article.database.ArticleForView import fr.dcproject.component.article.database.ArticleForView
import fr.dcproject.component.auth.database.UserCreator import fr.dcproject.component.auth.database.UserCreator
import fr.dcproject.component.citizen.database.CitizenCreator import fr.dcproject.component.citizen.database.CitizenCreator
@@ -44,33 +44,33 @@ class ViewTest {
val citizenRef = CitizenRef() val citizenRef = CitizenRef()
withTestApplication({ module(TEST) }) { withTestApplication({ module(TEST) }) {
val viewManager: ArticleViewManager<ArticleForView> = application.get() val viewRepository: ArticleViewRepository<ArticleForView> = application.get()
/* Get view before */ /* Get view before */
val startView = viewManager.getViewsCount(article) val startView = viewRepository.getViewsCount(article)
/* Add View */ /* Add View */
viewManager.addView( viewRepository.addView(
"1.2.3.4", "1.2.3.4",
article, article,
citizenRef citizenRef
) )
/* Add View */ /* Add View */
viewManager.addView( viewRepository.addView(
"10.10.10.10", "10.10.10.10",
article, article,
citizenRef citizenRef
) )
/* Add View */ /* Add View */
viewManager.addView( viewRepository.addView(
"8.8.8.8", "8.8.8.8",
article article
) )
/* Add View */ /* Add View */
viewManager.addView( viewRepository.addView(
"1.1.1.1", "1.1.1.1",
article article
) )
@@ -79,7 +79,7 @@ class ViewTest {
Thread.sleep(1000) Thread.sleep(1000)
/* Get view */ /* Get view */
val afterView = viewManager.getViewsCount(article) val afterView = viewRepository.getViewsCount(article)
/* Check if view has increment */ /* Check if view has increment */
afterView.total `should be equal to` startView.total + 4 afterView.total `should be equal to` startView.total + 4