Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

107 řádky
3.5 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: "description") var description: 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. description: String? = nil,
  29. segmentid: Int,
  30. levelid: Int,
  31. createuserid: Int? = nil,
  32. updateuserid: Int? = nil
  33. ) {
  34. self.id = id
  35. self.name = name
  36. self.description = description
  37. self.$segment.$id.value = segmentid
  38. self.$level.$id.value = levelid
  39. self.createuserid = createuserid
  40. self.updateuserid = updateuserid
  41. }
  42. }
  43. extension mdlInspection{
  44. struct Create: AsyncMigration {
  45. func prepare(on database: Database) async throws {
  46. try await database.schema("inspection")
  47. .field("id", .int, .identifier(auto: true))
  48. .field("name", .string)
  49. .field("description", .string)
  50. .field("workOrderSegmentId", .int, .references("workOrderSegment", "id"))
  51. .field("inspectionLevelId", .int, .references("inspectionLevel", "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("inspection").delete()
  60. }
  61. }
  62. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspection>{
  63. let query = mdlInspection.query(on: req.db)
  64. .with(\.$segment)
  65. .with(\.$categories)
  66. .with(\.$level)
  67. .with(\.$notes)
  68. if let x = req.parameters.get("x"){
  69. let decryptedString = try CustomCrypto.DecryptString(input: x)
  70. let array = decryptedString.components(separatedBy: ":")
  71. ///This call is the standard get by id call
  72. if (array.count == 1){
  73. let id = Int(array[0])!
  74. return query.filter(\.$id == id)
  75. }
  76. ///Optional else if if there are multiple parameters in the call
  77. ///You must include an else for each call that has parameters
  78. ///Need to check the endpoint to get the right filter statements
  79. ///else if(x.count == 2){
  80. /// let username = x[0]
  81. /// let password = x[1]
  82. /// return query.filter(\.$username == username).filter(\.$password == password)
  83. ///}
  84. else{
  85. throw Abort(.badRequest)
  86. }
  87. }
  88. else{
  89. return query
  90. }
  91. }
  92. }