-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
81 lines (70 loc) · 1.58 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
package main
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
uuid "github.com/satori/go.uuid"
)
var db = dynamodb.New(session.New(), aws.NewConfig().WithRegion("eu-west-3"))
// func getItem(id string) (*book, error) {
// input := &dynamodb.GetItemInput{
// TableName: aws.String("Photo"),
// Key: map[string]*dynamodb.AttributeValue{
// "ID": {
// S: aws.String(id),
// },
// },
// }
// result, err := db.GetItem(input)
// if err != nil {
// return nil, err
// }
// if result.Item == nil {
// return nil, nil
// }
// bk := new(book)
// err = dynamodbattribute.UnmarshalMap(result.Item, bk)
// if err != nil {
// return nil, err
// }
// return bk, nil
// }
func createItem(ph *photo) error {
id, err := uuid.NewV4()
if err != nil {
return err
}
ph.ID = id.String()
return putItem(ph)
}
// Add a Photo record to DynamoDB.
func putItem(ph *photo) error {
input := &dynamodb.PutItemInput{
TableName: aws.String("Photo"),
Item: map[string]*dynamodb.AttributeValue{
"AlbumID": {
S: aws.String(ph.AlbumID),
},
"ID": {
S: aws.String(ph.ID),
},
"URL": {
S: aws.String(ph.URL),
},
"Tags": {
S: aws.String(strings.Join(ph.Tags, " ")),
},
"Description": {
S: aws.String(ph.Description),
},
"Date": {
S: aws.String(ph.Date),
},
},
}
output, err := db.PutItem(input)
fmt.Printf("-----> %v", output)
return err
}