25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

119 satır
3.9 KiB

  1. //
  2. // mdlGate.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 mdlGate: Model, Content {
  12. static let schema = "gate"
  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. @Field(key: "sortOrder") var sortorder: Int?
  18. @Parent(key: "locationId") var location: mdlLocation
  19. @Children(for: \.$gate) var users: [mdlUser]
  20. @Children(for: \.$gate) var values: [mdlTblValue]
  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? = nil, sortorder: Int? = 0, createuserid: Int? = nil, updateuserid: Int? = nil, locationid: Int) {
  27. self.id = id
  28. self.name = name
  29. self.description = description
  30. self.isactive = isactive
  31. self.sortorder = sortorder
  32. self.$location.$id.value = locationid
  33. self.createuserid = createuserid
  34. self.updateuserid = updateuserid
  35. }
  36. }
  37. extension mdlGate{
  38. struct Create: AsyncMigration {
  39. func prepare(on database: Database) async throws {
  40. try await database.schema("gate")
  41. .field("id", .int, .identifier(auto: true))
  42. .field("name", .string)
  43. .field("description", .string)
  44. .field("isActive", .bool)
  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("gate").delete()
  53. }
  54. }
  55. struct Mod1: AsyncMigration{
  56. func prepare(on database: Database) async throws {
  57. try await database.schema("gate")
  58. .field("locationId", .int, .references("location", "id"))
  59. .field("sortOrder", .int)
  60. .update()
  61. }
  62. func revert(on database: Database) async throws {
  63. try await database.schema("gate")
  64. .deleteField("locationId")
  65. .deleteField("sortOrder")
  66. .update()
  67. }
  68. }
  69. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlGate>{
  70. let query = mdlGate.query(on: req.db)
  71. .with(\.$users)
  72. .with(\.$values)
  73. if let x = req.parameters.get("x"){
  74. let decryptedString = try CustomCrypto.DecryptString(input: x)
  75. let array = decryptedString.components(separatedBy: ":")
  76. ///This call is the standard get by id call
  77. if (array.count == 1){
  78. let id = Int(array[0])!
  79. return query.filter(\.$id == id)
  80. }
  81. ///Optional else if if there are multiple parameters in the call
  82. ///You must include an else for each call that has parameters
  83. ///Need to check the endpoint to get the right filter statements
  84. ///else if(x.count == 2){
  85. /// let username = x[0]
  86. /// let password = x[1]
  87. /// return query.filter(\.$username == username).filter(\.$password == password)
  88. ///}
  89. else{
  90. throw Abort(.badRequest)
  91. }
  92. }
  93. else{
  94. return query
  95. }
  96. }
  97. }