Skip to content

Commit 2989325

Browse files
committed
Update the collection if details have changed
1 parent 1fecfb0 commit 2989325

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Sources/App/Models/CustomCollection.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ extension CustomCollection {
8383
if let collection = try await CustomCollection.query(on: database)
8484
.filter(\.$key == details.key)
8585
.first() {
86+
if collection.details != details {
87+
// Update the collection if any of the details have changed
88+
collection.details = details
89+
try await collection.update(on: database)
90+
}
8691
return collection
8792
} else {
8893
let collection = CustomCollection(details)
@@ -106,7 +111,20 @@ extension CustomCollection {
106111
}
107112

108113
var details: Details {
109-
.init(key: key, name: name, description: description, badge: badge, url: url)
114+
get {
115+
.init(key: key,
116+
name: name,
117+
description: description,
118+
badge: badge,
119+
url: url)
120+
}
121+
set {
122+
key = newValue.key
123+
name = newValue.name
124+
description = newValue.description
125+
badge = newValue.badge
126+
url = newValue.url
127+
}
110128
}
111129
}
112130

Tests/AppTests/CustomCollectionTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ class CustomCollectionTests: AppTestCase {
107107
XCTAssertEqual(c.first?.name, "List")
108108
XCTAssertEqual(c.first?.url, "url")
109109
}
110+
111+
do { // a record is updated if data has changed
112+
// MUT
113+
let res = try await CustomCollection.findOrCreate(on: app.db, .init(key: "list",
114+
name: "New name",
115+
url: "new-url"))
116+
// validate
117+
XCTAssertEqual(res.key, "list")
118+
XCTAssertEqual(res.name, "New name")
119+
XCTAssertEqual(res.url, "new-url")
120+
121+
let c = try await CustomCollection.query(on: app.db).all()
122+
XCTAssertEqual(c.count, 1)
123+
XCTAssertEqual(c.first?.key, "list")
124+
XCTAssertEqual(c.first?.name, "New name")
125+
XCTAssertEqual(c.first?.url, "new-url")
126+
}
110127
}
111128

112129
func test_CustomCollectionPackage_attach() async throws {

0 commit comments

Comments
 (0)