|
8 | 8 | import Amplify
|
9 | 9 |
|
10 | 10 | public struct SocialNote: Model {
|
11 |
| - public let id: String |
12 |
| - public var content: String |
13 |
| - public var owner: String? |
14 |
| - public init(id: String = UUID().uuidString, |
15 |
| - content: String, |
16 |
| - owner: String?) { |
17 |
| - self.id = id |
18 |
| - self.content = content |
19 |
| - self.owner = owner |
20 |
| - } |
21 |
| - public enum CodingKeys: String, ModelKey { |
22 |
| - case id |
23 |
| - case content |
24 |
| - case owner |
25 |
| - } |
26 |
| - public static let keys = CodingKeys.self |
27 |
| - |
28 |
| - public static let schema = defineSchema { model in |
29 |
| - let socialNote = SocialNote.keys |
30 |
| - model.authRules = [ |
31 |
| - rule(allow: .owner, operations: [.create, .update, .delete]) |
32 |
| - ] |
33 |
| - model.fields( |
34 |
| - .id(), |
35 |
| - .field(socialNote.content, is: .required, ofType: .string), |
36 |
| - .field(socialNote.owner, is: .optional, ofType: .string)) |
| 11 | + public let id: String |
| 12 | + public var content: String |
| 13 | + public var owner: String? |
| 14 | + |
| 15 | + public init(id: String = UUID().uuidString, |
| 16 | + content: String, |
| 17 | + owner: String? = nil) { |
| 18 | + self.id = id |
| 19 | + self.content = content |
| 20 | + self.owner = owner |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +extension SocialNote { |
| 25 | + // MARK: - CodingKeys |
| 26 | + public enum CodingKeys: String, ModelKey { |
| 27 | + case id |
| 28 | + case content |
| 29 | + case owner |
| 30 | + } |
| 31 | + |
| 32 | + public static let keys = CodingKeys.self |
| 33 | + // MARK: - ModelSchema |
| 34 | + |
| 35 | + public static let schema = defineSchema { model in |
| 36 | + let socialNote = SocialNote.keys |
| 37 | + |
| 38 | + model.authRules = [ |
| 39 | + rule(allow: .owner, ownerField: "owner", identityClaim: "cognito:username", operations: [.create, .update, .delete]) |
| 40 | + ] |
| 41 | + |
| 42 | + model.pluralName = "SocialNotes" |
| 43 | + |
| 44 | + model.fields( |
| 45 | + .id(), |
| 46 | + .field(socialNote.content, is: .required, ofType: .string), |
| 47 | + .field(socialNote.owner, is: .optional, ofType: .string) |
| 48 | + ) |
37 | 49 | }
|
38 | 50 | }
|
0 commit comments