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.

mdlUser.swift 4.5 KiB

2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
2 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // mdlUser.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 mdlUser: Model, Content {
  12. static let schema = "user"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "number") var number: String?
  15. @Field(key: "firstName") var firstname: String?
  16. @Field(key: "lastName") var lastname: String?
  17. @Field(key: "username") var username: String?
  18. @Field(key: "password") var password: String?
  19. @Field(key: "email") var email: String?
  20. @Field(key: "isActive") var isactive: Bool?
  21. @OptionalParent(key: "divisionId") var division: mdlDivision?
  22. @OptionalParent(key: "locationId") var location: mdlLocation?
  23. @OptionalParent(key: "gateId") var gate: mdlGate?
  24. @Siblings(through: mdlXUserRole.self, from: \.$user, to: \.$role) var roles: [mdlRole]
  25. @Children(for: \.$technician) var workorders: [mdlWorkOrder]
  26. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  27. @Field(key: "createUserId") var createuserid: Int?
  28. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  29. @Field(key: "updateUserId") var updateuserid: Int?
  30. init() {}
  31. init(id: Int? = nil, number: String? = nil, firstname: String? = nil, lastname: String? = nil, username: String? = nil, password: String? = nil, email: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, divisionid: Int? = nil, locationid: Int? = nil, gateid: Int? = nil) {
  32. self.id = id
  33. self.number = number
  34. self.firstname = firstname
  35. self.lastname = lastname
  36. self.username = username
  37. self.password = password
  38. self.email = email
  39. self.isactive = isactive
  40. if(divisionid != nil) { self.$division.$id.value = divisionid }
  41. if(locationid != nil) { self.$location.$id.value = locationid }
  42. if(gateid != nil) { self.$gate.$id.value = gateid }
  43. self.createuserid = createuserid
  44. self.updateuserid = updateuserid
  45. }
  46. }
  47. extension mdlUser{
  48. struct Create: AsyncMigration {
  49. func prepare(on database: Database) async throws {
  50. try await database.schema("user")
  51. .field("id", .int, .identifier(auto: true))
  52. .field("number", .string)
  53. .field("firstName", .string)
  54. .field("lastName", .string)
  55. .field("username", .string)
  56. .field("password", .string)
  57. .field("email", .string)
  58. .field("isActive", .bool)
  59. .field("divisionId", .int, .references("division", "id"))
  60. .field("locationId", .int, .references("location", "id"))
  61. .field("gateId", .int, .references("gate", "id"))
  62. .field("createDate", .datetime)
  63. .field("createUserId", .int)
  64. .field("updateDate", .datetime)
  65. .field("updateUserId", .int)
  66. .create()
  67. }
  68. func revert(on database: Database) async throws {
  69. try await database.schema("user").delete()
  70. }
  71. }
  72. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlUser>{
  73. let query = mdlUser.query(on: req.db)
  74. .with(\.$roles)
  75. .with(\.$division)
  76. .with(\.$location)
  77. .with(\.$gate)
  78. .with(\.$workorders)
  79. if let x = req.parameters.get("x"){
  80. let decryptedString = try CustomCrypto.DecryptString(input: x)
  81. let array = decryptedString.components(separatedBy: ":")
  82. ///This call is the standard get by id call
  83. let p = req.url.path.components(separatedBy: "/")[4]
  84. if (array.count == 1){
  85. let id = Int(array[0])!
  86. return query.filter(\.$id == id)
  87. }
  88. ///Optional else if if there are multiple parameters in the call
  89. ///You must include an else for each call that has parameters
  90. ///Need to check the endpoint to get the right filter statements
  91. ///
  92. else if(array.count == 2 && p.uppercased() == "LOGIN"){
  93. let username = array[0]
  94. let password = array[1]
  95. return query.filter(\.$username == username).filter(\.$password == password)
  96. }
  97. else{
  98. throw Abort(.badRequest, reason: "Parameter missmatch.")
  99. }
  100. }
  101. else{
  102. return query
  103. }
  104. }
  105. }