@@ -3,70 +3,43 @@ package postgres
33import (
44 "context"
55
6+ "github.com/jackc/pgx/v5/pgxpool"
7+
68 commonpb "github.com/code-payments/flipchat-protobuf-api/generated/go/common/v1"
7- pg "github.com/code-payments/flipchat-server/database/postgres"
89
9- "github.com/code-payments/flipchat-server/database/prisma/db"
1010 "github.com/code-payments/flipchat-server/promoted"
1111)
1212
1313type 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
2615}
2716
2817// 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 {
3019 return & store {
31- client : client ,
20+ pool : pool ,
3221 }
3322}
3423
3524// GetPromotedChats retrieves promoted chats by topic from PostgreSQL.
3625func (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 )
4327 if err != nil {
4428 return nil , err
4529 }
4630
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 )
5134 if err != nil {
5235 return nil , err
5336 }
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- })
6237 }
63-
64- return chats , nil
38+ return res , nil
6539}
6640
6741// PromoteChat promotes a chat (or updates the score if it already exists).
6842func (s * store ) PromoteChat (ctx context.Context , chatID * commonpb.ChatId , topic string , score int ) error {
69-
7043 if chatID == nil {
7144 return promoted .ErrInvalidChatID
7245 }
@@ -79,22 +52,16 @@ func (s *store) PromoteChat(ctx context.Context, chatID *commonpb.ChatId, topic
7952 return promoted .ErrInvalidTopic
8053 }
8154
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 )
9865}
9966
10067func (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
10673 return promoted .ErrInvalidTopic
10774 }
10875
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+ }
11578
79+ func (s * store ) reset () {
80+ _ , err := s .pool .Exec (context .Background (), "DELETE FROM " + promotedChatsTableName )
11681 if err != nil {
117- return promoted . ErrNotFound
82+ panic ( err )
11883 }
119-
120- return nil
12184}
0 commit comments