Added the catalogue models and seed to support phase 5 of the POC prototype in the Persistence package.

This commit is contained in:
javier committed 2026-09-21 13:31:44 +02:00
1 parent 9403931109
commit 89a7a7b8e5
19 files changed
+990 -15

No files matched your search

@@ -0,0 +1,30 @@
import FluentKit
/// Adds the seller kind and the business registration to shops.
///
/// Registered ahead of ``SeedCatalogue``, whose model writes the columns. One column per update: SQLite adds no more in a statement.
struct AddShopRegistration: AsyncMigration {
// MARK: Constants
private static let columns = ["kind", "kvk_number", "vat_id", "legal_name"]
// MARK: Methods
func prepare(on database: Database) async throws {
for column in Self.columns {
try await database.schema(ShopRecord.schema)
.field(.string(column), .string)
.update()
}
}
func revert(on database: Database) async throws {
for column in Self.columns.reversed() {
try await database.schema(ShopRecord.schema)
.deleteField(.string(column))
.update()
}
}
}
@@ -0,0 +1,41 @@
import FluentKit
/// Creates the `wants` table, and the `series` table seeded with the runs the demo listings belong to.
struct CreateCollecting: AsyncMigration {
// MARK: Methods
func prepare(on database: Database) async throws {
try await database.schema(WantRecord.schema)
.id()
.field("user_id", .uuid, .required, .references(UserRecord.schema, "id", onDelete: .cascade))
.field("title", .string, .required)
.field("min_grade", .double)
.field("max_price_cents", .int)
.field("slabbed_only", .bool, .required)
.field("alerts", .bool, .required)
.field("created_at", .datetime, .required)
.field("seen_at", .datetime, .required)
.create()
try await database.schema(SeriesRecord.schema)
.id()
.field("slug", .string, .required)
.field("name", .string, .required)
.field("publisher", .string, .required)
.field("years", .string, .required)
.field("issue_count", .int, .required)
.field("blurb", .string, .required)
.unique(on: "slug")
.create()
for series in Seed.series {
try await SeriesRecord(series).save(on: database)
}
}
func revert(on database: Database) async throws {
try await database.schema(SeriesRecord.schema).delete()
try await database.schema(WantRecord.schema).delete()
}
}
@@ -0,0 +1,67 @@
import FluentKit
import Foundation
/// A FluentKit model of a single `series` row: a run of issues in the catalogue.
final class SeriesRecord: Model, @unchecked Sendable {
// MARK: Properties
static let schema = "series"
@ID(key: .id)
var id: UUID?
@Field(key: "slug")
var slug: String
@Field(key: "name")
var name: String
@Field(key: "publisher")
var publisher: String
@Field(key: "years")
var years: String
@Field(key: "issue_count")
var issueCount: Int
@Field(key: "blurb")
var blurb: String
// MARK: Initializers
init() {}
init(
_ series: Series
) {
self.slug = series.slug
self.name = series.name
self.publisher = series.publisher
self.years = series.years
self.issueCount = series.issueCount
self.blurb = series.blurb
}
}
// MARK: - Extensions
extension SeriesRecord {
// MARK: Computed
/// The `Sendable` snapshot of the record.
var snapshot: Series {
.init(
slug: slug,
name: name,
publisher: publisher,
years: years,
issueCount: issueCount,
blurb: blurb
)
}
}
@@ -53,6 +53,19 @@ final class ShopRecord: Model, @unchecked Sendable {
@OptionalParent(key: "owner_id")
var owner: UserRecord?
/// The seller kind token; `nil` reads as a business, which every seeded shop is.
@OptionalField(key: "kind")
var kind: String?
@OptionalField(key: "kvk_number")
var kvkNumber: String?
@OptionalField(key: "vat_id")
var vatID: String?
@OptionalField(key: "legal_name")
var legalName: String?
@Children(for: \.$shop)
var listings: [ListingRecord]
@@ -78,6 +91,10 @@ final class ShopRecord: Model, @unchecked Sendable {
self.shipsNote = shop.shipsNote
self.returnsNote = shop.returnsNote
self.about = shop.about
self.kind = shop.kind.rawValue
self.kvkNumber = shop.kvkNumber
self.vatID = shop.vatID
self.legalName = shop.legalName
}
}
@@ -103,7 +120,11 @@ extension ShopRecord {
handlingDays: handlingDays,
shipsNote: shipsNote,
returnsNote: returnsNote,
about: about
about: about,
kind: kind.flatMap(Shop.Kind.init) ?? .business,
kvkNumber: kvkNumber,
vatID: vatID,
legalName: legalName
)
}
@@ -0,0 +1,43 @@
import FluentKit
import Foundation
/// A FluentKit model of a single `wants` row: a copy an account is looking for.
final class WantRecord: Model, @unchecked Sendable {
// MARK: Properties
static let schema = "wants"
@ID(key: .id)
var id: UUID?
@Parent(key: "user_id")
var user: UserRecord
@Field(key: "title")
var title: String
@OptionalField(key: "min_grade")
var minGrade: Double?
@OptionalField(key: "max_price_cents")
var maxPriceCents: Int?
@Field(key: "slabbed_only")
var slabbedOnly: Bool
@Field(key: "alerts")
var alerts: Bool
@Field(key: "created_at")
var createdAt: Date
/// When the account last looked at the want; matches listed later are new to them.
@Field(key: "seen_at")
var seenAt: Date
// MARK: Initializers
init() {}
}
@@ -158,6 +158,20 @@ enum Seed {
),
]
/// The runs the demo listings belong to.
static let series: [Series] = [
.init(slug: "nightward", name: "Nightward", publisher: "Meridian Comics", years: "2019–2021", issueCount: 24,
blurb: "Two-year run, one printing for most issues. #1 has a second printing with a red logo; the hardcover collects #1–12."),
.init(slug: "ferrous-saint", name: "Ferrous Saint", publisher: "Blackline", years: "1993–1996", issueCount: 30,
blurb: "A nineties independent with direct and newsstand editions throughout. The omnibus collects the whole run."),
.init(slug: "echo-country", name: "Echo Country", publisher: "Harbour & Vale", years: "2021–2022", issueCount: 6,
blurb: "A six-issue limited series. Later printings carry recoloured covers."),
.init(slug: "vermilion-lake", name: "Vermilion Lake", publisher: "Meridian Comics", years: "1986–1989", issueCount: 18,
blurb: "Meridian's late-eighties flagship. #7 tells the origin of the Lake Warden."),
.init(slug: "paper-tigers", name: "Paper Tigers", publisher: "Cutwater Press", years: "2023–", issueCount: 4,
blurb: "Ongoing. Every issue ships with a 1:10 retailer variant."),
]
/// The demo shops.
static let shops: [Shop] = [kestrelRow, thirdPanel, longbox, silverfish, inkwell]