You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.6 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 dataQuery = req.parameters.get("x")
  70. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  71. let x = decryptedString.components(separatedBy: ":")
  72. ///This call is the standard get by id call
  73. if (x.count == 1){
  74. let id = Int(x[0])!
  75. return query.filter(\.$id == id)
  76. }
  77. ///Optional else if if there are multiple parameters in the call
  78. ///You must include an else for each call that has parameters
  79. ///Need to check the endpoint to get the right filter statements
  80. ///else if(x.count == 2){
  81. /// let username = x[0]
  82. /// let password = x[1]
  83. /// return query.filter(\.$username == username).filter(\.$password == password)
  84. ///}
  85. else{
  86. throw Abort(.badRequest)
  87. }
  88. }
  89. else{
  90. return query
  91. }
  92. }
  93. }