-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathDoobie.scala
More file actions
75 lines (65 loc) · 2.04 KB
/
Doobie.scala
File metadata and controls
75 lines (65 loc) · 2.04 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
package io.pager.subscription.chat
import doobie.implicits._
import doobie.util.query.Query0
import doobie.util.transactor.Transactor
import doobie.util.update.Update0
import io.pager.client.telegram.ChatId
import io.pager.subscription.Repository.Name
import io.pager.subscription.Subscription
import zio.Task
import zio.interop.catz._
private[chat] final case class Doobie(xa: Transactor[Task]) extends ChatStorage {
override def subscribe(chatId: ChatId, name: Name): Task[Unit] =
SQL
.create(chatId, name)
.withUniqueGeneratedKeys[Long]("ID")
.transact(xa)
.unit
.orDie
override def unsubscribe(chatId: ChatId, name: Name): Task[Unit] =
SQL
.delete(chatId, name)
.run
.transact(xa)
.unit
.orDie
override def listSubscriptions(chatId: ChatId): Task[Set[Name]] =
SQL
.getByChat(chatId)
.to[Set]
.map(_.map(_.name))
.transact(xa)
.orDie
override def listSubscribers(name: Name): Task[Set[ChatId]] =
SQL
.getByName(name)
.to[Set]
.map(_.map(_.chatId))
.transact(xa)
.orDie
}
private object SQL {
def create(chatId: ChatId, name: Name): Update0 =
sql"""
INSERT INTO SUBSCRIPTION (CHAT_ID, REPOSITORY_NAME)
VALUES (${chatId.value}, ${name.value})
""".update
def delete(chatId: ChatId, name: Name): Update0 =
sql"""
DELETE from SUBSCRIPTION
WHERE CHAT_ID = ${chatId.value} AND REPOSITORY_NAME = ${name.value}
""".update
def getByChat(chatId: ChatId): Query0[Subscription] = sql"""
SELECT * FROM SUBSCRIPTION WHERE CHAT_ID = ${chatId.value}
""".query[Subscription]
def getByName(name: Name): Query0[Subscription] = sql"""
SELECT * FROM SUBSCRIPTION WHERE REPOSITORY_NAME = ${name.value}
""".query[Subscription]
def update(subscription: Subscription): Update0 =
sql"""
UPDATE SUBSCRIPTION SET
CHAT_ID = ${subscription.chatId.value},
REPOSITORY_NAME = ${subscription.name.value}
WHERE ID = ${subscription.id.value}
""".update
}