Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

119 строки
3.8 KiB

  1. //
  2. // Contact.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 10/25/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class mdlContact: Model, Content{
  12. struct Input: Content{
  13. let id: Int?
  14. let firstname: String
  15. let lastname: String
  16. let isactive: Bool
  17. let createdate: Date?
  18. let createuserid: Int?
  19. let updatedate: Date?
  20. let updateuserid: Int?
  21. let customer: mdlCustomer
  22. let location: mdlLocation
  23. let contacttypes: [mdlContactMethod]?
  24. let files: [mdlFile]?
  25. }
  26. struct Output: Content{
  27. let id: Int?
  28. let firstname: String
  29. let lastname: String
  30. let isactive: Bool
  31. let createdate: Date?
  32. let createuserid: Int?
  33. let updatedate: Date?
  34. let updateuserid: Int?
  35. let customer: mdlCustomer
  36. let location: mdlLocation
  37. let contacttypes: [mdlContactMethod]?
  38. let files: [mdlFile]?
  39. }
  40. static let schema = "Contact"
  41. @ID(custom: "id", generatedBy: .database) var id: Int?
  42. @Field(key: "firstName") var firstname: String
  43. @Field(key: "lastName") var lastname: String
  44. @Field(key: "isActive") var isactive: Bool
  45. @Field(key: "createUserId" ) var createuserid: Int?
  46. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  47. @Field(key: "updateUserId") var updateuserid: Int?
  48. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  49. @Parent(key: "customerId") var customer: mdlCustomer
  50. @Parent(key: "locationId") var location: mdlLocation
  51. @Children(for: \.$contact) var contacttypes: [mdlContactMethod]
  52. @Children(for: \.$contact) var files: [mdlFile]
  53. init(){}
  54. 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]?){
  55. self.id = id
  56. self.firstname = firstname
  57. self.lastname = lastname
  58. self.isactive = isactive
  59. self.createuserid = createuserid
  60. self.createdate = createdate
  61. self.updateuserid = updateuserid
  62. self.updatedate = updatedate
  63. self.$customer.id = customer.id!
  64. self.$location.id = location.id!
  65. self.$contacttypes.value = contacttypes
  66. self.$files.value = files
  67. }
  68. }
  69. extension mdlContact {
  70. struct create: Migration{
  71. func prepare(on database: Database) -> EventLoopFuture<Void> {
  72. return database.schema("Contact")
  73. .field("id", .int, .identifier(auto: true))
  74. .field("customerId", .int, .references("Customer", "id"))
  75. .field("locationId", .int, .references("Location", "id"))
  76. .field("firstName", .string)
  77. .field("lastName", .string)
  78. .field("isActive", .bool)
  79. .field("createUserId", .int)
  80. .field("createDate", .datetime)
  81. .field("updateUserId", .int)
  82. .field("updateDate", .datetime)
  83. .create()
  84. }
  85. func revert(on database: Database) -> EventLoopFuture<Void> {
  86. database.schema("Contact").delete()
  87. }
  88. }
  89. /*
  90. struct seed: Migration {
  91. func prepare(on database: Database) -> EventLoopFuture<Void> {
  92. // 1
  93. let array: [<#Model#>] = [
  94. <#init#>
  95. ]
  96. // 2
  97. return array.map { obj in
  98. obj.save(on: database)
  99. }
  100. .flatten(on: database.eventLoop)
  101. }
  102. func revert(on database: Database) -> EventLoopFuture<Void> {
  103. // 3
  104. <#Model#>.query(on: database).delete()
  105. }
  106. }
  107. */
  108. }