25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

154 lines
5.4 KiB

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