This repository was archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDynamoTodoStore.swift
138 lines (117 loc) · 4 KB
/
DynamoTodoStore.swift
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import NIO
import DynamoDB
import AWSSDKSwiftCore
public class DynamoTodoStore {
let dynamo : DynamoDB
let tableName: String
let listName : String = "list"
public init(
eventLoopGroup: EventLoopGroup,
tableName: String,
accessKeyId: String,
secretAccessKey: String,
sessionToken: String?,
region: Region)
{
self.dynamo = DynamoDB(
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
sessionToken: sessionToken,
region: region,
eventLoopGroupProvider: .shared(eventLoopGroup))
self.tableName = tableName
}
}
extension DynamoTodoStore: TodoStore {
public func getTodos() -> EventLoopFuture<[TodoItem]> {
return self.dynamo.query(.init(
expressionAttributeValues: [":id" : .init(s: listName)],
keyConditionExpression: "ListId = :id",
tableName: tableName))
.map { (output) -> ([TodoItem]) in
return output.items!
.compactMap { (attributes) -> TodoItem? in
return TodoItem(attributes: attributes)
}
.sorted { (t1, t2) -> Bool in
switch (t1.order, t2.order) {
case (.none, .none):
return false
case (.some(_), .none):
return true
case (.none, .some(_)):
return false
case (.some(let o1), .some(let o2)):
return o1 < o2
}
}
}
}
public func getTodo(id: String) -> EventLoopFuture<TodoItem> {
return self.dynamo.getItem(.init(key: ["ListId": .init(s: listName), "TodoId": .init(s: id)], tableName: tableName))
.flatMapThrowing { (output) throws -> TodoItem in
guard let attributes = output.item else {
throw TodoError.notFound
}
guard let todo = TodoItem(attributes: attributes) else {
throw TodoError.missingAttributes
}
return todo
}
}
public func createTodo(_ todo: TodoItem) -> EventLoopFuture<TodoItem> {
var attributes = todo.toDynamoItem()
attributes["ListId"] = .init(s: self.listName)
return self.dynamo.putItem(.init(item: attributes, tableName: tableName))
.map { _ in
return todo
}
}
public func patchTodo(id: String, patch: PatchTodo) -> EventLoopFuture<TodoItem> {
var updates: [String : DynamoDB.AttributeValueUpdate] = [:]
if let title = patch.title {
updates["Title"] = .init(action: .put, value: .init(s: title))
}
if let order = patch.order {
updates["Order"] = .init(action: .put, value: .init(n: String(order)))
}
if let completed = patch.completed {
updates["Completed"] = .init(action: .put, value: .init(bool: completed))
}
guard updates.count > 0 else {
return self.getTodo(id: id)
}
let update = DynamoDB.UpdateItemInput(
attributeUpdates: updates,
key: ["ListId": .init(s: listName), "TodoId": .init(s: id)],
returnValues: .allNew,
tableName: tableName)
return self.dynamo.updateItem(update)
.flatMapThrowing { (output) -> TodoItem in
guard let attributes = output.attributes else {
throw TodoError.notFound
}
guard let todo = TodoItem(attributes: attributes) else {
throw TodoError.missingAttributes
}
return todo
}
}
public func deleteTodos(ids: [String]) -> EventLoopFuture<Void> {
guard ids.count > 0 else {
return self.dynamo.client.eventLoopGroup.next().makeSucceededFuture(Void())
}
let writeRequests = ids.map { (id) in
DynamoDB.WriteRequest(deleteRequest: .init(key: ["ListId": .init(s: listName), "TodoId": .init(s: id)]))
}
return self.dynamo.batchWriteItem(.init(requestItems: [tableName : writeRequests]))
.map { _ in }
}
public func deleteAllTodos() -> EventLoopFuture<Void> {
return self.getTodos()
.flatMap { (todos) -> EventLoopFuture<Void> in
let ids = todos.map() { $0.id }
return self.deleteTodos(ids: ids)
}
}
}