feat: Reorganizes files.

This commit is contained in:
2025-01-10 14:03:52 -05:00
parent 280bc31a03
commit 455287fe1c
10 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
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)
}
}