Added the catalogue models and seed to support phase 1 of the POC prototype in the Persistence package.
This commit is contained in:
1 parent
717df5208c
commit
6d7ab4bcba
19 files changed
+1381
-151
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import FluentKit
|
||||
import HummingbirdFluent
|
||||
|
||||
/// Reads the shops and their listings through the default database.
|
||||
public struct CatalogueRepository: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private let fluent: Fluent
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Creates a repository backed by the given `Fluent` service.
|
||||
/// - Parameter fluent: the service whose default database the repository operates on.
|
||||
public init(
|
||||
fluent: Fluent
|
||||
) {
|
||||
self.fluent = fluent
|
||||
}
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// Fetches the listing with the given slug.
|
||||
/// - Parameter slug: the listing's URL slug.
|
||||
/// - Returns: the listing, or `nil` when none has the slug.
|
||||
public func listing(
|
||||
slug: String
|
||||
) async throws -> Listing? {
|
||||
try await ListingRecord.query(on: fluent.db())
|
||||
.filter(\.$slug == slug)
|
||||
.with(\.$shop)
|
||||
.first()?
|
||||
.snapshot
|
||||
}
|
||||
|
||||
/// Fetches the listings meeting the filter, in its order.
|
||||
///
|
||||
/// The prototype filters in memory, which its few dozen seeded listings allow; the criteria move into the query with real data.
|
||||
/// - Parameter filter: the criteria the listings are narrowed and ordered by.
|
||||
/// - Returns: every matching listing.
|
||||
public func listings(
|
||||
matching filter: ListingFilter = .init()
|
||||
) async throws -> [Listing] {
|
||||
try await ListingRecord.query(on: fluent.db())
|
||||
.with(\.$shop)
|
||||
.all()
|
||||
.map(\.snapshot)
|
||||
.filter(filter.matches)
|
||||
.sorted(by: filter.ordersBefore)
|
||||
}
|
||||
|
||||
/// Fetches the name of every publisher with a listing, in alphabetical order.
|
||||
public func publishers() async throws -> [String] {
|
||||
try await ListingRecord.query(on: fluent.db())
|
||||
.unique()
|
||||
.sort(\.$publisher)
|
||||
.all(\.$publisher)
|
||||
}
|
||||
|
||||
/// Fetches the shop with the given slug.
|
||||
/// - Parameter slug: the shop's URL slug.
|
||||
/// - Returns: the shop, or `nil` when none has the slug.
|
||||
public func shop(
|
||||
slug: String
|
||||
) async throws -> Shop? {
|
||||
try await ShopRecord.query(on: fluent.db())
|
||||
.filter(\.$slug == slug)
|
||||
.first()?
|
||||
.snapshot
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user