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ů.
 
 

98 řádky
3.2 KiB

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