|
- //
- // Contact.swift
- //
- //
- // Created by Michiel Carman on 10/25/21.
- //
-
- import Foundation
- import Vapor
- import Fluent
- import FluentPostgresDriver
-
- final class mdlContact: Model, Content{
- struct Input: Content{
- let id: Int?
- let firstname: String
- let lastname: String
- let isactive: Bool
- let createdate: Date?
- let createuserid: Int?
- let updatedate: Date?
- let updateuserid: Int?
- let customer: mdlCustomer
- let location: mdlLocation
- let contacttypes: [mdlContactMethod]?
- let files: [mdlFile]?
- }
-
- struct Output: Content{
- let id: Int?
- let firstname: String
- let lastname: String
- let isactive: Bool
- let createdate: Date?
- let createuserid: Int?
- let updatedate: Date?
- let updateuserid: Int?
- let customer: mdlCustomer
- let location: mdlLocation
- let contacttypes: [mdlContactMethod]?
- let files: [mdlFile]?
- }
-
- static let schema = "Contact"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
- @Field(key: "firstName") var firstname: String
- @Field(key: "lastName") var lastname: String
- @Field(key: "isActive") var isactive: Bool
- @Field(key: "createUserId" ) var createuserid: Int?
- @Timestamp(key: "createDate", on: .create) var createdate: Date?
- @Field(key: "updateUserId") var updateuserid: Int?
- @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
- @Parent(key: "customerId") var customer: mdlCustomer
- @Parent(key: "locationId") var location: mdlLocation
- @Children(for: \.$contact) var contacttypes: [mdlContactMethod]
- @Children(for: \.$contact) var files: [mdlFile]
-
- init(){}
-
- init(id: Int? = nil, firstname: String, lastname: String, isactive: Bool, createuserid: Int? = -1, createdate: Date? = nil, updateuserid: Int? = -1, updatedate: Date? = nil, customer: mdlCustomer, location: mdlLocation, contacttypes: [mdlContactMethod]?, files: [mdlFile]?){
- self.id = id
- self.firstname = firstname
- self.lastname = lastname
- self.isactive = isactive
- self.createuserid = createuserid
- self.createdate = createdate
- self.updateuserid = updateuserid
- self.updatedate = updatedate
- self.$customer.id = customer.id!
- self.$location.id = location.id!
- self.$contacttypes.value = contacttypes
- self.$files.value = files
- }
- }
-
- extension mdlContact {
- struct create: Migration{
- func prepare(on database: Database) -> EventLoopFuture<Void> {
- return database.schema("Contact")
- .field("id", .int, .identifier(auto: true))
- .field("customerId", .int, .references("Customer", "id"))
- .field("locationId", .int, .references("Location", "id"))
- .field("firstName", .string)
- .field("lastName", .string)
- .field("isActive", .bool)
- .field("createUserId", .int)
- .field("createDate", .datetime)
- .field("updateUserId", .int)
- .field("updateDate", .datetime)
- .create()
- }
- func revert(on database: Database) -> EventLoopFuture<Void> {
- database.schema("Contact").delete()
- }
- }
- /*
- struct seed: Migration {
- func prepare(on database: Database) -> EventLoopFuture<Void> {
- // 1
- let array: [<#Model#>] = [
- <#init#>
- ]
- // 2
- return array.map { obj in
- obj.save(on: database)
- }
- .flatten(on: database.eventLoop)
- }
-
- func revert(on database: Database) -> EventLoopFuture<Void> {
- // 3
- <#Model#>.query(on: database).delete()
- }
- }
- */
- }
|