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.personality
21 
22 import org.make.api.operation.OperationOfQuestionServiceComponent
23 import org.make.api.proposal.ProposalSearchEngineComponent
24 import org.make.api.question._
25 import org.make.api.technical.IdGeneratorComponent
26 import org.make.core.technical.Pagination
27 import org.make.core.personality.{Personality, PersonalityId, PersonalityRoleId}
28 import org.make.core.question.QuestionId
29 import org.make.core.user.UserId
30 import org.make.core.Order
31 
32 import scala.concurrent.ExecutionContext.Implicits.global
33 import scala.concurrent.Future
34 
35 trait DefaultQuestionPersonalityServiceComponent extends QuestionPersonalityServiceComponent {
36   this: PersistentQuestionPersonalityServiceComponent
37     with IdGeneratorComponent
38     with QuestionServiceComponent
39     with OperationOfQuestionServiceComponent
40     with ProposalSearchEngineComponent =>
41 
42   override lazy val questionPersonalityService: DefaultQuestionPersonalityService =
43     new DefaultQuestionPersonalityService
44 
45   class DefaultQuestionPersonalityService extends QuestionPersonalityService {
46 
47     override def getPersonality(personalityId: PersonalityId): Future[Option[Personality]] = {
48       persistentQuestionPersonalityService.getById(personalityId)
49     }
50 
51     override def createPersonality(request: CreateQuestionPersonalityRequest): Future[Personality] = {
52       val personality: Personality = Personality(
53         personalityId = idGenerator.nextPersonalityId(),
54         userId = request.userId,
55         questionId = request.questionId,
56         personalityRoleId = request.personalityRoleId
57       )
58       persistentQuestionPersonalityService.persist(personality)
59     }
60 
61     override def updatePersonality(
62       personalityId: PersonalityId,
63       request: UpdateQuestionPersonalityRequest
64     ): Future[Option[Personality]] = {
65       persistentQuestionPersonalityService.getById(personalityId).flatMap {
66         case Some(personality) =>
67           persistentQuestionPersonalityService
68             .modify(personality.copy(userId = request.userId, personalityRoleId = request.personalityRoleId))
69             .map(Some.apply)
70         case None => Future.successful(None)
71       }
72     }
73 
74     override def find(
75       offset: Pagination.Offset,
76       end: Option[Pagination.End],
77       sort: Option[String],
78       order: Option[Order],
79       userId: Option[UserId],
80       questionId: Option[QuestionId],
81       personalityRoleId: Option[PersonalityRoleId]
82     ): Future[Seq[Personality]] = {
83       persistentQuestionPersonalityService.find(offset, end, sort, order, userId, questionId, personalityRoleId)
84     }
85 
86     override def count(
87       userId: Option[UserId],
88       questionId: Option[QuestionId],
89       personalityRoleId: Option[PersonalityRoleId]
90     ): Future[Int] = {
91       persistentQuestionPersonalityService.count(userId, questionId, personalityRoleId)
92     }
93 
94     override def deletePersonality(personalityId: PersonalityId): Future[Unit] = {
95       persistentQuestionPersonalityService.delete(personalityId)
96     }
97 
98   }
99 }
Line Stmt Id Pos Tree Symbol Tests Code
48 27065 1910 - 1969 Apply org.make.api.personality.PersistentQuestionPersonalityService.getById DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.getById(personalityId)
52 22934 2117 - 2322 Apply org.make.core.personality.Personality.apply org.make.api.personality.questionpersonalityservicetest org.make.core.personality.Personality.apply(DefaultQuestionPersonalityServiceComponent.this.idGenerator.nextPersonalityId(), request.userId, request.questionId, request.personalityRoleId)
53 24728 2154 - 2185 Apply org.make.core.technical.IdGenerator.nextPersonalityId org.make.api.personality.questionpersonalityservicetest DefaultQuestionPersonalityServiceComponent.this.idGenerator.nextPersonalityId()
54 23522 2204 - 2218 Select org.make.api.personality.CreateQuestionPersonalityRequest.userId org.make.api.personality.questionpersonalityservicetest request.userId
55 27344 2241 - 2259 Select org.make.api.personality.CreateQuestionPersonalityRequest.questionId org.make.api.personality.questionpersonalityservicetest request.questionId
56 25015 2289 - 2314 Select org.make.api.personality.CreateQuestionPersonalityRequest.personalityRoleId org.make.api.personality.questionpersonalityservicetest request.personalityRoleId
58 26604 2329 - 2386 Apply org.make.api.personality.PersistentQuestionPersonalityService.persist org.make.api.personality.questionpersonalityservicetest DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.persist(personality)
65 23071 2627 - 2627 Select scala.concurrent.ExecutionContext.Implicits.global org.make.api.personality.questionpersonalityservicetest scala.concurrent.ExecutionContext.Implicits.global
65 26832 2559 - 2901 ApplyToImplicitArgs scala.concurrent.Future.flatMap org.make.api.personality.questionpersonalityservicetest DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.getById(personalityId).flatMap[Option[org.make.core.personality.Personality]](((x0$1: Option[org.make.core.personality.Personality]) => x0$1 match { case (value: org.make.core.personality.Personality): Some[org.make.core.personality.Personality]((personality @ _)) => DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.modify({ <artifact> val x$1: org.make.core.user.UserId = request.userId; <artifact> val x$2: org.make.core.personality.PersonalityRoleId = request.personalityRoleId; <artifact> val x$3: org.make.core.personality.PersonalityId = personality.copy$default$1; <artifact> val x$4: org.make.core.question.QuestionId = personality.copy$default$3; personality.copy(x$3, x$1, x$4, x$2) }).map[Some[org.make.core.personality.Personality]](((value: org.make.core.personality.Personality) => scala.Some.apply[org.make.core.personality.Personality](value)))(scala.concurrent.ExecutionContext.Implicits.global) case scala.None => scala.concurrent.Future.successful[None.type](scala.None) }))(scala.concurrent.ExecutionContext.Implicits.global)
68 24860 2742 - 2742 Select org.make.core.personality.Personality.copy$default$3 personality.copy$default$3
68 23531 2730 - 2818 Apply org.make.core.personality.Personality.copy personality.copy(x$3, x$1, x$4, x$2)
68 26904 2742 - 2742 Select org.make.core.personality.Personality.copy$default$1 personality.copy$default$1
68 23064 2792 - 2817 Select org.make.api.personality.UpdateQuestionPersonalityRequest.personalityRoleId request.personalityRoleId
68 25477 2756 - 2770 Select org.make.api.personality.UpdateQuestionPersonalityRequest.userId request.userId
69 24949 2836 - 2836 Select scala.concurrent.ExecutionContext.Implicits.global scala.concurrent.ExecutionContext.Implicits.global
69 22987 2673 - 2848 ApplyToImplicitArgs scala.concurrent.Future.map DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.modify({ <artifact> val x$1: org.make.core.user.UserId = request.userId; <artifact> val x$2: org.make.core.personality.PersonalityRoleId = request.personalityRoleId; <artifact> val x$3: org.make.core.personality.PersonalityId = personality.copy$default$1; <artifact> val x$4: org.make.core.question.QuestionId = personality.copy$default$3; personality.copy(x$3, x$1, x$4, x$2) }).map[Some[org.make.core.personality.Personality]](((value: org.make.core.personality.Personality) => scala.Some.apply[org.make.core.personality.Personality](value)))(scala.concurrent.ExecutionContext.Implicits.global)
69 27204 2837 - 2847 Apply scala.Some.apply scala.Some.apply[org.make.core.personality.Personality](value)
70 25412 2870 - 2893 Apply scala.concurrent.Future.successful scala.concurrent.Future.successful[None.type](scala.None)
70 26739 2888 - 2892 Select scala.None scala.None
83 24870 3217 - 3323 Apply org.make.api.personality.PersistentQuestionPersonalityService.find DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.find(offset, end, sort, order, userId, questionId, personalityRoleId)
91 23752 3503 - 3584 Apply org.make.api.personality.PersistentQuestionPersonalityService.count DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.count(userId, questionId, personalityRoleId)
95 27510 3681 - 3739 Apply org.make.api.personality.PersistentQuestionPersonalityService.delete DefaultQuestionPersonalityServiceComponent.this.persistentQuestionPersonalityService.delete(personalityId)