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

127 строки
4.5 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. @OptionalParent(key: "customerId") var customer: mdlCustomer?
  17. @Parent(key: "modelId") var model: mdlModel
  18. @Parent(key: "equipmentTypeId") var equipmenttype: mdlEquipmentType
  19. @OptionalParent(key: "parentId") var parent: mdlEquipment?
  20. @Children(for: \.$equipment) var properties: [mdlObjProperty]
  21. @Children(for: \.$equipment) var templates: [mdlTemplate]
  22. @Siblings(through: mdlWorkOrderAttachment.self, from: \.$equipment, to: \.$workorder) var workorders: [mdlWorkOrder]
  23. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  24. @Field(key: "createUserId") var createuserid: Int?
  25. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  26. @Field(key: "updateUserId") var updateuserid: Int?
  27. init() { }
  28. 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) {
  29. self.id = id
  30. self.number = number
  31. self.isactive = isactive
  32. if(customerid != nil) { self.$customer.$id.value = customerid! }
  33. self.$model.$id.value = modelid
  34. self.$equipmenttype.$id.value = equipmenttypeid
  35. self.$parent.$id.value = parentid
  36. if(parentid != nil) { self.$parent.$id.value = parentid! }
  37. self.createuserid = createuserid
  38. self.updateuserid = updateuserid
  39. }
  40. }
  41. extension mdlEquipment{
  42. struct Create: AsyncMigration {
  43. func prepare(on database: Database) async throws {
  44. try await database.schema("equipment")
  45. .field("id", .int, .identifier(auto: true))
  46. .field("number", .string)
  47. .field("isActive", .bool)
  48. .field("customerId", .int, .references("customer", "id"))
  49. .field("modelId", .int, .references("model", "id"))
  50. .field("equipmentTypeId", .int, .references("equipmentType", "id"))
  51. .field("createDate", .datetime)
  52. .field("createUserId", .int)
  53. .field("updateDate", .datetime)
  54. .field("updateUserId", .int)
  55. .create()
  56. }
  57. func revert(on database: Database) async throws {
  58. try await database.schema("equipment").delete()
  59. }
  60. }
  61. struct AddParent: AsyncMigration {
  62. func prepare(on database: Database) async throws {
  63. try await database.schema("equipment")
  64. .field("parentId", .int, .references("equipment", "id"))
  65. .update()
  66. }
  67. func revert(on database: Database) async throws {
  68. try await database.schema("equipment")
  69. .deleteField("parentId")
  70. .update()
  71. }
  72. }
  73. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlEquipment>{
  74. let query = mdlEquipment.query(on: req.db)
  75. .with(\.$customer)
  76. .with(\.$model)
  77. .with(\.$equipmenttype)
  78. .with(\.$parent)
  79. .with(\.$properties)
  80. .with(\.$templates)
  81. .with(\.$workorders)
  82. if let x = req.parameters.get("x"){
  83. let decryptedString = try CustomCrypto.DecryptString(input: x)
  84. let array = decryptedString.components(separatedBy: ":")
  85. ///This call is the standard get by id call
  86. if (array.count == 1){
  87. let id = Int(array[0])!
  88. return query.filter(\.$id == id)
  89. }
  90. ///Optional else if if there are multiple parameters in the call
  91. ///You must include an else for each call that has parameters
  92. ///Need to check the endpoint to get the right filter statements
  93. ///else if(x.count == 2){
  94. /// let username = x[0]
  95. /// let password = x[1]
  96. /// return query.filter(\.$username == username).filter(\.$password == password)
  97. ///}
  98. else{
  99. throw Abort(.badRequest)
  100. }
  101. }
  102. else{
  103. return query
  104. }
  105. }
  106. }