You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB

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