-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbapi.go
286 lines (271 loc) · 7.04 KB
/
dbapi.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"errors"
"fmt"
"os"
"reflect"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
)
const (
partitionKey = "Email"
)
func tableStruct(tableName string) *dynamodb.CreateTableInput {
return &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition{
{
AttributeName: aws.String(partitionKey),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement{
{
AttributeName: aws.String(partitionKey),
KeyType: aws.String("HASH"),
},
},
BillingMode: aws.String(dynamodb.BillingModePayPerRequest), // ondemand
TableName: aws.String(tableName),
}
}
func validTable(db dynamodb.DynamoDB, tableName string) bool {
input := &dynamodb.ListTablesInput{}
isTableName := false
// 한번에 최대 100개의 테이블만 가지고 올 수 있다.
// 한 리전에 최대 256개의 테이블이 존재할 수 있다.
// https://docs.aws.amazon.com/ko_kr/amazondynamodb/latest/developerguide/Limits.html
for {
result, err := db.ListTables(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeInternalServerError:
fmt.Fprintf(os.Stderr, "%s %s\n", dynamodb.ErrCodeInternalServerError, err.Error())
default:
fmt.Fprintf(os.Stderr, "%s\n", aerr.Error())
}
} else {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
return false
}
for _, n := range result.TableNames {
if *n == tableName {
isTableName = true
break
}
}
if isTableName {
break
}
input.ExclusiveStartTableName = result.LastEvaluatedTableName
if result.LastEvaluatedTableName == nil {
break
}
}
return isTableName
}
func hasItem(db dynamodb.DynamoDB, tableName string, primarykey string) (bool, error) {
input := &dynamodb.GetItemInput{
TableName: aws.String(tableName),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {
S: aws.String(primarykey),
},
},
}
result, err := db.GetItem(input)
if err != nil {
return false, err
}
if result.Item == nil {
return false, nil
}
return true, nil
}
// AddUser 는 사용자를 추가하는 함수이다.
func AddUser(db dynamodb.DynamoDB) error {
hasBool, err := hasItem(db, *flagTable, *flagEmail)
if err != nil {
return err
}
if hasBool {
return errors.New("The data already exists. Can not add data")
}
item := User{
Email: *flagEmail,
UpdateDate: *flagUpdateDate,
NameKor: *flagNameKor,
NameEng: *flagNameEng,
Jobcode: *flagJobcode,
Bank: *flagBank,
BankAccount: *flagBankAccount,
SharesNum: *flagSharesNum,
CostHourly: *flagCostHourly,
CostWeekly: *flagCostWeekly,
CostMonthly: *flagCostMonthly,
CostYearly: *flagCostYearly,
MonetaryUnit: *flagMonetaryUnit,
Working: *flagWorking,
Projects: strings.Split(*flagProjects, ","),
}
dynamodbJSON, err := dynamodbattribute.MarshalMap(item)
if err != nil {
return err
}
data := &dynamodb.PutItemInput{
Item: dynamodbJSON,
TableName: aws.String(*flagTable),
}
_, err = db.PutItem(data)
if err != nil {
return err
}
return nil
}
// SetUser 는 유저자료구조를 수정하는 함수이다.
func SetUser(db dynamodb.DynamoDB) error {
input := &dynamodb.GetItemInput{
TableName: aws.String(*flagTable),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {
S: aws.String(*flagEmail),
},
},
}
result, err := db.GetItem(input)
if err != nil {
return err
}
u := User{}
err = dynamodbattribute.UnmarshalMap(result.Item, &u)
if err != nil {
return err
}
if *flagNameKor != "" && u.NameKor != *flagNameKor {
u.NameKor = *flagNameKor
}
if *flagNameEng != "" && u.NameEng != *flagNameEng {
u.NameEng = *flagNameEng
}
if *flagJobcode != 0 && u.Jobcode != *flagJobcode {
u.Jobcode = *flagJobcode
}
if *flagBank != "" && u.Bank != *flagBank {
u.Bank = *flagBank
}
if *flagBankAccount != "" && u.BankAccount != *flagBankAccount {
u.BankAccount = *flagBankAccount
}
if *flagSharesNum != 0 && u.SharesNum != *flagSharesNum {
u.SharesNum = *flagSharesNum
}
if *flagCostHourly != 0 && u.CostHourly != *flagCostHourly {
u.CostHourly = *flagCostHourly
}
if *flagCostWeekly != 0 && u.CostWeekly != *flagCostWeekly {
u.CostWeekly = *flagCostWeekly
}
if *flagCostMonthly != 0 && u.CostMonthly != *flagCostMonthly {
u.CostMonthly = *flagCostMonthly
}
if *flagCostYearly != 0 && u.CostYearly != *flagCostYearly {
u.CostYearly = *flagCostYearly
}
if *flagMonetaryUnit != "KRW" && u.MonetaryUnit != *flagMonetaryUnit {
u.MonetaryUnit = *flagMonetaryUnit
}
if u.Working != *flagWorking {
u.Working = *flagWorking
}
if *flagProjects != "" && reflect.DeepEqual(u.Projects, strings.Split(*flagProjects, ",")) == false {
u.Projects = strings.Split(*flagProjects, ",")
}
u.UpdateDate = *flagUpdateDate
dynamodbJSON, err := dynamodbattribute.MarshalMap(u)
if err != nil {
return err
}
data := &dynamodb.PutItemInput{
Item: dynamodbJSON,
TableName: aws.String(*flagTable),
}
_, err = db.PutItem(data)
if err != nil {
return err
}
return nil
}
// RmUser 는 유저자료구조를 사용자를 삭제하는 함수이다.
func RmUser(db dynamodb.DynamoDB) error {
input := &dynamodb.DeleteItemInput{
TableName: aws.String(*flagTable),
Key: map[string]*dynamodb.AttributeValue{
partitionKey: {
S: aws.String(*flagEmail),
},
},
}
_, err := db.DeleteItem(input)
if err != nil {
return err
}
return nil
}
// GetUsers 는 사용자를 가지고오는 함수이다.
func GetUsers(db dynamodb.DynamoDB, word string) error {
proj := expression.NamesList(
expression.Name("Email"),
expression.Name("NameKor"),
expression.Name("NameEng"),
expression.Name("Jobcode"),
expression.Name("Bank"),
expression.Name("BankAccount"),
expression.Name("SharesNum"),
expression.Name("CostHourly"),
expression.Name("CostWeekly"),
expression.Name("CostMonthly"),
expression.Name("CostYearly"),
expression.Name("MonetaryUnit"),
expression.Name("Working"),
expression.Name("Projects"),
)
filt1 := expression.Name("Email").Contains(word)
filt2 := expression.Name("NameKor").Contains(word)
filt3 := expression.Name("NameEng").Contains(word)
filt4 := expression.Name("Projects").Contains(word)
expr, err := expression.NewBuilder().
WithFilter(filt1.
Or(filt2).
Or(filt3).
Or(filt4)).
WithProjection(proj).
Build()
if err != nil {
return err
}
params := &dynamodb.ScanInput{
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
FilterExpression: expr.Filter(),
ProjectionExpression: expr.Projection(),
TableName: aws.String(*flagTable),
}
result, err := db.Scan(params)
if err != nil {
return err
}
for _, i := range result.Items {
u := User{}
err = dynamodbattribute.UnmarshalMap(i, &u)
if err != nil {
return err
}
fmt.Println(u)
}
return nil
}