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

145 строки
5.1 KiB

  1. //
  2. // mdlTemplate.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 2/16/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlTemplate: Model, Content {
  12. static let schema = "template"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @OptionalParent(key: "inspectionLevelId") var level: mdlInspectionLevel?
  15. @OptionalParent(key: "productId") var product: mdlProduct?
  16. @OptionalParent(key: "makeId") var make: mdlMake?
  17. @OptionalParent(key: "modelId") var model: mdlModel?
  18. @OptionalParent(key: "customerId") var customer: mdlCustomer?
  19. @OptionalParent(key: "equipmentId") var equipment: mdlEquipment?
  20. @Children(for: \.$template) var categories: [mdlTemplateCategory]
  21. @Field(key: "name") var name: String?
  22. @Field(key: "description") var description: String?
  23. @Field(key: "isActive") var isactive: Bool?
  24. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  25. @Field(key: "createUserId") var createuserid: Int?
  26. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  27. @Field(key: "updateUserId") var updateuserid: Int?
  28. init() { }
  29. init(id: Int? = nil,
  30. levelid: Int? = nil,
  31. productid: Int? = nil,
  32. makeid: Int? = nil,
  33. modelid: Int? = nil,
  34. customerid: Int? = nil,
  35. equipmentid: Int? = nil,
  36. name: String? = nil,
  37. description: String? = nil,
  38. isactive: Bool? = true,
  39. createuserid: Int? = nil,
  40. updateuserid: Int? = nil
  41. ) {
  42. self.id = id
  43. self.$level.$id.value = levelid!
  44. self.$product.$id.value = productid!
  45. self.$make.$id.value = makeid!
  46. self.$model.$id.value = modelid!
  47. self.$customer.$id.value = customerid!
  48. self.$equipment.$id.value = equipmentid!
  49. self.name = name
  50. self.description = description
  51. self.isactive = isactive
  52. self.createuserid = createuserid
  53. self.updateuserid = updateuserid
  54. }
  55. }
  56. extension mdlTemplate{
  57. struct Create: AsyncMigration {
  58. func prepare(on database: Database) async throws {
  59. try await database.schema("template")
  60. .field("id", .int, .identifier(auto: true))
  61. .field("inspectionLevelId", .int, .references("inspectionLevel", "id"))
  62. .field("productId", .int, .references("product", "id"))
  63. .field("makeId", .int, .references("make", "id"))
  64. .field("modelId", .int, .references("model", "id"))
  65. .field("customerId", .int, .references("customer", "id"))
  66. .field("equipment", .int, .references("equipment", "id"))
  67. .field("name", .string)
  68. .field("description", .string)
  69. .field("isActive", .bool)
  70. .field("createDate", .datetime)
  71. .field("createUserId", .int)
  72. .field("updateDate", .datetime)
  73. .field("updateUserId", .int)
  74. .create()
  75. }
  76. func revert(on database: Database) async throws {
  77. try await database.schema("template").delete()
  78. }
  79. }
  80. struct Mod1: AsyncMigration {
  81. func prepare(on database: Database) async throws {
  82. try await database.schema("template")
  83. .deleteField("equipment")
  84. .field("equipmentId", .int, .references("equipment", "id"))
  85. .update()
  86. }
  87. func revert(on database: Database) async throws {
  88. try await database.schema("template")
  89. .deleteField("equipmentId")
  90. .field("equipment", .int, .references("equipment", "id"))
  91. .update()
  92. }
  93. }
  94. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlTemplate>{
  95. let query = mdlTemplate.query(on: req.db)
  96. .with(\.$level)
  97. .with(\.$product)
  98. .with(\.$make)
  99. .with(\.$model)
  100. .with(\.$customer)
  101. .with(\.$equipment)
  102. .with(\.$categories)
  103. if let x = req.parameters.get("x"){
  104. let dataQuery = req.parameters.get("x")
  105. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  106. let x = decryptedString.components(separatedBy: ":")
  107. ///This call is the standard get by id call
  108. if (x.count == 1){
  109. let id = Int(x[0])!
  110. return query.filter(\.$id == id)
  111. }
  112. ///Optional else if if there are multiple parameters in the call
  113. ///You must include an else for each call that has parameters
  114. ///Need to check the endpoint to get the right filter statements
  115. ///else if(x.count == 2){
  116. /// let username = x[0]
  117. /// let password = x[1]
  118. /// return query.filter(\.$username == username).filter(\.$password == password)
  119. ///}
  120. else{
  121. throw Abort(.badRequest)
  122. }
  123. }
  124. else{
  125. return query
  126. }
  127. }
  128. }