-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create.go
107 lines (94 loc) · 2.94 KB
/
Create.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
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"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"
uuid "github.com/satori/go.uuid"
)
type Todo struct {
ID string `json:"id"`
Date string `json:"Date"`
Ticket string `json:"Ticket"`
Type string `json:"Type"`
SellPrice float64 `json:"SellPrice"`
Qt int `json:"Qt"`
TaxLiquidacao float64 `json:"TaxLiquidacao"`
TaxRegistro float64 `json:"TaxRegistro"`
TaxTermo float64 `json:"TaxTermo"`
TaxANA float64 `json:"TaxANA"`
TaxEmolumentos float64 `json:"TaxEmolumentos"`
TaxOperacional float64 `json:"TaxOperacional"`
TaxExecucao float64 `json:"TaxExecucao"`
TaxCustodia float64 `json:"TaxCustodia"`
TaxImpostos float64 `json:"TaxImpostos"`
TaxIRRF float64 `json:"TaxIRRF"`
TaxOutros float64 `json:"TaxOutros"`
TotalLiquidoNota float64 `json:"TotalLiquidoNota"`
TaxTotal float64 `json:"TaxTotal"`
TotalCalculo float64 `json:"TotalCalculo"`
AvgPriceNota float64 `json:"AvgPriceNota"`
AvgPriceCalculo float64 `json:"AvgPriceCalculo"`
DiffNota float64 `json:"DiffNota"`
DiffPrice float64 `json:"DiffPrice"`
CreatedAt string `json:"created_at"`
}
// type Todo struct {
// ID string `json:"id"`
// Description string `json:"description"`
// Done bool `json:"done"`
// CreatedAt string `json:"created_at"`
// }
var ddb *dynamodb.DynamoDB
func init() {
region := os.Getenv("AWS_REGION")
if session, err := session.NewSession(&aws.Config{ // Use aws sdk to connect to dynamoDB
Region: ®ion,
}); err != nil {
fmt.Println(fmt.Sprintf("Failed to connect to AWS: %s", err.Error()))
} else {
ddb = dynamodb.New(session) // Create DynamoDB client
}
}
func AddTodo(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
fmt.Println("AddTodo")
var (
id = uuid.Must(uuid.NewV4(), nil).String()
tableName = aws.String(os.Getenv("TODOS_TABLE_NAME"))
)
// Initialize todo
todo := &Todo{
ID: id,
CreatedAt: time.Now().String(),
}
// Parse request body
json.Unmarshal([]byte(request.Body), todo)
// Write to DynamoDB
item, _ := dynamodbattribute.MarshalMap(todo)
input := &dynamodb.PutItemInput{
Item: item,
TableName: tableName,
}
if _, err := ddb.PutItem(input); err != nil {
return events.APIGatewayProxyResponse{ // Error HTTP response
Body: err.Error(),
StatusCode: 500,
}, nil
} else {
body, _ := json.Marshal(todo)
return events.APIGatewayProxyResponse{ // Success HTTP response
Body: string(body),
StatusCode: 200,
}, nil
}
}
func main() {
lambda.Start(AddTodo)
}