/
utils.go
183 lines (164 loc) · 5.51 KB
/
utils.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
package utils
import (
"encoding/json"
"io/ioutil"
"os"
"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/s3/s3manager"
"github.com/kennybebel/testgostruc/model"
)
func Unmarshal(fileName string, data interface{}) error {
fileBody, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
err = json.Unmarshal(fileBody, &data)
return err
}
//BatchQuery will query a table for all the items that have the supplierID passed
func BatchQueryOrder(tableName string, SupplierID string, filter string, client *dynamodb.DynamoDB) (*dynamodb.QueryOutput, error) {
if filter == "" {
// Creating the query parameters which will get all test Order from dynamodb
inputQuery := &dynamodb.QueryInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":SupplierID": {
S: aws.String(SupplierID),
},
},
KeyConditionExpression: aws.String("SupplierID = :SupplierID"),
ProjectionExpression: aws.String("SupplierID, SortKey"),
TableName: aws.String(tableName),
}
// result will hold the results of the Query of TradeOrders table for all test data
result, err := client.Query(inputQuery)
if err != nil {
return result, err
}
return result, nil
}
// Creating the query parameters which will get the test Order from dynamodb
inputQuery := &dynamodb.QueryInput{
ExpressionAttributeNames: map[string]*string{
"#Type": aws.String("Type"),
},
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":SupplierID": {
S: aws.String("RAPISAMTEST"),
},
":Type": {
S: aws.String("DELIVERY"),
},
},
KeyConditionExpression: aws.String("SupplierID = :SupplierID"),
FilterExpression: aws.String("#Type = :Type"),
ProjectionExpression: aws.String("SupplierID, SortKey"),
TableName: aws.String(tableName),
}
// queryResult will hold the results of the Query of TradeOrders table for all test data
result, err := client.Query(inputQuery)
if err != nil {
return result, err
}
return result, nil
}
// Creating the query parameters which will get the test stock from dynamodb
func BatchQueryStock(tableName string, SupplierID string, client *dynamodb.DynamoDB) (*dynamodb.QueryOutput, error) {
StockQuery := &dynamodb.QueryInput{
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":SupplierID": {
S: aws.String(SupplierID),
},
},
KeyConditionExpression: aws.String("SupplierID = :SupplierID"),
ProjectionExpression: aws.String("SupplierID, ProductID, Warehouse,Stock "),
TableName: aws.String("tableName"),
}
// result will hold the results of the Query of TradeStock table for all test data
result, err := client.Query(StockQuery)
if err != nil {
return result, err
}
return result, nil
}
//BatchDeleteOrder will delete all orders from the table given
func BatchDeleteOrder(orders []model.OrderInfo, tableName string, client *dynamodb.DynamoDB) error {
table := make(map[string][]*dynamodb.WriteRequest)
var orderKey []*dynamodb.WriteRequest
// looping through the order slice and creating key for each Order to be deleted
for _, order := range orders {
item := &dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Key: map[string]*dynamodb.AttributeValue{
"SupplierID": {
S: aws.String(order.SupplierID),
},
"SortKey": {
S: aws.String(order.SortKey),
},
},
},
}
// adding the Key to items array to use for the BatchWriteItemInput(batch delete)
orderKey = append(orderKey, item)
}
// input holds the values(informtion) need for the items needed to be deleted from the TradeOrders table
table[tableName] = orderKey
input := &dynamodb.BatchWriteItemInput{
RequestItems: table,
ReturnConsumedCapacity: aws.String("NONE"),
}
// result will hold the results of the items deleted message
_, err := client.BatchWriteItem(input)
if err != nil {
return err
}
return err
}
func BatchDeleteStock(stocks []model.StockInfo, tableName string, client *dynamodb.DynamoDB) error {
stockTable := make(map[string][]*dynamodb.WriteRequest)
var stockItems []*dynamodb.WriteRequest
// looping through the stocks slice and creating key for each stock to be deleted
for _, stock := range stocks {
item := &dynamodb.WriteRequest{
DeleteRequest: &dynamodb.DeleteRequest{
Key: map[string]*dynamodb.AttributeValue{
"SupplierID": {
S: aws.String(stock.SupplierID),
},
"WarehouseProductID": {
S: aws.String(stock.WarehouseProductID),
},
},
},
}
// adding the Key to items array to use for the BatchWriteItemInput(batch delete)
stockItems = append(stockItems, item)
}
// input holds the values(informtion) need for the items needed to be deleted from the TradeOrders table
stockTable[tableName] = stockItems
input := &dynamodb.BatchWriteItemInput{
RequestItems: stockTable,
ReturnConsumedCapacity: aws.String("NONE"),
}
// result will hold the results of the items deleted message
_, err := client.BatchWriteItem(input)
if err != nil {
return err
}
return err
}
func UploadFileToS3(file *os.File, awsSession *session.Session) (*s3manager.UploadOutput, error) {
uploader := s3manager.NewUploader(awsSession)
// Uploads the file(stored in delivFile) to the S3 bucket.
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String("rapidtradeinboxs"),
Key: aws.String("Deliv.json"),
Body: file,
})
if err != nil {
return result, err
}
return result, err
}