59 lines
1.3 KiB
Swift
59 lines
1.3 KiB
Swift
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
|
|
}
|
|
|
|
}
|