/
trash.go
41 lines (36 loc) · 1.21 KB
/
trash.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
package thread
import (
"context"
"errors"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/harryzcy/mailbox/internal/api"
"github.com/harryzcy/mailbox/internal/env"
)
func Trash(ctx context.Context, client api.UpdateItemAPI, threadID string) error {
_, err := client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
TableName: aws.String(env.TableName),
Key: map[string]types.AttributeValue{
"MessageID": &types.AttributeValueMemberS{Value: threadID},
},
UpdateExpression: aws.String("SET TrashedTime = :val1"),
ConditionExpression: aws.String("attribute_not_exists(TrashedTime)"),
ExpressionAttributeValues: map[string]types.AttributeValue{
":val1": &types.AttributeValueMemberS{Value: time.Now().UTC().Format(time.RFC3339)},
},
})
if err != nil {
if apiErr := new(types.ConditionalCheckFailedException); errors.As(err, &apiErr) {
return &api.NotTrashedError{Type: "thread"}
}
if apiErr := new(types.ProvisionedThroughputExceededException); errors.As(err, &apiErr) {
return api.ErrTooManyRequests
}
return err
}
fmt.Println("trash thread finished successfully")
return nil
}