feat: Initial working api routes.
This commit is contained in:
102
Sources/App/Models/VendorBranch.swift
Normal file
102
Sources/App/Models/VendorBranch.swift
Normal file
@@ -0,0 +1,102 @@
|
||||
import Fluent
|
||||
import struct Foundation.UUID
|
||||
import Vapor
|
||||
|
||||
final class VendorBranch: Model, @unchecked Sendable {
|
||||
|
||||
static let schema = "vendor_branch"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "name")
|
||||
var name: String
|
||||
|
||||
@Parent(key: "vendor_id")
|
||||
var vendor: Vendor
|
||||
|
||||
init() {}
|
||||
|
||||
init(id: UUID? = nil, name: String, vendorId: Vendor.IDValue) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
$vendor.id = vendorId
|
||||
}
|
||||
|
||||
func toDTO() -> DTO {
|
||||
.init(id: id, name: $name.value, vendorId: $vendor.id)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Update) {
|
||||
name = updates.name
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
extension VendorBranch {
|
||||
struct Create: Content {
|
||||
var name: String
|
||||
|
||||
func toModel() -> VendorBranch {
|
||||
let model = VendorBranch()
|
||||
model.name = name
|
||||
return model
|
||||
}
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
var id: UUID?
|
||||
var name: String?
|
||||
var vendorId: Vendor.IDValue?
|
||||
|
||||
func toModel() -> VendorBranch {
|
||||
let model = VendorBranch()
|
||||
|
||||
model.id = id
|
||||
if let name {
|
||||
model.name = name
|
||||
}
|
||||
if let vendorId {
|
||||
model.$vendor.id = vendorId
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateVendorBranch"
|
||||
|
||||
func prepare(on database: Database) async throws {
|
||||
try await database.schema(VendorBranch.schema)
|
||||
.id()
|
||||
.field("name", .string, .required)
|
||||
.field("vendor_id", .uuid, .required, .references("vendor", "id"))
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: Database) async throws {
|
||||
try await database.schema(VendorBranch.schema).delete()
|
||||
}
|
||||
}
|
||||
|
||||
struct Update: Content {
|
||||
var name: String
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Validations
|
||||
|
||||
extension VendorBranch.Create: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
|
||||
extension VendorBranch.Update: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user