Skip to content

Commit d96ab16

Browse files
Refactor ProductField
1 parent 8649522 commit d96ab16

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

Examples/LambdaFunctions/Sources/ProductAPI/Product+DynamoDB.swift

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,35 @@
1414

1515
import AWSDynamoDB
1616

17-
public struct ProductField {
18-
static let sku = "sku"
19-
static let name = "name"
20-
static let description = "description"
21-
static let createdAt = "createdAt"
22-
static let updatedAt = "updatedAt"
23-
}
24-
2517
extension Product {
18+
2619
public var dynamoDictionary: [String: DynamoDB.AttributeValue] {
2720
var dictionary = [
28-
ProductField.sku: DynamoDB.AttributeValue(s: sku),
29-
ProductField.name: DynamoDB.AttributeValue(s: name),
30-
ProductField.description: DynamoDB.AttributeValue(s: description),
21+
Field.sku: DynamoDB.AttributeValue(s: sku),
22+
Field.name: DynamoDB.AttributeValue(s: name),
23+
Field.description: DynamoDB.AttributeValue(s: description),
3124
]
3225
if let createdAt = createdAt {
33-
dictionary[ProductField.createdAt] = DynamoDB.AttributeValue(s: createdAt)
26+
dictionary[Field.createdAt] = DynamoDB.AttributeValue(s: createdAt)
3427
}
3528

3629
if let updatedAt = updatedAt {
37-
dictionary[ProductField.updatedAt] = DynamoDB.AttributeValue(s: updatedAt)
30+
dictionary[Field.updatedAt] = DynamoDB.AttributeValue(s: updatedAt)
3831
}
3932
return dictionary
4033
}
4134

4235
public init(dictionary: [String: DynamoDB.AttributeValue]) throws {
43-
guard let name = dictionary[ProductField.name]?.s,
44-
let sku = dictionary[ProductField.sku]?.s,
45-
let description = dictionary[ProductField.description]?.s
36+
guard let name = dictionary[Field.name]?.s,
37+
let sku = dictionary[Field.sku]?.s,
38+
let description = dictionary[Field.description]?.s
4639
else {
4740
throw APIError.invalidItem
4841
}
4942
self.name = name
5043
self.sku = sku
5144
self.description = description
52-
self.createdAt = dictionary[ProductField.createdAt]?.s
53-
self.updatedAt = dictionary[ProductField.updatedAt]?.s
45+
self.createdAt = dictionary[Field.createdAt]?.s
46+
self.updatedAt = dictionary[Field.updatedAt]?.s
5447
}
5548
}

Examples/LambdaFunctions/Sources/ProductAPI/Product.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,20 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
import Foundation
16+
1517
public struct Product: Codable {
1618
public let sku: String
1719
public let name: String
1820
public let description: String
1921
public var createdAt: String?
2022
public var updatedAt: String?
23+
24+
public struct Field {
25+
static let sku = "sku"
26+
static let name = "name"
27+
static let description = "description"
28+
static let createdAt = "createdAt"
29+
static let updatedAt = "updatedAt"
30+
}
2131
}

Examples/LambdaFunctions/Sources/ProductAPI/ProductService.swift

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ public enum APIError: Error {
2323
case invalidHandler
2424
}
2525

26-
extension Date {
27-
var iso8601: String {
26+
extension DateFormatter {
27+
static var iso8061: DateFormatter {
2828
let formatter = DateFormatter()
2929
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
3030
formatter.timeZone = TimeZone(secondsFromGMT: 0)
31+
return formatter
32+
}
33+
}
34+
35+
extension Date {
36+
var iso8601: String {
37+
let formatter = DateFormatter.iso8061
3138
return formatter.string(from: self)
3239
}
3340
}
@@ -45,9 +52,9 @@ public class ProductService {
4552
public func createItem(product: Product) -> EventLoopFuture<Product> {
4653

4754
var product = product
48-
let date = Date().iso8601
49-
product.createdAt = date
50-
product.updatedAt = date
55+
let date = Date()
56+
product.createdAt = date.iso8601
57+
product.updatedAt = date.iso8601
5158

5259
let input = DynamoDB.PutItemInput(
5360
item: product.dynamoDictionary,
@@ -60,7 +67,7 @@ public class ProductService {
6067

6168
public func readItem(key: String) -> EventLoopFuture<Product> {
6269
let input = DynamoDB.GetItemInput(
63-
key: [ProductField.sku: DynamoDB.AttributeValue(s: key)],
70+
key: [Product.Field.sku: DynamoDB.AttributeValue(s: key)],
6471
tableName: tableName
6572
)
6673
return db.getItem(input).flatMapThrowing { data -> Product in
@@ -70,21 +77,21 @@ public class ProductService {
7077

7178
public func updateItem(product: Product) -> EventLoopFuture<Product> {
7279
var product = product
73-
let date = Date().iso8601
74-
product.updatedAt = date
80+
let date = Date()
81+
product.updatedAt = date.iso8601
7582

7683
let input = DynamoDB.UpdateItemInput(
7784
expressionAttributeNames: [
78-
"#name": ProductField.name,
79-
"#description": ProductField.description,
80-
"#updatedAt": ProductField.updatedAt,
85+
"#name": Product.Field.name,
86+
"#description": Product.Field.description,
87+
"#updatedAt": Product.Field.updatedAt,
8188
],
8289
expressionAttributeValues: [
8390
":name": DynamoDB.AttributeValue(s: product.name),
8491
":description": DynamoDB.AttributeValue(s: product.description),
8592
":updatedAt": DynamoDB.AttributeValue(s: product.updatedAt),
8693
],
87-
key: [ProductField.sku: DynamoDB.AttributeValue(s: product.sku)],
94+
key: [Product.Field.sku: DynamoDB.AttributeValue(s: product.sku)],
8895
returnValues: DynamoDB.ReturnValue.allNew,
8996
tableName: tableName,
9097
updateExpression: "SET #name = :name, #description = :description, #updatedAt = :updatedAt"
@@ -96,7 +103,7 @@ public class ProductService {
96103

97104
public func deleteItem(key: String) -> EventLoopFuture<Void> {
98105
let input = DynamoDB.DeleteItemInput(
99-
key: [ProductField.sku: DynamoDB.AttributeValue(s: key)],
106+
key: [Product.Field.sku: DynamoDB.AttributeValue(s: key)],
100107
tableName: tableName
101108
)
102109
return db.deleteItem(input).map { _ in Void() }

0 commit comments

Comments
 (0)