Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

106 righe
3.5 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. @Children(for: \.$inspectionlevel) var values: [mdlTblValue]
  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. isactive: Bool? = true,
  30. inspectiontypeid: Int,
  31. createuserid: Int? = nil,
  32. updateuserid: Int? = nil
  33. ) {
  34. self.id = id
  35. self.name = name
  36. self.description = description
  37. self.isactive = isactive
  38. self.$inspectiontype.$id.value = inspectiontypeid
  39. self.createuserid = createuserid
  40. self.updateuserid = updateuserid
  41. }
  42. }
  43. extension mdlInspectionLevel{
  44. struct Create: AsyncMigration {
  45. func prepare(on database: Database) async throws {
  46. try await database.schema("inspectionLevel")
  47. .field("id", .int, .identifier(auto: true))
  48. .field("name", .string)
  49. .field("description", .string)
  50. .field("isActive", .bool)
  51. .field("inspectionTypeId", .int)
  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("inspectionLevel").delete()
  60. }
  61. }
  62. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspectionLevel>{
  63. let query = mdlInspectionLevel.query(on: req.db)
  64. .with(\.$inspectiontype)
  65. .with(\.$inspections)
  66. .with(\.$templates)
  67. .with(\.$values)
  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. }