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.

185 lines
6.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 wosegment: mdlWorkOrderSegment
  15. @Parent(key: "segmentId") var segment: mdlSegment
  16. @Parent(key: "workOrderId") var workorder: mdlWorkOrder
  17. @Parent(key: "inspectionLevelId") var level: mdlInspectionLevel
  18. @Field(key: "name") var name: String?
  19. @Field(key: "segment") var seg: String?
  20. @Field(key: "interval") var interval: String?
  21. @Children(for: \.$inspection) var categories: [mdlInspectionCategory]
  22. @Children(for: \.$inspection) var notes: [mdlNote]
  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,
  29. name: String? = nil,
  30. interval: String? = nil,
  31. seg: String? = nil,
  32. segmentid: Int,
  33. workorderid: Int,
  34. levelid: Int,
  35. createuserid: Int? = nil,
  36. updateuserid: Int? = nil
  37. ) {
  38. self.id = id
  39. self.name = name
  40. self.interval = interval
  41. self.seg = seg
  42. self.$segment.$id.value = segmentid
  43. self.$workorder.$id.value = workorderid
  44. self.$level.$id.value = levelid
  45. self.createuserid = createuserid
  46. self.updateuserid = updateuserid
  47. }
  48. }
  49. extension mdlInspection{
  50. struct Create: AsyncMigration {
  51. func prepare(on database: Database) async throws {
  52. try await database.schema("inspection")
  53. .field("id", .int, .identifier(auto: true))
  54. .field("name", .string)
  55. .field("description", .string)
  56. .field("workOrderSegmentId", .int, .references("workOrderSegment", "id"))
  57. .field("inspectionLevelId", .int, .references("inspectionLevel", "id"))
  58. .field("createDate", .datetime)
  59. .field("createUserId", .int)
  60. .field("updateDate", .datetime)
  61. .field("updateUserId", .int)
  62. .create()
  63. }
  64. func revert(on database: Database) async throws {
  65. try await database.schema("inspection").delete()
  66. }
  67. }
  68. struct Mod1: AsyncMigration{
  69. func prepare(on database: Database) async throws {
  70. try await database.schema("inspection")
  71. .deleteField("description")
  72. .field("segment", .string)
  73. .field("interval", .string)
  74. .update()
  75. }
  76. func revert(on database: Database) async throws {
  77. try await database.schema("inspection")
  78. .deleteField("segment")
  79. .deleteField("interval")
  80. .field("description", .string)
  81. .update()
  82. }
  83. }
  84. struct Mod2: AsyncMigration{
  85. func prepare(on database: Database) async throws {
  86. try await database.schema("inspection")
  87. .field("segmentId", .int, .references("segment", "id"))
  88. .field("workOrderId", .int, .references("workOrder", "id"))
  89. .update()
  90. }
  91. func revert(on database: Database) async throws {
  92. try await database.schema("inspection")
  93. .deleteField("segmentId")
  94. .deleteField("workOrderId")
  95. .update()
  96. }
  97. }
  98. // struct Update: AsyncMigration{
  99. //
  100. // func prepare(on database: Database) async throws {
  101. // let mdls = try await mdlInspection.query(on: database).with(\.$wosegment){wos in wos.with(\.$segment).with(\.$workorder)}.all()// have to change to optional parent first so the query will run
  102. // for mdl in mdls {
  103. // mdl.$segment.$id.value = mdl.wosegment.segment.id
  104. // mdl.$workorder.$id.value = mdl.wosegment.workorder.id
  105. // try await mdl.save(on: database)
  106. // }
  107. //// let mdls: [mdlSignatureType] = [
  108. //// .init(id: 1, name: "Technician", description: nil, isactive: true, isrequired: true, createuserid: -1, updateuserid: -1),
  109. //// .init(id: 2, name: "Customer", description: nil, isactive: true, isrequired: false, createuserid: -1, updateuserid: -1)
  110. //// ]
  111. //// return mdls.map { mdl in
  112. //// mdl.save(on: database)
  113. //// }
  114. //// .flatten(on: database.eventLoop)
  115. // }
  116. //
  117. // func revert(on database: Database) async throws {
  118. // try await mdlInspection.query(on: database).delete()
  119. // }
  120. //
  121. //
  122. // }
  123. struct Mod3: AsyncMigration{
  124. func prepare(on database: Database) async throws {
  125. try await database.schema("inspection")
  126. .deleteField("workOrderSegmentId")
  127. .update()
  128. }
  129. func revert(on database: Database) async throws {
  130. try await database.schema("inspection")
  131. .field("workOrderSegmentId", .int, .references("workOrderSegment", "id"))
  132. .update()
  133. }
  134. }
  135. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspection>{
  136. let query = mdlInspection.query(on: req.db)
  137. .with(\.$segment)
  138. .with(\.$workorder)
  139. .with(\.$categories)
  140. .with(\.$level)
  141. .with(\.$notes)
  142. if let x = req.parameters.get("x"){
  143. let decryptedString = try CustomCrypto.DecryptString(input: x)
  144. let array = decryptedString.components(separatedBy: ":")
  145. ///This call is the standard get by id call
  146. if (array.count == 1){
  147. let id = Int(array[0])!
  148. return query.filter(\.$id == id)
  149. }
  150. ///Optional else if if there are multiple parameters in the call
  151. ///You must include an else for each call that has parameters
  152. ///Need to check the endpoint to get the right filter statements
  153. ///else if(x.count == 2){
  154. /// let username = x[0]
  155. /// let password = x[1]
  156. /// return query.filter(\.$username == username).filter(\.$password == password)
  157. ///}
  158. else{
  159. throw Abort(.badRequest)
  160. }
  161. }
  162. else{
  163. return query
  164. }
  165. }
  166. }