Skip to content

Commit 729f4e1

Browse files
committed
Update CustomCollectionsController to use key instead of name
1 parent 3ad1dd1 commit 729f4e1

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

Sources/App/Controllers/CustomCollectionsController.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ enum CustomCollectionsController {
6060
throw Abort(.notFound)
6161
}
6262
let query = try req.query.decode(Query.self)
63-
let collection = try await CustomCollection.query(on: req.db)
64-
.filter(\.$key == key)
65-
.first()
63+
let collection = try await CustomCollection.find(on: req.db, key: key)
6664
.unwrap(or: Abort(.notFound))
6765
let page = try await Self.query(on: req.db, key: key, page: query.page, pageSize: query.pageSize)
6866

Sources/App/Controllers/PackageCollectionController.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ enum PackageCollectionController {
3232
filterBy: .author(owner),
3333
authorName: "\(owner) via the Swift Package Index"
3434
)
35-
case let .custom(name):
35+
case let .custom(key):
36+
let collection = try await CustomCollection.find(on: req.db, key: key)
37+
.unwrap(or: Abort(.notFound))
3638
return try await SignedCollection.generate(
3739
db: req.db,
38-
filterBy: .customCollection(name),
40+
filterBy: .customCollection(key),
3941
authorName: "Swift Package Index",
40-
collectionName: name,
42+
collectionName: collection.name,
4143
overview: "A custom package collection generated by the Swift Package Index"
4244
)
4345
}
@@ -53,7 +55,7 @@ enum PackageCollectionController {
5355

5456
static func getCollectionType(req: Request) -> CollectionType? {
5557
if let owner = req.parameters.get("owner") { return .author(owner) }
56-
if let name = req.parameters.get("name") { return .custom(name) }
58+
if let key = req.parameters.get("key") { return .custom(key) }
5759
return nil
5860
}
5961
}

Sources/App/Core/PackageCollection+VersionResult.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ extension PackageCollection.VersionResult {
5555
switch filter {
5656
case let .author(owner):
5757
query.filter(Repository.self, \Repository.$owner, .custom("ilike"), owner)
58-
case let .customCollection(name):
58+
case let .customCollection(key):
5959
query
6060
.join(CustomCollectionPackage.self, on: \Package.$id == \CustomCollectionPackage.$package.$id)
6161
.join(CustomCollection.self, on: \CustomCollection.$id == \CustomCollectionPackage.$customCollection.$id)
62-
.filter(CustomCollection.self, \.$name == name)
62+
.filter(CustomCollection.self, \.$key == key)
6363
case let .urls(packageURLs):
6464
query.filter(App.Package.self, \.$url ~~ packageURLs)
6565
}

Sources/App/Models/CustomCollection.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ extension CustomCollection {
7979
var url: URL
8080
}
8181

82+
static func find(on database: Database, key: String) async throws -> CustomCollection? {
83+
try await CustomCollection.query(on: database)
84+
.filter(\.$key == key)
85+
.first()
86+
}
87+
8288
static func findOrCreate(on database: Database, _ details: Details) async throws -> CustomCollection {
83-
if let collection = try await CustomCollection.query(on: database)
84-
.filter(\.$key == details.key)
85-
.first() {
89+
if let collection = try await CustomCollection.find(on: database, key: details.key) {
8690
if collection.details != details {
8791
// Update the collection if any of the details have changed
8892
collection.details = details

Tests/AppTests/PackageCollectionControllerTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class PackageCollectionControllerTests: AppTestCase {
122122
let encoder = self.encoder
123123
try await app.test(
124124
.GET,
125-
"collections/Custom%20Collection/collection.json",
125+
"collections/custom-collection/collection.json",
126126
afterResponse: { @MainActor res async throws in
127127
// validation
128128
XCTAssertEqual(res.status, .ok)

0 commit comments

Comments
 (0)