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.
 
 

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