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.
 
 

120 line
4.0 KiB

  1. //
  2. // mdlItem.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 mdlItem: Model, Content {
  12. static let schema = "item"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "description") var description: String?
  16. @Field(key: "isActive") var isactive: Bool?
  17. @Parent(key: "categoryId") var category: mdlCategory
  18. @OptionalParent(key: "smscCodeId") var smsccode: mdlSMSCCode?
  19. @Parent(key: "inputTypeId") var inputtype: mdlInputType
  20. @Children(for: \.$item) var templateitems: [mdlTemplateItemSpec]
  21. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  22. @Field(key: "createUserId") var createuserid: Int?
  23. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  24. @Field(key: "updateUserId") var updateuserid: Int?
  25. init() { }
  26. init(id: Int? = nil, name: String? = nil, description: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil, categoryid: Int, smsccodeid: Int? = nil, inputtypeid: Int) {
  27. self.id = id
  28. self.name = name
  29. self.description = description
  30. self.isactive = isactive
  31. self.$category.$id.value = categoryid
  32. if(smsccodeid != nil){ self.$smsccode.$id.value = smsccodeid }
  33. self.$inputtype.$id.value = inputtypeid
  34. self.createuserid = createuserid
  35. self.updateuserid = updateuserid
  36. }
  37. }
  38. extension mdlItem{
  39. struct Create: AsyncMigration {
  40. func prepare(on database: Database) async throws {
  41. try await database.schema("item")
  42. .field("id", .int, .identifier(auto: true))
  43. .field("name", .string)
  44. .field("description", .string)
  45. .field("isActive", .bool)
  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("item").delete()
  54. }
  55. }
  56. struct Mod1: AsyncMigration {
  57. func prepare(on database: Database) async throws {
  58. try await database.schema("item")
  59. .field("categoryId", .int, .references("category", "id"))
  60. .field("smscCodeId", .int, .references("smscCode", "id"))
  61. .field("inputTypeId", .int, .references("inputType", "id"))
  62. .update()
  63. }
  64. func revert(on database: Database) async throws {
  65. try await database.schema("item")
  66. .deleteField("categoryId")
  67. .deleteField("smscCodeId")
  68. .deleteField("inputTypeId")
  69. .update()
  70. }
  71. }
  72. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlItem>{
  73. let query = mdlItem.query(on: req.db)
  74. .with(\.$templateitems)
  75. if let x = req.parameters.get("x"){
  76. let decryptedString = try CustomCrypto.DecryptString(input: x)
  77. let array = decryptedString.components(separatedBy: ":")
  78. ///This call is the standard get by id call
  79. if (array.count == 1){
  80. let id = Int(array[0])!
  81. return query.filter(\.$id == id)
  82. }
  83. ///Optional else if if there are multiple parameters in the call
  84. ///You must include an else for each call that has parameters
  85. ///Need to check the endpoint to get the right filter statements
  86. ///else if(x.count == 2){
  87. /// let username = x[0]
  88. /// let password = x[1]
  89. /// return query.filter(\.$username == username).filter(\.$password == password)
  90. ///}
  91. else{
  92. throw Abort(.badRequest)
  93. }
  94. }
  95. else{
  96. return query
  97. }
  98. }
  99. }