Added the catalogue models and seed to support phase 6 of the POC prototype in the Persistence package.
This commit is contained in:
1 parent
31d2d98d49
commit
256d22df40
10 files changed
+473
-4
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import FluentKit
|
||||
|
||||
/// Creates the `events`, `event_shops`, and `owned_issues` tables, seeded with the demo cons and the shops trading at them.
|
||||
struct CreateCommunity: AsyncMigration {
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
func prepare(on database: Database) async throws {
|
||||
try await database.schema(EventRecord.schema)
|
||||
.id()
|
||||
.field("slug", .string, .required)
|
||||
.field("name", .string, .required)
|
||||
.field("venue", .string, .required)
|
||||
.field("starts_on", .datetime, .required)
|
||||
.field("ends_on", .datetime, .required)
|
||||
.unique(on: "slug")
|
||||
.create()
|
||||
try await database.schema(EventShopRecord.schema)
|
||||
.id()
|
||||
.field("event_id", .uuid, .required, .references(EventRecord.schema, "id", onDelete: .cascade))
|
||||
.field("shop_id", .uuid, .required, .references(ShopRecord.schema, "id", onDelete: .cascade))
|
||||
.unique(on: "event_id", "shop_id")
|
||||
.create()
|
||||
try await database.schema(OwnedIssueRecord.schema)
|
||||
.id()
|
||||
.field("user_id", .uuid, .required, .references(UserRecord.schema, "id", onDelete: .cascade))
|
||||
.field("series_slug", .string, .required)
|
||||
.field("issue", .int, .required)
|
||||
.unique(on: "user_id", "series_slug", "issue")
|
||||
.create()
|
||||
|
||||
for (event, shops) in Seed.events {
|
||||
let record = EventRecord(event)
|
||||
|
||||
try await record.save(on: database)
|
||||
|
||||
for shop in try await ShopRecord.query(on: database).filter(\.$slug ~~ shops).all() {
|
||||
try await record.$shops.attach(shop, on: database)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func revert(on database: Database) async throws {
|
||||
for schema in [OwnedIssueRecord.schema, EventShopRecord.schema, EventRecord.schema] {
|
||||
try await database.schema(schema).delete()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import FluentKit
|
||||
import Foundation
|
||||
|
||||
/// A FluentKit model of a single `events` row: a con where shops trade in person.
|
||||
final class EventRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "events"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "slug")
|
||||
var slug: String
|
||||
|
||||
@Field(key: "name")
|
||||
var name: String
|
||||
|
||||
@Field(key: "venue")
|
||||
var venue: String
|
||||
|
||||
@Field(key: "starts_on")
|
||||
var startsOn: Date
|
||||
|
||||
@Field(key: "ends_on")
|
||||
var endsOn: Date
|
||||
|
||||
@Siblings(through: EventShopRecord.self, from: \.$event, to: \.$shop)
|
||||
var shops: [ShopRecord]
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
_ event: Event
|
||||
) {
|
||||
self.slug = event.slug
|
||||
self.name = event.name
|
||||
self.venue = event.venue
|
||||
self.startsOn = event.startsOn
|
||||
self.endsOn = event.endsOn
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// A FluentKit model of a single `event_shops` row: a shop trading at an event.
|
||||
final class EventShopRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "event_shops"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Parent(key: "event_id")
|
||||
var event: EventRecord
|
||||
|
||||
@Parent(key: "shop_id")
|
||||
var shop: ShopRecord
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
}
|
||||
|
||||
/// A FluentKit model of a single `owned_issues` row: an issue of a series in an account's collection.
|
||||
final class OwnedIssueRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "owned_issues"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Parent(key: "user_id")
|
||||
var user: UserRecord
|
||||
|
||||
@Field(key: "series_slug")
|
||||
var seriesSlug: String
|
||||
|
||||
@Field(key: "issue")
|
||||
var issue: Int
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
userID: UUID,
|
||||
seriesSlug: String,
|
||||
issue: Int
|
||||
) {
|
||||
self.$user.id = userID
|
||||
self.seriesSlug = seriesSlug
|
||||
self.issue = issue
|
||||
}
|
||||
|
||||
}
|
||||
@@ -158,6 +158,31 @@ enum Seed {
|
||||
),
|
||||
]
|
||||
|
||||
/// The demo cons, soonest first, each with the slugs of the shops trading there.
|
||||
static let events: [(event: Event, shops: [String])] = [
|
||||
(.init(slug: "dutch-comic-con", name: "Dutch Comic Con", venue: "Jaarbeurs, Utrecht", startsOn: day(2026, 9, 26), endsOn: day(2026, 9, 27)),
|
||||
["kestrel-row-comics", "silverfish-comics", "third-panel-supply"]),
|
||||
(.init(slug: "stripdagen-haarlem", name: "Stripdagen Haarlem", venue: "Grote Markt, Haarlem", startsOn: day(2026, 10, 3), endsOn: day(2026, 10, 4)),
|
||||
["silverfish-comics"]),
|
||||
(.init(slug: "comic-con-amsterdam", name: "Comic Con Amsterdam", venue: "RAI, Amsterdam", startsOn: day(2026, 11, 21), endsOn: day(2026, 11, 22)),
|
||||
["kestrel-row-comics", "inkwell-and-staple"]),
|
||||
(.init(slug: "comic-con-brussels", name: "Comic Con Brussels", venue: "Tour & Taxis, Brussels", startsOn: day(2026, 12, 5), endsOn: day(2026, 12, 6)),
|
||||
["third-panel-supply"]),
|
||||
]
|
||||
|
||||
/// Noon on a day in Amsterdam, where the demo cons are dated.
|
||||
static func day(
|
||||
_ year: Int,
|
||||
_ month: Int,
|
||||
_ day: Int
|
||||
) -> Date {
|
||||
var calendar = Calendar(identifier: .gregorian)
|
||||
|
||||
calendar.timeZone = TimeZone(identifier: "Europe/Amsterdam") ?? .gmt
|
||||
|
||||
return calendar.date(from: .init(year: year, month: month, day: day, hour: 12)) ?? .distantPast
|
||||
}
|
||||
|
||||
/// The runs the demo listings belong to.
|
||||
static let series: [Series] = [
|
||||
.init(slug: "nightward", name: "Nightward", publisher: "Meridian Comics", years: "2019–2021", issueCount: 24,
|
||||
|
||||
@@ -32,6 +32,7 @@ public struct PrepareDB: Sendable {
|
||||
CreateTrade(),
|
||||
CreatePasskey(),
|
||||
CreateCollecting(),
|
||||
CreateCommunity(),
|
||||
])
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import Foundation
|
||||
|
||||
/// A `Sendable` snapshot of a con where shops trade in person, so a sale agreed on the site can be collected there.
|
||||
public struct Event: Sendable, Equatable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// The event's URL slug.
|
||||
public let slug: String
|
||||
/// The event's name.
|
||||
public let name: String
|
||||
/// Where it is held, e.g. `Jaarbeurs, Utrecht`.
|
||||
public let venue: String
|
||||
/// The first day.
|
||||
public let startsOn: Date
|
||||
/// The last day.
|
||||
public let endsOn: Date
|
||||
/// The shops trading there.
|
||||
public let shops: [Shop]
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
public init(
|
||||
slug: String,
|
||||
name: String,
|
||||
venue: String,
|
||||
startsOn: Date,
|
||||
endsOn: Date,
|
||||
shops: [Shop] = []
|
||||
) {
|
||||
self.slug = slug
|
||||
self.name = name
|
||||
self.venue = venue
|
||||
self.startsOn = startsOn
|
||||
self.endsOn = endsOn
|
||||
self.shops = shops
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,6 +31,8 @@ public struct ListingFilter: Sendable, Equatable {
|
||||
public var verifiedShops: Bool
|
||||
/// The slug of the one shop to match, or `nil` for all shops.
|
||||
public var shop: String?
|
||||
/// The slugs of the shops to match — those trading at an event, say — or `nil` for all shops.
|
||||
public var shops: [String]?
|
||||
/// The order of the results.
|
||||
public var sort: Sort
|
||||
|
||||
@@ -51,6 +53,7 @@ public struct ListingFilter: Sendable, Equatable {
|
||||
acceptsOffers: Bool = false,
|
||||
verifiedShops: Bool = false,
|
||||
shop: String? = nil,
|
||||
shops: [String]? = nil,
|
||||
sort: Sort = .newest
|
||||
) {
|
||||
self.query = query
|
||||
@@ -67,6 +70,7 @@ public struct ListingFilter: Sendable, Equatable {
|
||||
self.acceptsOffers = acceptsOffers
|
||||
self.verifiedShops = verifiedShops
|
||||
self.shop = shop
|
||||
self.shops = shops
|
||||
self.sort = sort
|
||||
}
|
||||
|
||||
@@ -117,6 +121,10 @@ extension ListingFilter {
|
||||
return false
|
||||
}
|
||||
|
||||
if let shops, !shops.contains(listing.shop.slug) {
|
||||
return false
|
||||
}
|
||||
|
||||
if !formats.isEmpty, !formats.contains(listing.format) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -133,6 +133,27 @@ public struct AccountRepository: Sendable {
|
||||
).save(on: fluent.db())
|
||||
}
|
||||
|
||||
/// Replaces every passkey of an account with a new one: what recovering an account comes down to.
|
||||
public func replacePasskeys(
|
||||
of user: User,
|
||||
credentialID: String,
|
||||
publicKey: [UInt8],
|
||||
signCount: UInt32
|
||||
) async throws {
|
||||
try await fluent.db().transaction { database in
|
||||
try await PasskeyRecord.query(on: database)
|
||||
.filter(\.$user.$id == user.id)
|
||||
.delete()
|
||||
try await PasskeyRecord(
|
||||
userID: user.id,
|
||||
credentialID: credentialID,
|
||||
publicKey: publicKey,
|
||||
signCount: signCount,
|
||||
at: .init()
|
||||
).save(on: database)
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches the passkeys registered to an account, the oldest first, with when each was created.
|
||||
public func passkeys(
|
||||
of user: User
|
||||
|
||||
@@ -192,3 +192,105 @@ public extension CollectingRepository {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Collection
|
||||
|
||||
public extension CollectingRepository {
|
||||
|
||||
/// Adds issues of a series to an account's collection; the ones already in it, or outside the run, are skipped.
|
||||
func own(
|
||||
issues: [Int],
|
||||
ofSeries slug: String,
|
||||
by user: User
|
||||
) async throws {
|
||||
guard let series = try await series(slug: slug) else {
|
||||
return
|
||||
}
|
||||
|
||||
let owned = try await ownedIssues(of: user)[slug] ?? []
|
||||
let run = series.issueCount > 0 ? 1...series.issueCount : 1...999
|
||||
|
||||
for issue in Set(issues).subtracting(owned) where run.contains(issue) {
|
||||
try await OwnedIssueRecord(userID: user.id, seriesSlug: slug, issue: issue).save(on: fluent.db())
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches the issues an account owns, by series slug.
|
||||
func ownedIssues(
|
||||
of user: User
|
||||
) async throws -> [String: Set<Int>] {
|
||||
try await OwnedIssueRecord.query(on: fluent.db())
|
||||
.filter(\.$user.$id == user.id)
|
||||
.all()
|
||||
.reduce(into: [:]) { $0[$1.seriesSlug, default: []].insert($1.issue) }
|
||||
}
|
||||
|
||||
/// Takes an issue out of an account's collection.
|
||||
func disown(
|
||||
issue: Int,
|
||||
ofSeries slug: String,
|
||||
by user: User
|
||||
) async throws {
|
||||
try await OwnedIssueRecord.query(on: fluent.db())
|
||||
.filter(\.$user.$id == user.id)
|
||||
.filter(\.$seriesSlug == slug)
|
||||
.filter(\.$issue == issue)
|
||||
.delete()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Events
|
||||
|
||||
public extension CollectingRepository {
|
||||
|
||||
/// Fetches the events still to come or under way, soonest first, each with the shops trading there.
|
||||
func upcomingEvents() async throws -> [Event] {
|
||||
try await EventRecord.query(on: fluent.db())
|
||||
.filter(\.$endsOn >= now().addingTimeInterval(-86_400))
|
||||
.sort(\.$startsOn)
|
||||
.with(\.$shops)
|
||||
.all()
|
||||
.map { record in
|
||||
.init(
|
||||
slug: record.slug,
|
||||
name: record.name,
|
||||
venue: record.venue,
|
||||
startsOn: record.startsOn,
|
||||
endsOn: record.endsOn,
|
||||
shops: record.shops.map(\.snapshot)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the upcoming events the shop an account owns trades at.
|
||||
func setEvents(
|
||||
_ slugs: [String],
|
||||
forShopOwnedBy user: User
|
||||
) async throws {
|
||||
try await fluent.db().transaction { database in
|
||||
guard let shop = try await ShopRecord.query(on: database)
|
||||
.filter(\.$owner.$id == user.id)
|
||||
.first()
|
||||
else {
|
||||
return
|
||||
}
|
||||
|
||||
let upcoming = try await EventRecord.query(on: database)
|
||||
.filter(\.$endsOn >= now().addingTimeInterval(-86_400))
|
||||
.with(\.$shops)
|
||||
.all()
|
||||
|
||||
for event in upcoming {
|
||||
let trades = try event.shops.contains { try $0.requireID() == shop.requireID() }
|
||||
|
||||
if slugs.contains(event.slug), !trades {
|
||||
try await event.$shops.attach(shop, on: database)
|
||||
} else if !slugs.contains(event.slug), trades {
|
||||
try await event.$shops.detach(shop, on: database)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user