/
db.go
148 lines (120 loc) · 2.81 KB
/
db.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package db
import (
"fmt"
"log"
"os"
"github.com/marlosl/gpt-telegram-bot/consts"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
type DBClient struct {
TableName *string
Session *session.Session
}
type CacheRepository struct {
DBClient
}
type CacheItem struct {
PK string `json:"pk" dynamodbav:"PK"`
SK string `json:"sk" dynamodbav:"SK"`
}
var MESSAGE = "MESSAGE"
func NewDBClient(tableName string, config *aws.Config) (*DBClient, error) {
sess, err := session.NewSession(config)
if err != nil {
fmt.Println("Got an error creating a new session:")
fmt.Println(err)
return nil, err
}
return &DBClient{
TableName: &tableName,
Session: sess,
}, nil
}
func NewCacheRepository() (*CacheRepository, error) {
tableName := os.Getenv(consts.CacheTable)
dbClient, err := NewDBClient(tableName, nil)
if err != nil {
return nil, err
}
return &CacheRepository{
*dbClient,
}, nil
}
func (db *CacheRepository) SaveItem(item *string) error {
svc := dynamodb.New(db.Session)
dbItem := &CacheItem{
PK: MESSAGE,
SK: *item,
}
av, err := dynamodbattribute.MarshalMap(dbItem)
if err != nil {
log.Fatalf("Got error marshalling map: %s", err)
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: db.TableName,
}
_, err = svc.PutItem(input)
if err != nil {
log.Fatalf("Got error calling PutItem: %s", err)
}
fmt.Println("Successfully added '" + *item + "' to table " + *db.TableName)
return nil
}
func (db *CacheRepository) ItemExists(item string) bool {
dbItem, err := db.GetItem(item)
if err != nil {
fmt.Printf("Got error calling GetItem: %s\n", err)
return false
}
return dbItem != nil && *dbItem != ""
}
func (db *CacheRepository) GetItem(item string) (*string, error) {
svc := dynamodb.New(db.Session)
sk := item
input := &dynamodb.GetItemInput{
Key: map[string]*dynamodb.AttributeValue{
"PK": {
S: &MESSAGE,
},
"SK": {
S: &sk,
},
},
TableName: db.TableName,
}
result, err := svc.GetItem(input)
if err != nil {
fmt.Printf("Got error calling GetItem: %s\n", err)
}
dbItem := &CacheItem{}
err = dynamodbattribute.UnmarshalMap(result.Item, dbItem)
if err != nil {
fmt.Printf("Got error unmarshalling: %s\n", err)
}
return &dbItem.SK, nil
}
func (db *CacheRepository) DeleteItem(item string) error {
svc := dynamodb.New(db.Session)
sk := item
input := &dynamodb.DeleteItemInput{
Key: map[string]*dynamodb.AttributeValue{
"PK": {
S: &MESSAGE,
},
"SK": {
S: &sk,
},
},
TableName: db.TableName,
}
_, err := svc.DeleteItem(input)
if err != nil {
fmt.Printf("Got error calling DeleteItem: %v\n", err)
}
fmt.Println("Deleted '" + item + "' from table " + *db.TableName)
return nil
}