File tree Expand file tree Collapse file tree
web/app/net/rouly/employability/web Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ lazy val commonSettings = Seq(
1313 resolvers += Resolver .bintrayRepo(" jrouly" , " sbt-release" ),
1414 organization := " net.rouly" ,
1515 scalaVersion := " 2.11.12" ,
16- version := " 1.1 " ,
16+ version := " 1.2 " ,
1717 name := s " employability- ${name.value}"
1818) ++ Bintray .settings
1919
Original file line number Diff line number Diff line change 11package net .rouly .employability .web .api
22
3+ import net .rouly .common .server .play .implicits .ResultBodyImplicits ._
34import net .rouly .employability .models .Topic
45import net .rouly .employability .web .elasticsearch .{DocumentService , TopicService }
56import play .api .libs .json .{Format , Json }
67import play .api .mvc .{AbstractController , ControllerComponents }
78
9+ import scala .concurrent .ExecutionContext
10+
811class ApiController (
912 cc : ControllerComponents ,
1013 documentService : DocumentService ,
1114 topicService : TopicService
12- ) extends AbstractController (cc) {
15+ )(implicit ec : ExecutionContext )
16+ extends AbstractController (cc) {
1317
1418 def allTopics = {
1519 Action (topicService.topicSource.chunkedResponse)
1620 }
1721
1822 def topicById (id : String ) = {
19- Action {
23+ Action .async {
2024 topicService
21- .topicSource
22- .filter(_.id == id)
23- .take(1 )
24- .chunkedResponse
25+ .topicById(id)
26+ .toJsonResult
2527 }
2628 }
2729
2830 def docsByTopicId (id : String ) = {
29- Action (documentService.documentsByTopic(id).chunkedResponse)
31+ Action .async {
32+ documentService
33+ .documentById(id)
34+ .toJsonResult
35+ }
3036 }
3137
3238}
Original file line number Diff line number Diff line change @@ -39,20 +39,21 @@ class ElasticsearchController(
3939
4040 def allTopics = {
4141 Action .async {
42- for {
43- topics <- topicService.topicSource.runWith(Sink .collection)
44- } yield Ok (application.topics(topics.toList.sortBy(_.id.toInt))).cached
42+ topicService
43+ .topicSource
44+ .runWith(Sink .collection)
45+ .map(_.toList.sortBy(_.id.toInt))
46+ .as(application.topics.apply)
47+ .cached
4548 }
4649 }
4750
4851 def topicById (id : String ) = {
4952 Action .async {
5053 for {
51- topic <- topicService.topicSource.filter(_.id == id).runWith( Sink .headOption )
54+ topic <- topicService.topicById(id )
5255 docs <- documentService.documentsByTopic(id).take(10 ).runWith(Sink .collection)
53- } yield {
54- topic.render(application.topic(_, docs.toList)).cached
55- }
56+ } yield Ok (application.topic(topic, docs.toList)).cached
5657 }
5758 }
5859
@@ -67,9 +68,10 @@ class ElasticsearchController(
6768
6869 def docById (id : String ) = {
6970 Action .async {
70- for {
71- doc <- documentService.documentSource.filter(_.id == id).runWith(Sink .headOption)
72- } yield doc.render(application.document.apply).cached
71+ documentService
72+ .documentById(id)
73+ .as(application.document.apply)
74+ .cached
7375 }
7476 }
7577
Original file line number Diff line number Diff line change @@ -3,18 +3,33 @@ package net.rouly.employability.web
33import play .api .mvc .{Result , Results }
44import play .twirl .api .Html
55
6+ import scala .concurrent .{ExecutionContext , Future }
7+
68package object application {
79
8- implicit class OptionalRender [T ](option : Option [T ]) {
9- def render (r : T => Html ): Result = option match {
10+ implicit class RenderOption [T ](option : Option [T ]) {
11+ def as (r : T => Html ): Result = option match {
1012 case Some (t) => Results .Ok (r(t))
1113 case None => Results .NotFound
1214 }
1315 }
1416
17+ implicit class RenderT [T ](t : T ) {
18+ def as (r : T => Html ): Result = Results .Ok (r(t))
19+ }
20+
21+ implicit class RenderFutureT [T ](ft : Future [T ]) {
22+ def as (r : T => Html )(implicit ec : ExecutionContext ): Future [Result ] = ft.map(t => Results .Ok (r(t)))
23+ }
24+
1525 implicit class RichResult (result : Result ) {
1626 def cached : Result = result.withHeaders(" Cache-Control" -> " public" )
1727 def notCached : Result = result.withHeaders(" Cache-Control" -> " no-store" )
1828 }
1929
30+ implicit class RichFutureResult (fresult : Future [Result ]) {
31+ def cached (implicit ec : ExecutionContext ): Future [Result ] = fresult.map(_.withHeaders(" Cache-Control" -> " public" ))
32+ def notCached (implicit ec : ExecutionContext ): Future [Result ] = fresult.map(_.withHeaders(" Cache-Control" -> " no-store" ))
33+ }
34+
2035}
Original file line number Diff line number Diff line change @@ -26,6 +26,12 @@ class DocumentService(elasticsearch: ElasticsearchModule)(implicit ec: Execution
2626 .map(_.to[ModeledDocument ])
2727 }
2828
29+ def documentById (docId : String ): Future [ModeledDocument ] = {
30+ execute {
31+ get(docId).from(elasticsearch.config.modeledDocumentIndex)
32+ }.map(_.result.to[ModeledDocument ])
33+ }
34+
2935 def documentsByTopic (topicId : String ): Source [ModeledDocument , NotUsed ] = {
3036 val weightQuery = rangeQuery(" weightedTopics.weight" ).gt(0.1 )
3137 val topicIdQuery = termQuery(" weightedTopics.topic.id" , topicId)
Original file line number Diff line number Diff line change @@ -25,4 +25,10 @@ class TopicService(elasticsearch: ElasticsearchModule)(implicit ec: ExecutionCon
2525 .map(_.to[Topic ])
2626 }
2727
28+ def topicById (topicId : String ): Future [Topic ] = {
29+ execute {
30+ get(topicId).from(elasticsearch.config.topicIndex)
31+ }.map(_.result.to[Topic ])
32+ }
33+
2834}
You can’t perform that action at this time.
0 commit comments