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

117 lines
4.1 KiB

  1. //
  2. // mdlStatus.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 mdlStatus: Model, Content {
  12. static let schema = "status"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "description") var description: String?
  16. @Children(for: \.$status) var workorders: [mdlWorkOrder]
  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 mdlStatus{
  31. struct Create: AsyncMigration {
  32. func prepare(on database: Database) async throws {
  33. try await database.schema("status")
  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("status").delete()
  45. }
  46. }
  47. struct Seed: Migration{
  48. func prepare(on database: Database) -> EventLoopFuture<Void> {
  49. let mdls: [mdlStatus] = [
  50. .init(id: 1, name: "Open", description: nil, createuserid: -1, updateuserid: -1),
  51. .init(id: 2, name: "Synced", description: nil, createuserid: -1, updateuserid: -1),
  52. .init(id: 3, name: "Completed", description: nil, createuserid: -1, updateuserid: -1),
  53. .init(id: 4, name: "Approved", description: nil, createuserid: -1, updateuserid: -1),
  54. .init(id: 5, name: "Incomplete", description: nil, createuserid: -1, updateuserid: -1),
  55. .init(id: 6, name: "Reassigned", description: nil, createuserid: -1, updateuserid: -1),
  56. .init(id: 7, name: "Pending", description: nil, createuserid: -1, updateuserid: -1),
  57. .init(id: 8, name: "Deleted", description: nil, createuserid: -1, updateuserid: -1)
  58. ]
  59. return mdls.map { mdl in
  60. mdl.save(on: database)
  61. }
  62. .flatten(on: database.eventLoop)
  63. }
  64. func revert(on database: Database) -> EventLoopFuture<Void> {
  65. mdlStatus.query(on: database).delete()
  66. }
  67. }
  68. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlStatus>{
  69. let query = mdlStatus.query(on: req.db)
  70. .with(\.$workorders)
  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. }