feat: Working on hummingbird app

This commit is contained in:
2025-01-14 07:51:13 -05:00
parent 6225c32007
commit 4f47f1aed8
10 changed files with 223 additions and 86 deletions

View File

@@ -9,7 +9,7 @@ public extension DatabaseClient.VendorBranches {
.init { create in
let model = try create.toModel()
try await model.save(on: db)
return model.toDTO()
return try model.toDTO()
} delete: { id in
guard let model = try await VendorBranchModel.find(id, on: db) else {
throw NotFoundError()
@@ -29,22 +29,22 @@ public extension DatabaseClient.VendorBranches {
.with(\.$branches)
.first()?
.branches
.map { $0.toDTO() }
.map { try $0.toDTO() }
guard let branches else { throw NotFoundError() }
return branches
}
return try await query.all().map { $0.toDTO() }
return try await query.all().map { try $0.toDTO() }
} get: { id in
try await VendorBranchModel.find(id, on: db).map { $0.toDTO() }
try await VendorBranchModel.find(id, on: db).map { try $0.toDTO() }
} update: { id, updates in
guard let model = try await VendorBranchModel.find(id, on: db) else {
throw NotFoundError()
}
try model.applyUpdates(updates)
try await model.save(on: db)
return model.toDTO()
return try model.toDTO()
}
}
@@ -53,16 +53,19 @@ public extension DatabaseClient.VendorBranches {
extension VendorBranch {
struct Migrate: AsyncMigration {
let name = "CreateVendorBranch"
fileprivate typealias FieldKey = VendorBranch.Key.Field
func prepare(on database: any Database) async throws {
try await database.schema(VendorBranchModel.schema)
.id()
.field("name", .string, .required)
.field("vendor_id", .uuid, .required)
.field("created_at", .datetime)
.field("updated_at", .datetime)
.foreignKey("vendor_id", references: VendorModel.schema, "id", onDelete: .cascade)
.field(.string(FieldKey.name), .string, .required)
.field(.string(FieldKey.vendorID), .uuid, .required)
.field(.string(FieldKey.createdAt), .datetime)
.field(.string(FieldKey.updatedAt), .datetime)
.foreignKey(.string(FieldKey.vendorID), references: VendorModel.schema, "id", onDelete: .cascade)
.create()
}
@@ -96,23 +99,36 @@ extension VendorBranch.Update {
}
}
private extension VendorBranch {
enum Key {
static let schema = "vendor_branch"
enum Field {
static let createdAt = "created_at"
static let name = "name"
static let updatedAt = "updated_at"
static let vendorID = "vendor_id"
}
}
}
final class VendorBranchModel: Model, @unchecked Sendable {
static let schema = "vendor_branch"
fileprivate typealias FieldKey = VendorBranch.Key.Field
static let schema = VendorBranch.Key.schema
@ID(key: .id)
var id: UUID?
@Field(key: "name")
@Field(key: .string(FieldKey.name))
var name: String
@Timestamp(key: "created_at", on: .create)
@Timestamp(key: .string(FieldKey.createdAt), on: .create)
var createdAt: Date?
@Timestamp(key: "updated_at", on: .update)
@Timestamp(key: .string(FieldKey.updatedAt), on: .update)
var updatedAt: Date?
@Parent(key: "vendor_id")
@Parent(key: .string(FieldKey.vendorID))
var vendor: VendorModel
init() {}
@@ -123,9 +139,9 @@ final class VendorBranchModel: Model, @unchecked Sendable {
$vendor.id = vendorId
}
func toDTO() -> VendorBranch {
.init(
id: id,
func toDTO() throws -> VendorBranch {
try .init(
id: requireID(),
name: name,
vendorID: $vendor.id,
createdAt: createdAt,