25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

125 satır
4.1 KiB

  1. //
  2. // mdlInspection.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 mdlInspection: Model, Content {
  12. static let schema = "inspection"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Parent(key: "workOrderSegmentId") var segment: mdlWorkOrderSegment
  15. @Parent(key: "inspectionLevelId") var level: mdlInspectionLevel
  16. @Field(key: "name") var name: String?
  17. @Field(key: "segment") var seg: String?
  18. @Field(key: "interval") var interval: String?
  19. @Children(for: \.$inspection) var categories: [mdlInspectionCategory]
  20. @Children(for: \.$inspection) var notes: [mdlNote]
  21. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  22. @Field(key: "createUserId") var createuserid: Int?
  23. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  24. @Field(key: "updateUserId") var updateuserid: Int?
  25. init() { }
  26. init(id: Int? = nil,
  27. name: String? = nil,
  28. interval: String? = nil,
  29. seg: String? = nil,
  30. segmentid: Int,
  31. levelid: Int,
  32. createuserid: Int? = nil,
  33. updateuserid: Int? = nil
  34. ) {
  35. self.id = id
  36. self.name = name
  37. self.interval = interval
  38. self.seg = seg
  39. self.$segment.$id.value = segmentid
  40. self.$level.$id.value = levelid
  41. self.createuserid = createuserid
  42. self.updateuserid = updateuserid
  43. }
  44. }
  45. extension mdlInspection{
  46. struct Create: AsyncMigration {
  47. func prepare(on database: Database) async throws {
  48. try await database.schema("inspection")
  49. .field("id", .int, .identifier(auto: true))
  50. .field("name", .string)
  51. .field("description", .string)
  52. .field("workOrderSegmentId", .int, .references("workOrderSegment", "id"))
  53. .field("inspectionLevelId", .int, .references("inspectionLevel", "id"))
  54. .field("createDate", .datetime)
  55. .field("createUserId", .int)
  56. .field("updateDate", .datetime)
  57. .field("updateUserId", .int)
  58. .create()
  59. }
  60. func revert(on database: Database) async throws {
  61. try await database.schema("inspection").delete()
  62. }
  63. }
  64. struct Mod1: AsyncMigration{
  65. func prepare(on database: Database) async throws {
  66. try await database.schema("inspection")
  67. .deleteField("description")
  68. .field("segment", .string)
  69. .field("interval", .string)
  70. .update()
  71. }
  72. func revert(on database: Database) async throws {
  73. try await database.schema("inspection")
  74. .deleteField("segment")
  75. .deleteField("interval")
  76. .field("description", .string)
  77. .update()
  78. }
  79. }
  80. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspection>{
  81. let query = mdlInspection.query(on: req.db)
  82. .with(\.$segment)
  83. .with(\.$categories)
  84. .with(\.$level)
  85. .with(\.$notes)
  86. if let x = req.parameters.get("x"){
  87. let decryptedString = try CustomCrypto.DecryptString(input: x)
  88. let array = decryptedString.components(separatedBy: ":")
  89. ///This call is the standard get by id call
  90. if (array.count == 1){
  91. let id = Int(array[0])!
  92. return query.filter(\.$id == id)
  93. }
  94. ///Optional else if if there are multiple parameters in the call
  95. ///You must include an else for each call that has parameters
  96. ///Need to check the endpoint to get the right filter statements
  97. ///else if(x.count == 2){
  98. /// let username = x[0]
  99. /// let password = x[1]
  100. /// return query.filter(\.$username == username).filter(\.$password == password)
  101. ///}
  102. else{
  103. throw Abort(.badRequest)
  104. }
  105. }
  106. else{
  107. return query
  108. }
  109. }
  110. }