feat: Begins integrating database client into vapor app.
This commit is contained in:
@@ -1,119 +1,119 @@
|
||||
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
|
||||
|
||||
@Timestamp(key: "created_at", on: .create)
|
||||
var createdAt: Date?
|
||||
|
||||
@Timestamp(key: "updated_at", on: .update)
|
||||
var updatedAt: Date?
|
||||
|
||||
@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,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt
|
||||
)
|
||||
}
|
||||
|
||||
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?
|
||||
let createdAt: Date?
|
||||
let updatedAt: Date?
|
||||
|
||||
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: any Database) async throws {
|
||||
try await database.schema(VendorBranch.schema)
|
||||
.id()
|
||||
.field("name", .string, .required)
|
||||
.field("vendor_id", .uuid, .required)
|
||||
.field("created_at", .datetime)
|
||||
.field("updated_at", .datetime)
|
||||
.foreignKey("vendor_id", references: Vendor.schema, "id", onDelete: .cascade)
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: any 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)
|
||||
}
|
||||
}
|
||||
// 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
|
||||
//
|
||||
// @Timestamp(key: "created_at", on: .create)
|
||||
// var createdAt: Date?
|
||||
//
|
||||
// @Timestamp(key: "updated_at", on: .update)
|
||||
// var updatedAt: Date?
|
||||
//
|
||||
// @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,
|
||||
// createdAt: createdAt,
|
||||
// updatedAt: updatedAt
|
||||
// )
|
||||
// }
|
||||
//
|
||||
// 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?
|
||||
// let createdAt: Date?
|
||||
// let updatedAt: Date?
|
||||
//
|
||||
// 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: any Database) async throws {
|
||||
// try await database.schema(VendorBranch.schema)
|
||||
// .id()
|
||||
// .field("name", .string, .required)
|
||||
// .field("vendor_id", .uuid, .required)
|
||||
// .field("created_at", .datetime)
|
||||
// .field("updated_at", .datetime)
|
||||
// .foreignKey("vendor_id", references: Vendor.schema, "id", onDelete: .cascade)
|
||||
// .create()
|
||||
// }
|
||||
//
|
||||
// func revert(on database: any 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