1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.keyword
21 
22 import org.make.api.technical.IdGeneratorComponent
23 import org.make.core.technical.Pagination
24 import org.make.core.question.QuestionId
25 import org.make.core.keyword._
26 
27 import scala.concurrent.Future
28 import scala.concurrent.ExecutionContext.Implicits.global
29 
30 trait DefaultKeywordServiceComponent extends KeywordServiceComponent {
31   this: PersistentKeywordServiceComponent with IdGeneratorComponent =>
32 
33   override lazy val keywordService: KeywordService = new DefaultKeywordService
34 
35   class DefaultKeywordService extends KeywordService {
36 
37     override def get(key: String, questionId: QuestionId): Future[Option[Keyword]] = {
38       persistentKeywordService.get(key, questionId)
39     }
40 
41     override def findTop(questionId: QuestionId, limit: Pagination.Limit): Future[Seq[Keyword]] = {
42       persistentKeywordService.findTop(questionId, limit)
43     }
44 
45     override def addAndReplaceTop(questionId: QuestionId, keywords: Seq[Keyword]): Future[Unit] = {
46       for {
47         _           <- persistentKeywordService.resetTop(questionId)
48         allKeywords <- persistentKeywordService.findAll(questionId)
49         (existingKeywords, newKeywords) = keywords.partition(kw => allKeywords.exists(_.key == kw.key))
50         _ <- persistentKeywordService.updateTop(questionId, existingKeywords)
51         _ <- persistentKeywordService.createKeywords(questionId, newKeywords)
52       } yield {}
53     }
54   }
55 }
Line Stmt Id Pos Tree Symbol Tests Code
38 24326 1403 - 1448 Apply org.make.api.keyword.PersistentKeywordService.get DefaultKeywordServiceComponent.this.persistentKeywordService.get(key, questionId)
42 27917 1562 - 1613 Apply org.make.api.keyword.PersistentKeywordService.findTop DefaultKeywordServiceComponent.this.persistentKeywordService.findTop(questionId, limit)
47 26379 1753 - 1753 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
47 23818 1727 - 2146 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultKeywordServiceComponent.this.persistentKeywordService.resetTop(questionId).flatMap[Unit](((x$7: Unit) => (x$7: Unit @unchecked) match { case _ => DefaultKeywordServiceComponent.this.persistentKeywordService.findAll(questionId).map[(Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))](((allKeywords: Seq[org.make.core.keyword.Keyword]) => { <synthetic> <artifact> private[this] val x$3: ((Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]), Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]) = keywords.partition(((kw: org.make.core.keyword.Keyword) => allKeywords.exists(((x$1: org.make.core.keyword.Keyword) => x$1.key.==(kw.key))))) match { case (x$2 @ (_1: Seq[org.make.core.keyword.Keyword], _2: Seq[org.make.core.keyword.Keyword]): (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])((existingKeywords @ _), (newKeywords @ _))) => scala.Tuple3.apply[(Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]), Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]](x$2, existingKeywords, newKeywords) }; val x$2: (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]) = x$3._1; val existingKeywords: Seq[org.make.core.keyword.Keyword] = x$3._2; val newKeywords: Seq[org.make.core.keyword.Keyword] = x$3._3; scala.Tuple2.apply[Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])](allKeywords, x$2) }))(scala.concurrent.ExecutionContext.Implicits.global).flatMap[Unit](((x$6: (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))) => (x$6: (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])) @unchecked) match { case (_1: Seq[org.make.core.keyword.Keyword], _2: (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])): (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))((allKeywords @ _), (_1: Seq[org.make.core.keyword.Keyword], _2: Seq[org.make.core.keyword.Keyword]): (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])((existingKeywords @ _), (newKeywords @ _))) => DefaultKeywordServiceComponent.this.persistentKeywordService.updateTop(questionId, existingKeywords).flatMap[Unit](((x$5: Unit) => (x$5: Unit @unchecked) match { case _ => DefaultKeywordServiceComponent.this.persistentKeywordService.createKeywords(questionId, newKeywords).map[Unit](((x$4: Unit) => (x$4: Unit @unchecked) match { case _ => () }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global)
48 23805 1822 - 1822 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
48 26068 1810 - 1810 Apply scala.Tuple2.apply scala.Tuple2.apply[Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])](allKeywords, x$2)
48 24778 1822 - 1822 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
48 22383 1810 - 2146 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultKeywordServiceComponent.this.persistentKeywordService.findAll(questionId).map[(Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))](((allKeywords: Seq[org.make.core.keyword.Keyword]) => { <synthetic> <artifact> private[this] val x$3: ((Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]), Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]) = keywords.partition(((kw: org.make.core.keyword.Keyword) => allKeywords.exists(((x$1: org.make.core.keyword.Keyword) => x$1.key.==(kw.key))))) match { case (x$2 @ (_1: Seq[org.make.core.keyword.Keyword], _2: Seq[org.make.core.keyword.Keyword]): (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])((existingKeywords @ _), (newKeywords @ _))) => scala.Tuple3.apply[(Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]), Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]](x$2, existingKeywords, newKeywords) }; val x$2: (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]) = x$3._1; val existingKeywords: Seq[org.make.core.keyword.Keyword] = x$3._2; val newKeywords: Seq[org.make.core.keyword.Keyword] = x$3._3; scala.Tuple2.apply[Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])](allKeywords, x$2) }))(scala.concurrent.ExecutionContext.Implicits.global).flatMap[Unit](((x$6: (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))) => (x$6: (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])) @unchecked) match { case (_1: Seq[org.make.core.keyword.Keyword], _2: (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])): (Seq[org.make.core.keyword.Keyword], (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword]))((allKeywords @ _), (_1: Seq[org.make.core.keyword.Keyword], _2: Seq[org.make.core.keyword.Keyword]): (Seq[org.make.core.keyword.Keyword], Seq[org.make.core.keyword.Keyword])((existingKeywords @ _), (newKeywords @ _))) => DefaultKeywordServiceComponent.this.persistentKeywordService.updateTop(questionId, existingKeywords).flatMap[Unit](((x$5: Unit) => (x$5: Unit @unchecked) match { case _ => DefaultKeywordServiceComponent.this.persistentKeywordService.createKeywords(questionId, newKeywords).map[Unit](((x$4: Unit) => (x$4: Unit @unchecked) match { case _ => () }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global)
49 22375 1897 - 1897 Select scala.Tuple3._3 x$3._3
49 24575 1879 - 1879 Select scala.Tuple3._2 x$3._2
49 25771 1878 - 1878 Select scala.Tuple3._1 x$3._1
50 27928 1984 - 1984 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
50 25692 1982 - 2146 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultKeywordServiceComponent.this.persistentKeywordService.updateTop(questionId, existingKeywords).flatMap[Unit](((x$5: Unit) => (x$5: Unit @unchecked) match { case _ => DefaultKeywordServiceComponent.this.persistentKeywordService.createKeywords(questionId, newKeywords).map[Unit](((x$4: Unit) => (x$4: Unit @unchecked) match { case _ => () }))(scala.concurrent.ExecutionContext.Implicits.global) }))(scala.concurrent.ExecutionContext.Implicits.global)
51 26459 2062 - 2062 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
51 24494 2060 - 2146 ApplyToImplicitArgs scala.concurrent.Future.map DefaultKeywordServiceComponent.this.persistentKeywordService.createKeywords(questionId, newKeywords).map[Unit](((x$4: Unit) => (x$4: Unit @unchecked) match { case _ => () }))(scala.concurrent.ExecutionContext.Implicits.global)
52 22903 2144 - 2146 Literal <nosymbol> ()