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.Vendors {
.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 VendorModel.find(id, on: db) else {
throw NotFoundError()
@@ -27,7 +27,7 @@ public extension DatabaseClient.Vendors {
break
}
return try await query.all().map { $0.toDTO(includeBranches: withBranches) }
return try await query.all().map { try $0.toDTO(includeBranches: withBranches) }
} get: { id, request in
var query = VendorModel.query(on: db).filter(\.$id == id)
@@ -35,14 +35,14 @@ public extension DatabaseClient.Vendors {
if withBranches {
query = query.with(\.$branches)
}
return try await query.first().map { $0.toDTO(includeBranches: withBranches) }
return try await query.first().map { try $0.toDTO(includeBranches: withBranches) }
} update: { id, updates in
guard let model = try await VendorModel.find(id, on: db) else {
throw NotFoundError()
}
try model.applyUpdates(updates)
try await model.save(on: db)
return model.toDTO()
return try model.toDTO()
}
}
}
@@ -119,12 +119,12 @@ final class VendorModel: Model, @unchecked Sendable {
self.name = name
}
func toDTO(includeBranches: Bool? = nil) -> Vendor {
.init(
id: id,
func toDTO(includeBranches: Bool? = nil) throws -> Vendor {
try .init(
id: requireID(),
name: name,
branches: ($branches.value != nil && $branches.value!.count > 0)
? $branches.value!.map { $0.toDTO() }
? $branches.value!.map { try $0.toDTO() }
: [],
createdAt: createdAt,
updatedAt: updatedAt