/
dynamo.go
76 lines (61 loc) · 1.59 KB
/
dynamo.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
package db
import (
"context"
"fmt"
"time"
aws2 "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/Foundation-13/mwarehouse/src/service/aws"
"github.com/Foundation-13/mwarehouse/src/service/types"
)
const (
jobsTable = "Jobs"
)
type dynamoImpl struct {
db aws.DynamoWrapper
}
func (d *dynamoImpl) CreateJob(ctx context.Context, key string, fileName string) (*types.Job, error) {
job := types.Job{
FileName: fileName,
Key: key,
Created: time.Now().UTC().UnixNano(),
Status: types.JobStatusCreated,
}
av, err := dynamodbattribute.MarshalMap(job)
if err != nil {
return nil, fmt.Errorf("failed to marshal job: %w", err)
}
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws2.String(jobsTable),
}
_, err = d.db.PutItem(input)
if err != nil {
return nil, fmt.Errorf("failed to store job: %w", err)
}
return &job, nil
}
func (d *dynamoImpl) GetJobStatus(ctx context.Context, key string) (types.Job, error) {
input := &dynamodb.GetItemInput{
TableName: aws2.String(jobsTable),
Key: map[string]*dynamodb.AttributeValue{
"Key": {
S: aws2.String(key),
},
},
}
res, err := d.db.GetItem(input)
if err != nil {
return types.Job{}, fmt.Errorf("failed to get item: %w", err)
}
job := types.Job{}
err = dynamodbattribute.UnmarshalMap(res.Item, &job)
if err != nil {
return types.Job{}, fmt.Errorf("failed to unmarshal job: %w", err)
}
if job.Key == "" {
return types.Job{}, fmt.Errorf("job with a key: %v not found", key)
}
return job, nil
}