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.

82 lines
2.1 KiB

  1. //
  2. // Role.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 10/24/21.
  6. //
  7. import Foundation
  8. import Vapor
  9. import Fluent
  10. import FluentPostgresDriver
  11. final class mdlRole: Model, Content{
  12. struct Input: Content{
  13. let id: Int?
  14. let name: String
  15. let description: String
  16. let users: [mdlUser]?
  17. }
  18. struct Output: Content{
  19. let id: Int?
  20. let name: String
  21. let description: String
  22. let users: [mdlUser]?
  23. }
  24. static let schema = "Role"
  25. @ID(custom: "id", generatedBy: .database) var id: Int?
  26. @Field(key: "name") var name: String
  27. @Field(key: "description") var description: String
  28. @Siblings(through: mdlXUserRole.self, from: \.$role, to: \.$user) public var users: [mdlUser]
  29. init(){}
  30. init(id: Int? = nil){
  31. self.id = id
  32. }
  33. init(id: Int? = nil, name: String, description: String, users: [mdlUser]?){
  34. self.id = id
  35. self.name = name
  36. self.description = description
  37. self.$users.value = users
  38. }
  39. }
  40. extension mdlRole {
  41. struct create: Migration{
  42. func prepare(on database: Database) -> EventLoopFuture<Void> {
  43. return database.schema("Role")
  44. .field("id", .int, .identifier(auto: true))
  45. .field("name", .string)
  46. .field("description", .string)
  47. .create()
  48. }
  49. func revert(on database: Database) -> EventLoopFuture<Void> {
  50. database.schema("Role").delete()
  51. }
  52. }
  53. struct seed: Migration {
  54. func prepare(on database: Database) -> EventLoopFuture<Void> {
  55. // 1
  56. let array: [mdlRole] = [
  57. .init(id: -1, name: "SuperAdmin", description: "System User Account", users: [])
  58. ]
  59. // 2
  60. return array.map { obj in
  61. obj.save(on: database)
  62. }
  63. .flatten(on: database.eventLoop)
  64. }
  65. func revert(on database: Database) -> EventLoopFuture<Void> {
  66. // 3
  67. mdlRole.query(on: database).delete()
  68. }
  69. }
  70. }