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.

123 lines
4.7 KiB

  1. //
  2. // mdlRole.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 mdlRole: Model, Content {
  12. static let schema = "role"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "description") var description: String?
  16. @Siblings(through: mdlXUserRole.self, from: \.$role, to: \.$user) var users: [mdlUser]
  17. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  18. @Field(key: "createUserId") var createuserid: Int?
  19. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  20. @Field(key: "updateUserId") var updateuserid: Int?
  21. init() { }
  22. init(id: Int? = nil, name: String? = nil, description: String? = nil, createuserid: Int? = nil, updateuserid: Int? = nil) {
  23. self.id = id
  24. self.name = name
  25. self.description = description
  26. self.createuserid = createuserid
  27. self.updateuserid = updateuserid
  28. }
  29. }
  30. extension mdlRole{
  31. struct Create: AsyncMigration {
  32. func prepare(on database: Database) async throws {
  33. try await database.schema("role")
  34. .field("id", .int, .identifier(auto: true))
  35. .field("name", .string)
  36. .field("description", .string)
  37. .field("createDate", .datetime)
  38. .field("createUserId", .int)
  39. .field("updateDate", .datetime)
  40. .field("updateUserId", .int)
  41. .create()
  42. }
  43. func revert(on database: Database) async throws {
  44. try await database.schema("role").delete()
  45. }
  46. }
  47. struct Seed: Migration{
  48. func prepare(on database: Database) -> EventLoopFuture<Void> {
  49. let mdls: [mdlRole] = [
  50. .init(id: 1, name: "Administrator", description: nil, createuserid: -1, updateuserid: -1),
  51. .init(id: 2, name: "Gate Keeper", description: nil, createuserid: -1, updateuserid: -1),
  52. .init(id: 3, name: "Preventative Maintenance", description: nil, createuserid: -1, updateuserid: -1),
  53. .init(id: 4, name: "Technician", description: nil, createuserid: -1, updateuserid: -1),
  54. .init(id: 5, name: "Yard Person", description: nil, createuserid: -1, updateuserid: -1),
  55. .init(id: 6, name: "Inspector", description: nil, createuserid: -1, updateuserid: -1),
  56. .init(id: 7, name: "Operator", description: nil, createuserid: -1, updateuserid: -1),
  57. .init(id: 8, name: "Customer", description: nil, createuserid: -1, updateuserid: -1),
  58. .init(id: 9, name: "Sales Person", description: nil, createuserid: -1, updateuserid: -1),
  59. .init(id: 10, name: "Sales Manager", description: nil, createuserid: -1, updateuserid: -1),
  60. .init(id: 11, name: "Parts Manager", description: nil, createuserid: -1, updateuserid: -1),
  61. .init(id: 12, name: "Foreman", description: nil, createuserid: -1, updateuserid: -1),
  62. .init(id: 13, name: "General Admin", description: nil, createuserid: -1, updateuserid: -1)
  63. ]
  64. return mdls.map { mdl in
  65. mdl.save(on: database)
  66. }
  67. .flatten(on: database.eventLoop)
  68. }
  69. func revert(on database: Database) -> EventLoopFuture<Void> {
  70. mdlRole.query(on: database).delete()
  71. }
  72. }
  73. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlRole>{
  74. let query = mdlRole.query(on: req.db)
  75. .with(\.$users)
  76. if let x = req.parameters.get("x"){
  77. let decryptedString = try CustomCrypto.DecryptString(input: x)
  78. let array = decryptedString.components(separatedBy: ":")
  79. ///This call is the standard get by id call
  80. if (array.count == 1){
  81. let id = Int(array[0])!
  82. return query.filter(\.$id == id)
  83. }
  84. ///Optional else if if there are multiple parameters in the call
  85. ///You must include an else for each call that has parameters
  86. ///Need to check the endpoint to get the right filter statements
  87. ///else if(x.count == 2){
  88. /// let username = x[0]
  89. /// let password = x[1]
  90. /// return query.filter(\.$username == username).filter(\.$password == password)
  91. ///}
  92. else{
  93. throw Abort(.badRequest)
  94. }
  95. }
  96. else{
  97. return query
  98. }
  99. }
  100. }