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.
 
 

112 righe
3.8 KiB

  1. //
  2. // mdlSpecItem.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 1/24/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlSpecItem: Model, Content {
  12. static let schema = "specItem"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "qualifier") var qualifier: String?
  16. @Field(key: "sortOrder") var sortorder: Int?
  17. @Field(key: "isActive") var isactive: Bool?
  18. @Parent(key: "specId") var spec: mdlSpec
  19. @Parent(key: "comparisonTypeId") var comparisontype: mdlComparisonType
  20. @Parent(key: "unitId") var unit: mdlUnit
  21. @Children(for: \.$specitem) var values: [mdlSpecItemValue]
  22. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  23. @Field(key: "createUserId") var createuserid: Int?
  24. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  25. @Field(key: "updateUserId") var updateuserid: Int?
  26. init() { }
  27. init(id: Int? = nil, name: String? = nil, qualifier: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, sortorder: Int? = nil, updateuserid: Int? = nil, specid: Int, comparisontypeid: Int, unitid: Int) {
  28. self.id = id
  29. self.name = name
  30. self.qualifier = qualifier
  31. self.sortorder = sortorder
  32. self.$spec.$id.value = specid
  33. self.$comparisontype.$id.value = comparisontypeid
  34. self.$unit.$id.value = unitid
  35. self.isactive = isactive
  36. self.createuserid = createuserid
  37. self.updateuserid = updateuserid
  38. }
  39. }
  40. extension mdlSpecItem{
  41. struct Create: AsyncMigration {
  42. func prepare(on database: Database) async throws {
  43. try await database.schema("specItem")
  44. .field("id", .int, .identifier(auto: true))
  45. .field("name", .string)
  46. .field("qualifier", .string)
  47. .field("sortOrder", .int)
  48. .field("specId", .int, .references("spec", "id"))
  49. .field("comparisonTypeId", .int, .references("comparisonType", "id"))
  50. .field("unitId", .int, .references("unit", "id"))
  51. .field("isActive", .bool)
  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("specItem").delete()
  60. }
  61. }
  62. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlSpecItem>{
  63. let query = mdlSpecItem.query(on: req.db)
  64. .with(\.$spec)
  65. .with(\.$comparisontype)
  66. .with(\.$unit)
  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. }