You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

113 lines
3.3 KiB

  1. //
  2. // Product.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 mdlProduct: Model, Content{
  12. static let schema = "Product"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "code") var code: String
  15. @Field(key: "description") var description: String
  16. @Field(key: "isActive") var isactive: Bool
  17. @Field(key: "createUserId" ) var createuserid: Int?
  18. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  19. @Field(key: "updateUserId") var updateuserid: Int?
  20. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  21. @Children(for: \.$product) var models: [mdlModel]
  22. @Children(for: \.$product) var templates: [mdlTemplate]
  23. init(){}
  24. init(id: Int? = nil, code: String, description: String, isactive: Bool = true, createuserid: Int? = -1, createdate: Date? = nil, updateuserid: Int? = -1, updatedate: Date? = nil, models: [mdlModel]?, templates: [mdlTemplate]?){
  25. self.id = id
  26. self.code = code
  27. self.description = description
  28. self.isactive = isactive
  29. self.createuserid = createuserid
  30. self.createdate = createdate
  31. self.updateuserid = updateuserid
  32. self.updatedate = updatedate
  33. self.$models.value = models
  34. self.$templates.value = templates
  35. }
  36. }
  37. //JSON Content
  38. extension mdlProduct{
  39. struct Input: Content{
  40. let id: Int?
  41. let code: String
  42. let description: String
  43. let isactive: Bool
  44. let createdate: Date?
  45. let createuserid: Int?
  46. let updatedate: Date?
  47. let updateuserid: Int?
  48. let models: [mdlModel]?
  49. let templates: [mdlTemplate]?
  50. }
  51. struct Output: Content{
  52. let id: Int?
  53. let code: String
  54. let description: String
  55. let isactive: Bool
  56. let createdate: Date?
  57. let createuserid: Int?
  58. let updatedate: Date?
  59. let updateuserid: Int?
  60. let models: [mdlModel]?
  61. let templates: [mdlTemplate]?
  62. }
  63. }
  64. extension mdlProduct {
  65. struct create: Migration{
  66. func prepare(on database: Database) -> EventLoopFuture<Void> {
  67. return database.schema("Product")
  68. .field("id", .int, .identifier(auto: true))
  69. .field("code", .string)
  70. .field("description", .string)
  71. .field("isActive", .bool)
  72. .field("createUserId", .int)
  73. .field("createDate", .datetime)
  74. .field("updateUserId", .int)
  75. .field("updateDate", .datetime)
  76. .create()
  77. }
  78. func revert(on database: Database) -> EventLoopFuture<Void> {
  79. database.schema("Product").delete()
  80. }
  81. }
  82. /*
  83. struct seed: Migration {
  84. func prepare(on database: Database) -> EventLoopFuture<Void> {
  85. // 1
  86. let array: [<#Model#>] = [
  87. <#init#>
  88. ]
  89. // 2
  90. return array.map { obj in
  91. obj.save(on: database)
  92. }
  93. .flatten(on: database.eventLoop)
  94. }
  95. func revert(on database: Database) -> EventLoopFuture<Void> {
  96. // 3
  97. <#Model#>.query(on: database).delete()
  98. }
  99. }
  100. */
  101. }