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.
 
 

103 lines
3.4 KiB

  1. //
  2. // mdlTemplateItemSpec.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 mdlTemplateItemSpec: Model, Content {
  12. static let schema = "templateItemSpec"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Parent(key: "templateCategoryId") var templatecategory: mdlTemplateCategory
  15. @OptionalParent(key: "itemId") var item: mdlItem?
  16. @OptionalParent(key: "specId") var spec: mdlSpec?
  17. @Field(key: "sortOrder") var sortorder: Int?
  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. categoryid: Int,
  25. itemid: Int? = nil,
  26. specid: Int? = nil,
  27. sortorder: Int? = 0,
  28. createuserid: Int? = nil,
  29. updateuserid: Int? = nil
  30. ) {
  31. self.id = id
  32. self.$templatecategory.$id.value = categoryid
  33. if(itemid != nil){self.$item.$id.value = itemid}
  34. if(itemid != nil){self.$spec.$id.value = specid}
  35. self.sortorder = sortorder
  36. self.createuserid = createuserid
  37. self.updateuserid = updateuserid
  38. }
  39. }
  40. extension mdlTemplateItemSpec{
  41. struct Create: AsyncMigration {
  42. func prepare(on database: Database) async throws {
  43. try await database.schema("templateItemSpec")
  44. .field("id", .int, .identifier(auto: true))
  45. .field("templateCategoryId", .int, .references("templateCategory", "id"))
  46. .field("itemId", .int, .references("item", "id"))
  47. .field("specId", .int, .references("spec", "id"))
  48. .field("sortOrder", .int)
  49. .field("createDate", .datetime)
  50. .field("createUserId", .int)
  51. .field("updateDate", .datetime)
  52. .field("updateUserId", .int)
  53. .create()
  54. }
  55. func revert(on database: Database) async throws {
  56. try await database.schema("templateItemSpec").delete()
  57. }
  58. }
  59. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlTemplateItemSpec>{
  60. let query = mdlTemplateItemSpec.query(on: req.db)
  61. .with(\.$templatecategory)
  62. .with(\.$item)
  63. .with(\.$spec)
  64. if let x = req.parameters.get("x"){
  65. let decryptedString = try CustomCrypto.DecryptString(input: x)
  66. let array = decryptedString.components(separatedBy: ":")
  67. ///This call is the standard get by id call
  68. if (array.count == 1){
  69. let id = Int(array[0])!
  70. return query.filter(\.$id == id)
  71. }
  72. ///Optional else if if there are multiple parameters in the call
  73. ///You must include an else for each call that has parameters
  74. ///Need to check the endpoint to get the right filter statements
  75. ///else if(x.count == 2){
  76. /// let username = x[0]
  77. /// let password = x[1]
  78. /// return query.filter(\.$username == username).filter(\.$password == password)
  79. ///}
  80. else{
  81. throw Abort(.badRequest)
  82. }
  83. }
  84. else{
  85. return query
  86. }
  87. }
  88. }