import Fluent import Vapor struct VendorDB { func create(_ model: Vendor.Create, on db: any Database) async throws -> Vendor.DTO { let model = model.toModel() try await model.save(on: db) return model.toDTO() } func fetchAll(withBranches: Bool? = nil, on db: any Database) async throws -> [Vendor.DTO] { var query = Vendor.query(on: db).sort(\.$name, .ascending) if withBranches == true { query = query.with(\.$branches) } return try await query.all().map { $0.toDTO(includeBranches: withBranches) } } func get(id: Vendor.IDValue, withBranches: Bool? = nil, on db: any Database) async throws -> Vendor.DTO? { var query = Vendor.query(on: db).filter(\.$id == id) if withBranches == true { query = query.with(\.$branches) } return try await query.first().map { $0.toDTO(includeBranches: withBranches) } } func update( id: Vendor.IDValue, with updates: Vendor.Update, on db: any Database ) async throws -> Vendor.DTO { guard let vendor = try await Vendor.find(id, on: db) else { throw Abort(.notFound) } vendor.applyUpdates(updates) return vendor.toDTO() } func delete(id: Vendor.IDValue, on db: any Database) async throws { guard let vendor = try await Vendor.find(id, on: db) else { throw Abort(.notFound) } try await vendor.delete(on: db) } }