Added the catalogue models and seed to support phase 3 of the POC prototype in the Persistence package.
This commit is contained in:
1 parent
43dcc03da6
commit
b1ceed7790
20 files changed
+2035
-8
No files matched your search
@@ -0,0 +1,61 @@
|
||||
import FluentKit
|
||||
import Foundation
|
||||
|
||||
/// A FluentKit model of a single `conversations` row: a buyer and a shop, talking about a listing or the shop at large.
|
||||
final class ConversationRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "conversations"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Parent(key: "buyer_id")
|
||||
var buyer: UserRecord
|
||||
|
||||
@Parent(key: "shop_id")
|
||||
var shop: ShopRecord
|
||||
|
||||
@OptionalParent(key: "listing_id")
|
||||
var listing: ListingRecord?
|
||||
|
||||
@Field(key: "created_at")
|
||||
var createdAt: Date
|
||||
|
||||
/// When the buyer last opened the conversation; what came later is unread to them.
|
||||
@Field(key: "buyer_read_at")
|
||||
var buyerReadAt: Date
|
||||
|
||||
/// When the seller last opened the conversation; what came later is unread to them.
|
||||
@Field(key: "seller_read_at")
|
||||
var sellerReadAt: Date
|
||||
|
||||
@Children(for: \.$conversation)
|
||||
var messages: [MessageRecord]
|
||||
|
||||
@Children(for: \.$conversation)
|
||||
var offers: [OfferRecord]
|
||||
|
||||
@Children(for: \.$conversation)
|
||||
var sales: [SaleRecord]
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
buyerID: UUID,
|
||||
shopID: UUID,
|
||||
listingID: UUID?,
|
||||
at date: Date
|
||||
) {
|
||||
self.$buyer.id = buyerID
|
||||
self.$shop.id = shopID
|
||||
self.$listing.id = listingID
|
||||
self.createdAt = date
|
||||
self.buyerReadAt = date
|
||||
self.sellerReadAt = date
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,6 +91,9 @@ final class ListingRecord: Model, @unchecked Sendable {
|
||||
@Field(key: "listed_at")
|
||||
var listedAt: Date
|
||||
|
||||
@OptionalField(key: "sold_at")
|
||||
var soldAt: Date?
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
@@ -169,7 +172,8 @@ extension ListingRecord {
|
||||
imprint: coverImprint,
|
||||
number: coverNumber
|
||||
),
|
||||
listedAt: listedAt
|
||||
listedAt: listedAt,
|
||||
soldAt: soldAt
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
import FluentKit
|
||||
import Foundation
|
||||
|
||||
/// A FluentKit model of a single `messages` row: something said, an offer made, or an event stamped into a conversation.
|
||||
final class MessageRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "messages"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Parent(key: "conversation_id")
|
||||
var conversation: ConversationRecord
|
||||
|
||||
/// Who wrote the message, made the offer, or caused the event.
|
||||
@Parent(key: "sender_id")
|
||||
var sender: UserRecord
|
||||
|
||||
@Field(key: "kind")
|
||||
var kind: String
|
||||
|
||||
@Field(key: "body")
|
||||
var body: String
|
||||
|
||||
@OptionalParent(key: "offer_id")
|
||||
var offer: OfferRecord?
|
||||
|
||||
@OptionalField(key: "sale_number")
|
||||
var saleNumber: Int?
|
||||
|
||||
@Field(key: "created_at")
|
||||
var createdAt: Date
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
conversationID: UUID,
|
||||
senderID: UUID,
|
||||
kind: Conversation.Entry.Kind,
|
||||
body: String,
|
||||
offerID: UUID? = nil,
|
||||
saleNumber: Int? = nil,
|
||||
at date: Date
|
||||
) {
|
||||
self.$conversation.id = conversationID
|
||||
self.$sender.id = senderID
|
||||
self.kind = kind.rawValue
|
||||
self.body = body
|
||||
self.$offer.id = offerID
|
||||
self.saleNumber = saleNumber
|
||||
self.createdAt = date
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
import FluentKit
|
||||
import Foundation
|
||||
|
||||
/// A FluentKit model of a single `offers` row.
|
||||
final class OfferRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "offers"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Parent(key: "conversation_id")
|
||||
var conversation: ConversationRecord
|
||||
|
||||
@Parent(key: "listing_id")
|
||||
var listing: ListingRecord
|
||||
|
||||
@Parent(key: "buyer_id")
|
||||
var buyer: UserRecord
|
||||
|
||||
@Field(key: "amount_cents")
|
||||
var amountCents: Int
|
||||
|
||||
@Field(key: "status")
|
||||
var status: String
|
||||
|
||||
@Field(key: "created_at")
|
||||
var createdAt: Date
|
||||
|
||||
@Field(key: "expires_at")
|
||||
var expiresAt: Date
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
conversationID: UUID,
|
||||
listingID: UUID,
|
||||
buyerID: UUID,
|
||||
amountCents: Int,
|
||||
at date: Date
|
||||
) {
|
||||
self.$conversation.id = conversationID
|
||||
self.$listing.id = listingID
|
||||
self.$buyer.id = buyerID
|
||||
self.amountCents = amountCents
|
||||
self.status = Offer.Status.open.rawValue
|
||||
self.createdAt = date
|
||||
self.expiresAt = date.addingTimeInterval(Offer.lifetime)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
extension OfferRecord {
|
||||
|
||||
// MARK: Methods
|
||||
|
||||
/// The `Sendable` snapshot of the saved record; its buyer, and its listing with its shop, must be eager-loaded.
|
||||
/// - Parameter now: the moment an open offer's expiry is judged at.
|
||||
func snapshot(
|
||||
at now: Date
|
||||
) throws -> Offer {
|
||||
let stored = Offer.Status(rawValue: status) ?? .declined
|
||||
|
||||
return .init(
|
||||
id: try requireID(),
|
||||
conversationID: $conversation.id,
|
||||
listing: listing.snapshot,
|
||||
buyer: try buyer.snapshot,
|
||||
amountCents: amountCents,
|
||||
status: stored == .open && expiresAt <= now ? .expired : stored,
|
||||
createdAt: createdAt,
|
||||
expiresAt: expiresAt
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
import FluentKit
|
||||
import Foundation
|
||||
|
||||
/// A FluentKit model of a single `sales` row: the record of an agreement and of its handshake.
|
||||
final class SaleRecord: Model, @unchecked Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
static let schema = "sales"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "number")
|
||||
var number: Int
|
||||
|
||||
@Parent(key: "conversation_id")
|
||||
var conversation: ConversationRecord
|
||||
|
||||
@Parent(key: "listing_id")
|
||||
var listing: ListingRecord
|
||||
|
||||
@Parent(key: "buyer_id")
|
||||
var buyer: UserRecord
|
||||
|
||||
@Parent(key: "seller_id")
|
||||
var seller: UserRecord
|
||||
|
||||
@Field(key: "price_cents")
|
||||
var priceCents: Int
|
||||
|
||||
@Field(key: "asked_cents")
|
||||
var askedCents: Int
|
||||
|
||||
@Field(key: "shipping_cents")
|
||||
var shippingCents: Int
|
||||
|
||||
@Field(key: "declared_grade")
|
||||
var declaredGrade: String
|
||||
|
||||
@OptionalField(key: "declared_page_quality")
|
||||
var declaredPageQuality: String?
|
||||
|
||||
@Field(key: "declared_printing")
|
||||
var declaredPrinting: String
|
||||
|
||||
@Field(key: "declared_notes")
|
||||
var declaredNotes: String
|
||||
|
||||
@Field(key: "agreed_at")
|
||||
var agreedAt: Date
|
||||
|
||||
@OptionalField(key: "paid_at")
|
||||
var paidAt: Date?
|
||||
|
||||
@OptionalField(key: "payment_note")
|
||||
var paymentNote: String?
|
||||
|
||||
@OptionalField(key: "sent_at")
|
||||
var sentAt: Date?
|
||||
|
||||
@OptionalField(key: "shipping_note")
|
||||
var shippingNote: String?
|
||||
|
||||
@OptionalField(key: "received_at")
|
||||
var receivedAt: Date?
|
||||
|
||||
@OptionalField(key: "buyer_rating")
|
||||
var buyerRating: Int?
|
||||
|
||||
@OptionalField(key: "seller_rating")
|
||||
var sellerRating: Int?
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
init() {}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
extension SaleRecord {
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The `Sendable` snapshot of the saved record; its buyer, its seller, and its listing with its shop must be eager-loaded.
|
||||
var snapshot: Sale {
|
||||
get throws {
|
||||
.init(
|
||||
number: number,
|
||||
conversationID: $conversation.id,
|
||||
listing: listing.snapshot,
|
||||
buyer: try buyer.snapshot,
|
||||
seller: try seller.snapshot,
|
||||
priceCents: priceCents,
|
||||
askedCents: askedCents,
|
||||
shippingCents: shippingCents,
|
||||
declared: .init(
|
||||
grade: declaredGrade,
|
||||
pageQuality: declaredPageQuality,
|
||||
printing: declaredPrinting,
|
||||
notes: declaredNotes
|
||||
),
|
||||
agreedAt: agreedAt,
|
||||
paidAt: paidAt,
|
||||
paymentNote: paymentNote,
|
||||
sentAt: sentAt,
|
||||
shippingNote: shippingNote,
|
||||
receivedAt: receivedAt,
|
||||
buyerRating: buyerRating,
|
||||
sellerRating: sellerRating
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,6 +50,9 @@ final class ShopRecord: Model, @unchecked Sendable {
|
||||
@Field(key: "about")
|
||||
var about: String
|
||||
|
||||
@OptionalParent(key: "owner_id")
|
||||
var owner: UserRecord?
|
||||
|
||||
@Children(for: \.$shop)
|
||||
var listings: [ListingRecord]
|
||||
|
||||
|
||||
Reference in new issue
Block a user