Skip to content

Commit 375c792

Browse files
committed
feat: add get item for Product object
1 parent aa918b4 commit 375c792

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

helpers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"github.com/farbodahm/dynamodb-optimistic-locking/pkg/tables"
66
)
77

8-
// populateProductsTable populates the products table with sample data.
8+
// populateProductsTable populates the Product table with sample data.
99
func populateProductsTable(dynamo ddb.DynamoDB) error {
1010

11-
sampleProducts := []tables.Products{
11+
sampleProduct := []tables.Product{
1212
{Id: "p#0", Name: "Product 0", Quantity: 10, Version: 1},
1313
{Id: "p#1", Name: "Product 1", Quantity: 4, Version: 1},
1414
{Id: "p#2", Name: "Product 2", Quantity: 2, Version: 1},
@@ -17,11 +17,11 @@ func populateProductsTable(dynamo ddb.DynamoDB) error {
1717
{Id: "p#5", Name: "Product 5", Quantity: 0, Version: 1},
1818
}
1919

20-
products := make([]ddb.DynamoDBWritable, len(sampleProducts))
21-
for i, product := range sampleProducts {
22-
products[i] = product
20+
Product := make([]ddb.DynamoDBWritable, len(sampleProduct))
21+
for i, product := range sampleProduct {
22+
Product[i] = product
2323
}
2424

25-
_, err := dynamo.AddBatch("products", products, 100)
25+
_, err := dynamo.AddBatch("products", Product, 100)
2626
return err
2727
}

main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55

66
"github.com/farbodahm/dynamodb-optimistic-locking/pkg/ddb"
7+
"github.com/farbodahm/dynamodb-optimistic-locking/pkg/tables"
78
"github.com/spf13/cobra"
89
)
910

@@ -32,4 +33,10 @@ func main() {
3233
log.Fatalln("Failed to populate products table:", err)
3334
}
3435
}
36+
37+
x, err := tables.GetProduct(*dynamo, "p#1")
38+
if err != nil {
39+
log.Fatalln("Failed to get product:", err)
40+
}
41+
log.Println(x)
3542
}

pkg/tables/products.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package tables
22

33
import (
4+
"log"
5+
6+
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
7+
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
48
"github.com/farbodahm/dynamodb-optimistic-locking/pkg/ddb"
59
)
610

711
// Products table
8-
type Products struct {
9-
ddb.DynamoDBWritable
12+
type Product struct {
13+
ddb.DynamoDBWritable `dynamodbav:",omitempty"`
1014

1115
Id string `dynamodbav:"id"`
1216
Name string `dynamodbav:"name"`
@@ -15,3 +19,31 @@ type Products struct {
1519
// Version field is used for implementing Optimistic locking
1620
Version int `dynamodbav:"version"`
1721
}
22+
23+
// GetProduct returns a product with the given id from the products table.
24+
// If no product with the given id exists, an empty product is returned.
25+
func GetProduct(d ddb.DynamoDB, id string) (Product, error) {
26+
var product Product
27+
28+
marshaledId, err := attributevalue.Marshal(id)
29+
30+
if err != nil {
31+
log.Printf("WARN: Couldn't marshal product id %v\n", id)
32+
return product, err
33+
}
34+
35+
query := map[string]types.AttributeValue{"id": marshaledId}
36+
result, err := d.GetItem("products", query)
37+
38+
if err != nil {
39+
log.Printf("WARN: Couldn't get product id %v\n", id)
40+
return product, err
41+
}
42+
43+
if err := attributevalue.UnmarshalMap(result, &product); err != nil {
44+
log.Printf("WARN: Couldn't unmarshal product id %v\n", id)
45+
return product, err
46+
}
47+
48+
return product, nil
49+
}

0 commit comments

Comments
 (0)