-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_bot_dialogs.go
86 lines (74 loc) · 2.25 KB
/
chat_bot_dialogs.go
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
77
78
79
80
81
82
83
84
85
86
package store
import (
"context"
sq "github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/l-orlov/slim-fairy/bot/internal/model"
)
// CreateChatBotDialog creates model.ChatBotDialog
func (s *Storage) CreateChatBotDialog(ctx context.Context, record *model.ChatBotDialog) error {
query := psql().
Insert(record.DbTable()).
SetMap(chatBotDialogAttrs(record)).
Suffix("RETURNING " + asteriskChatBotDialogs)
err := Getx(ctx, s.pool, record, query)
if err != nil {
return dbError(err)
}
return nil
}
// UpdateChatBotDialog updates model.ChatBotDialog
func (s *Storage) UpdateChatBotDialog(ctx context.Context, record *model.ChatBotDialog) error {
query := psql().
Update(record.DbTable()).
Where(sq.Eq{"id": record.ID}).
SetMap(chatBotDialogAttrs(record)).
Suffix("RETURNING " + asteriskChatBotDialogs)
err := Getx(ctx, s.pool, record, query)
if err != nil {
return dbError(err)
}
return nil
}
// UpdateChatBotDialogStatusTx updates status for model.ChatBotDialog in transaction
func (s *Storage) UpdateChatBotDialogStatusTx(ctx context.Context, tx Tx, status model.ChatBotDialogStatus, id uuid.UUID) error {
query := psql().
Update(model.ChatBotDialog{}.DbTable()).
Where(sq.Eq{"id": id}).
Set("status", status)
_, err := Execx(ctx, tx, query)
if err != nil {
return dbError(err)
}
return nil
}
// GetChatBotDialogByKeyFields gets last model.ChatBotDialog by user_telegram_id, kind, status
func (s *Storage) GetChatBotDialogByKeyFields(
ctx context.Context, userTelegramID int64,
kind model.ChatBotDialogKind, status model.ChatBotDialogStatus,
) (*model.ChatBotDialog, error) {
record := &model.ChatBotDialog{}
query := psql().
Select(asteriskChatBotDialogs).
From(record.DbTable()).
Where(sq.Eq{
"user_telegram_id": userTelegramID,
"kind": kind,
"status": status,
}).
OrderBy("created_at DESC").
Limit(1)
err := Getx(ctx, s.pool, record, query)
if err != nil {
return nil, dbError(err)
}
return record, nil
}
func chatBotDialogAttrs(record *model.ChatBotDialog) map[string]interface{} {
return map[string]interface{}{
"user_telegram_id": record.UserTelegramID,
"kind": record.Kind,
"status": record.Status,
"data": record.DataJSON,
}
}