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.idea
21 
22 import org.make.api.idea.IdeaExceptions.{IdeaAlreadyExistsException, IdeaDoesnotExistsException}
23 import org.make.api.technical.{EventBusServiceComponent, IdGeneratorComponent, ShortenedNames}
24 import org.make.core.DateHelper
25 import org.make.core.idea.indexed.IdeaSearchResult
26 import org.make.core.idea.{Idea, IdeaId, IdeaSearchQuery, IdeaStatus}
27 import org.make.core.question.{Question, QuestionId}
28 
29 import scala.concurrent.ExecutionContext.Implicits.global
30 import scala.concurrent.Future
31 
32 trait DefaultIdeaServiceComponent extends IdeaServiceComponent {
33   this: PersistentIdeaServiceComponent
34     with EventBusServiceComponent
35     with IdeaSearchEngineComponent
36     with IdGeneratorComponent =>
37 
38   override val ideaService: IdeaService = new DefaultIdeaService
39 
40   class DefaultIdeaService extends IdeaService with ShortenedNames {
41 
42     override def fetchAll(ideaSearchQuery: IdeaSearchQuery): Future[IdeaSearchResult] = {
43       elasticsearchIdeaAPI.searchIdeas(ideaSearchQuery)
44     }
45 
46     override def fetchAllByIdeaIds(ids: Seq[IdeaId]): Future[Seq[Idea]] = {
47       persistentIdeaService.findAllByIdeaIds(ids)
48     }
49 
50     override def fetchOne(ideaId: IdeaId): Future[Option[Idea]] = {
51       persistentIdeaService.findOne(ideaId)
52     }
53 
54     override def fetchOneByName(questionId: QuestionId, name: String): Future[Option[Idea]] = {
55       persistentIdeaService.findOneByName(questionId, name)
56     }
57 
58     override def insert(name: String, question: Question): Future[Idea] = {
59       val idea: Idea =
60         Idea(
61           ideaId = idGenerator.nextIdeaId(),
62           name = name,
63           questionId = Some(question.questionId),
64           operationId = question.operationId,
65           createdAt = Some(DateHelper.now()),
66           updatedAt = Some(DateHelper.now())
67         )
68       persistentIdeaService.findOneByName(question.questionId, name).flatMap { result =>
69         if (result.isDefined) {
70           Future.failed(IdeaAlreadyExistsException(idea.name))
71         } else {
72           persistentIdeaService.persist(idea).map { idea =>
73             eventBusService.publish(IdeaCreatedEvent(ideaId = idea.ideaId, eventDate = DateHelper.now()))
74             idea
75           }
76 
77         }
78       }
79     }
80 
81     override def update(ideaId: IdeaId, name: String, status: IdeaStatus): Future[Int] = {
82       persistentIdeaService.findOne(ideaId).flatMap { result =>
83         if (result.isEmpty) {
84           Future.failed(IdeaDoesnotExistsException(ideaId.value))
85         } else {
86           persistentIdeaService.modify(ideaId = ideaId, name = name, status = status).map { idea =>
87             eventBusService.publish(IdeaUpdatedEvent(ideaId = ideaId, eventDate = DateHelper.now()))
88             idea
89           }
90         }
91       }
92     }
93   }
94 }
Line Stmt Id Pos Tree Symbol Tests Code
38 27094 1510 - 1532 Apply org.make.api.idea.DefaultIdeaServiceComponent.DefaultIdeaService.<init> org.make.api.idea.ideaservicetest new DefaultIdeaServiceComponent.this.DefaultIdeaService()
43 24717 1700 - 1749 Apply org.make.api.idea.IdeaSearchEngine.searchIdeas org.make.api.idea.ideaservicetest DefaultIdeaServiceComponent.this.elasticsearchIdeaAPI.searchIdeas(ideaSearchQuery)
47 22285 1839 - 1882 Apply org.make.api.idea.PersistentIdeaService.findAllByIdeaIds DefaultIdeaServiceComponent.this.persistentIdeaService.findAllByIdeaIds(ids)
51 26134 1964 - 2001 Apply org.make.api.idea.PersistentIdeaService.findOne org.make.api.idea.ideaservicetest DefaultIdeaServiceComponent.this.persistentIdeaService.findOne(ideaId)
55 24920 2111 - 2164 Apply org.make.api.idea.PersistentIdeaService.findOneByName DefaultIdeaServiceComponent.this.persistentIdeaService.findOneByName(questionId, name)
60 25171 2279 - 2279 Select org.make.core.idea.Idea.apply$default$5 org.make.core.idea.Idea.apply$default$5
60 22975 2279 - 2549 Apply org.make.core.idea.Idea.apply org.make.core.idea.Idea.apply(x$1, x$2, x$4, x$3, x$7, x$5, x$6)
61 22968 2304 - 2328 Apply org.make.core.technical.IdGenerator.nextIdeaId DefaultIdeaServiceComponent.this.idGenerator.nextIdeaId()
63 24177 2376 - 2401 Apply scala.Some.apply scala.Some.apply[org.make.core.question.QuestionId](question.questionId)
63 26590 2381 - 2400 Select org.make.core.question.Question.questionId question.questionId
64 27996 2427 - 2447 Select org.make.core.question.Question.operationId question.operationId
65 24841 2471 - 2493 Apply scala.Some.apply scala.Some.apply[java.time.ZonedDateTime](org.make.core.DateHelper.now())
65 27035 2476 - 2492 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
66 26058 2517 - 2539 Apply scala.Some.apply scala.Some.apply[java.time.ZonedDateTime](org.make.core.DateHelper.now())
66 22301 2522 - 2538 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
68 22434 2556 - 2964 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultIdeaServiceComponent.this.persistentIdeaService.findOneByName(question.questionId, name).flatMap[org.make.core.idea.Idea](((result: Option[org.make.core.idea.Idea]) => if (result.isDefined) scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaAlreadyExistsException.apply(idea.name)) else DefaultIdeaServiceComponent.this.persistentIdeaService.persist(idea).map[org.make.core.idea.Idea](((idea: org.make.core.idea.Idea) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaCreatedEvent.apply(idea.ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)))(scala.concurrent.ExecutionContext.Implicits.global)
68 26572 2592 - 2611 Select org.make.core.question.Question.questionId question.questionId
68 24637 2627 - 2627 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
69 24187 2651 - 2667 Select scala.Option.isDefined result.isDefined
70 24848 2681 - 2733 Apply scala.concurrent.Future.failed scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaAlreadyExistsException.apply(idea.name))
70 27919 2722 - 2731 Select org.make.core.idea.Idea.name idea.name
70 27042 2695 - 2732 Apply org.make.api.idea.IdeaExceptions.IdeaAlreadyExistsException.apply org.make.api.idea.IdeaExceptions.IdeaAlreadyExistsException.apply(idea.name)
70 22424 2681 - 2733 Block scala.concurrent.Future.failed scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaAlreadyExistsException.apply(idea.name))
72 24307 2801 - 2801 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
72 28159 2761 - 2945 ApplyToImplicitArgs scala.concurrent.Future.map DefaultIdeaServiceComponent.this.persistentIdeaService.persist(idea).map[org.make.core.idea.Idea](((idea: org.make.core.idea.Idea) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaCreatedEvent.apply(idea.ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)
72 27049 2761 - 2945 Block scala.concurrent.Future.map DefaultIdeaServiceComponent.this.persistentIdeaService.persist(idea).map[org.make.core.idea.Idea](((idea: org.make.core.idea.Idea) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaCreatedEvent.apply(idea.ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)
73 22907 2847 - 2915 Apply org.make.api.idea.IdeaCreatedEvent.apply IdeaCreatedEvent.apply(idea.ideaId, org.make.core.DateHelper.now())
73 26071 2873 - 2884 Select org.make.core.idea.Idea.ideaId idea.ideaId
73 26582 2823 - 2916 Apply org.make.api.technical.EventBusService.publish DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaCreatedEvent.apply(idea.ideaId, org.make.core.DateHelper.now()))
73 25179 2898 - 2914 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
82 22853 3115 - 3115 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
82 26527 3069 - 3487 ApplyToImplicitArgs scala.concurrent.Future.flatMap DefaultIdeaServiceComponent.this.persistentIdeaService.findOne(ideaId).flatMap[Int](((result: Option[org.make.core.idea.Idea]) => if (result.isEmpty) scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaDoesnotExistsException.apply(ideaId.value)) else DefaultIdeaServiceComponent.this.persistentIdeaService.modify(ideaId, name, status).map[Int](((idea: Int) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaUpdatedEvent.apply(ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)))(scala.concurrent.ExecutionContext.Implicits.global)
83 26203 3139 - 3153 Select scala.Option.isEmpty result.isEmpty
84 26517 3167 - 3222 Apply scala.concurrent.Future.failed scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaDoesnotExistsException.apply(ideaId.value))
84 22915 3181 - 3221 Apply org.make.api.idea.IdeaExceptions.IdeaDoesnotExistsException.apply org.make.api.idea.IdeaExceptions.IdeaDoesnotExistsException.apply(ideaId.value)
84 24045 3208 - 3220 Select org.make.core.idea.IdeaId.value ideaId.value
84 24317 3167 - 3222 Block scala.concurrent.Future.failed scala.concurrent.Future.failed[Nothing](org.make.api.idea.IdeaExceptions.IdeaDoesnotExistsException.apply(ideaId.value))
86 26056 3250 - 3469 ApplyToImplicitArgs scala.concurrent.Future.map DefaultIdeaServiceComponent.this.persistentIdeaService.modify(ideaId, name, status).map[Int](((idea: Int) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaUpdatedEvent.apply(ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)
86 22365 3330 - 3330 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
86 23884 3250 - 3469 Block scala.concurrent.Future.map DefaultIdeaServiceComponent.this.persistentIdeaService.modify(ideaId, name, status).map[Int](((idea: Int) => { DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaUpdatedEvent.apply(ideaId, org.make.core.DateHelper.now())); idea }))(scala.concurrent.ExecutionContext.Implicits.global)
87 26983 3376 - 3439 Apply org.make.api.idea.IdeaUpdatedEvent.apply IdeaUpdatedEvent.apply(ideaId, org.make.core.DateHelper.now())
87 27991 3422 - 3438 Apply org.make.core.DefaultDateHelper.now org.make.core.DateHelper.now()
87 24649 3352 - 3440 Apply org.make.api.technical.EventBusService.publish DefaultIdeaServiceComponent.this.eventBusService.publish(IdeaUpdatedEvent.apply(ideaId, org.make.core.DateHelper.now()))