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

127 строки
4.4 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? = nil, equipmenttypeid: Int? = nil, parentid: Int? = nil) {
  29. self.id = id
  30. self.number = number
  31. self.isactive = isactive
  32. self.$customer.$id.value = customerid!
  33. self.$model.$id.value = modelid!
  34. self.$equipmenttype.$id.value = equipmenttypeid!
  35. self.$parent.$id.value = parentid
  36. self.createuserid = createuserid
  37. self.updateuserid = updateuserid
  38. }
  39. }
  40. extension mdlEquipment{
  41. struct Create: AsyncMigration {
  42. func prepare(on database: Database) async throws {
  43. try await database.schema("equipment")
  44. .field("id", .int, .identifier(auto: true))
  45. .field("number", .string)
  46. .field("isActive", .bool)
  47. .field("customerId", .int, .references("customer", "id"))
  48. .field("modelId", .int, .references("model", "id"))
  49. .field("equipmentTypeId", .int, .references("equipmentType", "id"))
  50. .field("createDate", .datetime)
  51. .field("createUserId", .int)
  52. .field("updateDate", .datetime)
  53. .field("updateUserId", .int)
  54. .create()
  55. }
  56. func revert(on database: Database) async throws {
  57. try await database.schema("equipment").delete()
  58. }
  59. }
  60. struct AddParent: AsyncMigration {
  61. func prepare(on database: Database) async throws {
  62. try await database.schema("equipment")
  63. .field("parentId", .int, .references("equipment", "id"))
  64. .update()
  65. }
  66. func revert(on database: Database) async throws {
  67. try await database.schema("equipment")
  68. .deleteField("parentId")
  69. .update()
  70. }
  71. }
  72. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlEquipment>{
  73. let query = mdlEquipment.query(on: req.db)
  74. .with(\.$customer)
  75. .with(\.$model)
  76. .with(\.$equipmenttype)
  77. .with(\.$parent)
  78. .with(\.$properties)
  79. .with(\.$templates)
  80. .with(\.$workorders)
  81. if let x = req.parameters.get("x"){
  82. let dataQuery = req.parameters.get("x")
  83. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  84. let x = decryptedString.components(separatedBy: ":")
  85. ///This call is the standard get by id call
  86. if (x.count == 1){
  87. let id = Int(x[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. }