@@ -3,70 +3,43 @@ package postgres
3
3
import (
4
4
"context"
5
5
6
+ "github.com/jackc/pgx/v5/pgxpool"
7
+
6
8
commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
7
- pg "github.com/code-payments/flipchat-server/database/postgres"
8
9
9
- "github.com/code-payments/flipchat-server/database/prisma/db"
10
10
"github.com/code-payments/flipchat-server/promoted"
11
11
)
12
12
13
13
type store struct {
14
- client * db.PrismaClient
15
- }
16
-
17
- // reset clears the PromotedChat table (used for testing).
18
- func (s * store ) reset () {
19
- ctx := context .Background ()
20
-
21
- promotedChats := s .client .PromotedChat .FindMany ().Delete ().Tx ()
22
- err := s .client .Prisma .Transaction (promotedChats ).Exec (ctx )
23
- if err != nil {
24
- panic (err )
25
- }
14
+ pool * pgxpool.Pool
26
15
}
27
16
28
17
// NewInPostgres creates a new PostgreSQL store for Promoted Chats.
29
- func NewInPostgres (client * db. PrismaClient ) promoted.Store {
18
+ func NewInPostgres (pool * pgxpool. Pool ) promoted.Store {
30
19
return & store {
31
- client : client ,
20
+ pool : pool ,
32
21
}
33
22
}
34
23
35
24
// GetPromotedChats retrieves promoted chats by topic from PostgreSQL.
36
25
func (s * store ) GetPromotedChats (ctx context.Context , topic string ) ([]* promoted.PromotedChat , error ) {
37
-
38
- prChats , err := s .client .PromotedChat .FindMany (
39
- db .PromotedChat .Topic .Equals (topic ),
40
- ).OrderBy (
41
- db .PromotedChat .Score .Order (db .SortOrderDesc ),
42
- ).Exec (ctx )
26
+ models , err := dbGetPromotedChats (ctx , s .pool , topic )
43
27
if err != nil {
44
28
return nil , err
45
29
}
46
30
47
- var chats []* promoted.PromotedChat
48
- for _ , prChat := range prChats {
49
-
50
- decodedChatID , err := pg .Decode (prChat .ChatID )
31
+ res := make ([]* promoted.PromotedChat , len (models ))
32
+ for i , model := range models {
33
+ res [i ], err = fromModel (model )
51
34
if err != nil {
52
35
return nil , err
53
36
}
54
-
55
- chats = append (chats , & promoted.PromotedChat {
56
- ChatID : & commonpb.ChatId {Value : decodedChatID },
57
- Score : prChat .Score ,
58
- Topic : prChat .Topic ,
59
- CreatedAt : prChat .CreatedAt ,
60
- UpdatedAt : prChat .UpdatedAt ,
61
- })
62
37
}
63
-
64
- return chats , nil
38
+ return res , nil
65
39
}
66
40
67
41
// PromoteChat promotes a chat (or updates the score if it already exists).
68
42
func (s * store ) PromoteChat (ctx context.Context , chatID * commonpb.ChatId , topic string , score int ) error {
69
-
70
43
if chatID == nil {
71
44
return promoted .ErrInvalidChatID
72
45
}
@@ -79,22 +52,16 @@ func (s *store) PromoteChat(ctx context.Context, chatID *commonpb.ChatId, topic
79
52
return promoted .ErrInvalidTopic
80
53
}
81
54
82
- encodedChatID := pg .Encode (chatID .Value )
83
-
84
- _ , err := s .client .PromotedChat .UpsertOne (
85
- db .PromotedChat .ChatIDTopic (
86
- db .PromotedChat .ChatID .Equals (encodedChatID ),
87
- db .PromotedChat .Topic .Equals (topic ),
88
- ),
89
- ).Create (
90
- db .PromotedChat .Chat .Link (db .Chat .ID .Equals (encodedChatID )),
91
- db .PromotedChat .Topic .Set (topic ),
92
- db .PromotedChat .Score .Set (score ),
93
- ).Update (
94
- db .PromotedChat .Score .Set (score ),
95
- ).Exec (ctx )
96
-
97
- return err
55
+ model , err := toModel (& promoted.PromotedChat {
56
+ ChatID : chatID ,
57
+ Topic : topic ,
58
+ Score : score ,
59
+ })
60
+ if err != nil {
61
+ return err
62
+ }
63
+
64
+ return model .dbUpsert (ctx , s .pool )
98
65
}
99
66
100
67
func (s * store ) DemoteChat (ctx context.Context , chatID * commonpb.ChatId , topic string ) error {
@@ -106,16 +73,12 @@ func (s *store) DemoteChat(ctx context.Context, chatID *commonpb.ChatId, topic s
106
73
return promoted .ErrInvalidTopic
107
74
}
108
75
109
- encodedChatID := pg .Encode (chatID .Value )
110
-
111
- _ , err := s .client .PromotedChat .FindMany (
112
- db .PromotedChat .ChatID .Equals (encodedChatID ),
113
- db .PromotedChat .Topic .Equals (topic ),
114
- ).Delete ().Exec (ctx )
76
+ return dbDemoteChat (ctx , s .pool , chatID , topic )
77
+ }
115
78
79
+ func (s * store ) reset () {
80
+ _ , err := s .pool .Exec (context .Background (), "DELETE FROM " + promotedChatsTableName )
116
81
if err != nil {
117
- return promoted . ErrNotFound
82
+ panic ( err )
118
83
}
119
-
120
- return nil
121
84
}
0 commit comments