No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 

104 líneas
3.4 KiB

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