25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mdlSpecItem.swift 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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? = nil, comparisontypeid: Int? = nil, unitid: Int? = nil) {
  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 dataQuery = req.parameters.get("x")
  70. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  71. let x = decryptedString.components(separatedBy: ":")
  72. ///This call is the standard get by id call
  73. if (x.count == 1){
  74. let id = Int(x[0])!
  75. return query.filter(\.$id == id)
  76. }
  77. ///Optional else if if there are multiple parameters in the call
  78. ///You must include an else for each call that has parameters
  79. ///Need to check the endpoint to get the right filter statements
  80. ///else if(x.count == 2){
  81. /// let username = x[0]
  82. /// let password = x[1]
  83. /// return query.filter(\.$username == username).filter(\.$password == password)
  84. ///}
  85. else{
  86. throw Abort(.badRequest)
  87. }
  88. }
  89. else{
  90. return query
  91. }
  92. }
  93. }