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

141 строка
5.0 KiB

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