-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathSubscriptionLogic.scala
More file actions
76 lines (63 loc) · 2.89 KB
/
SubscriptionLogic.scala
File metadata and controls
76 lines (63 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package io.pager.subscription
import io.pager.client.telegram.ChatId
import io.pager.logging.Logger
import io.pager.subscription.Repository.{ Name, Version }
import zio.macros.annotation.mockable
import zio.{ RIO, Task, ZIO }
@mockable
trait SubscriptionLogic {
val subscriptionLogic: SubscriptionLogic.Service[Any]
}
object SubscriptionLogic {
trait Service[R] {
def subscribe(chatId: ChatId, name: Name): RIO[R, Unit]
def unsubscribe(chatId: ChatId, name: Name): RIO[R, Unit]
def listSubscriptions(chatId: ChatId): RIO[R, Set[Name]]
def listRepositories: RIO[R, Map[Name, Option[Version]]]
def listSubscribers(name: Name): RIO[R, Set[ChatId]]
def updateVersions(updatedVersions: Map[Name, Version]): RIO[R, Unit]
}
trait Live extends SubscriptionLogic {
def logger: Logger.Service
def chatStorage: ChatStorage.Service[Any]
def repositoryVersionStorage: RepositoryVersionStorage.Service[Any]
override val subscriptionLogic: Service[Any] = new Service[Any] {
override def subscribe(chatId: ChatId, name: Name): Task[Unit] =
logger.info(s"$chatId subscribed to $name") *>
chatStorage.subscribe(chatId, name) *>
repositoryVersionStorage.addRepository(name)
override def unsubscribe(chatId: ChatId, name: Name): Task[Unit] =
logger.info(s"Chat $chatId unsubscribed from $name") *>
chatStorage.unsubscribe(chatId, name)
override def listSubscriptions(chatId: ChatId): Task[Set[Name]] =
logger.info(s"Chat $chatId requested subscriptions") *>
chatStorage.listSubscriptions(chatId)
override def listRepositories: Task[Map[Name, Option[Version]]] =
logger.info(s"Listing repositories") *>
repositoryVersionStorage.listRepositories
override def listSubscribers(name: Name): Task[Set[ChatId]] =
logger.info(s"Listing repository ${name.value} subscribers") *>
chatStorage.listSubscribers(name)
override def updateVersions(updatedVersions: Map[Name, Version]): Task[Unit] =
ZIO
.foreach(updatedVersions) {
case (name, version) =>
logger.info(s"Updating repository ${name.value} version to $version") *>
repositoryVersionStorage.updateVersion(name, version)
}
.unit
}
}
object Live {
def make(
logger: Logger.Service,
chatStorageService: ChatStorage.Service[Any],
repositoryVersionStorageService: RepositoryVersionStorage.Service[Any]
): SubscriptionLogic.Service[Any] =
new SubscriptionLogic.Live {
override def logger: Logger.Service = Logger.Test
override def chatStorage: ChatStorage.Service[Any] = chatStorageService
override def repositoryVersionStorage: RepositoryVersionStorage.Service[Any] = repositoryVersionStorageService
}.subscriptionLogic
}
}