Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

96 строки
3.0 KiB

  1. //
  2. // mdlLevel.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 mdlLevel: Model, Content {
  12. static let schema = "level"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "description") var description: String?
  16. @Field(key: "sortOrder") var sortorder: Int?
  17. @Field(key: "isActive") var isactive: Bool?
  18. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  19. @Field(key: "createUserId") var createuserid: Int?
  20. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  21. @Field(key: "updateUserId") var updateuserid: Int?
  22. init() { }
  23. init(id: Int? = nil, name: String? = nil, description: String? = nil, sortorder: Int? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil) {
  24. self.id = id
  25. self.name = name
  26. self.description = description
  27. self.sortorder = sortorder
  28. self.isactive = isactive
  29. self.createuserid = createuserid
  30. self.updateuserid = updateuserid
  31. }
  32. }
  33. extension mdlLevel{
  34. struct Create: AsyncMigration {
  35. func prepare(on database: Database) async throws {
  36. try await database.schema("level")
  37. .field("id", .int, .identifier(auto: true))
  38. .field("name", .string)
  39. .field("description", .string)
  40. .field("sortOrder", .int)
  41. .field("isActive", .bool)
  42. .field("createDate", .datetime)
  43. .field("createUserId", .int)
  44. .field("updateDate", .datetime)
  45. .field("updateUserId", .int)
  46. .create()
  47. }
  48. func revert(on database: Database) async throws {
  49. try await database.schema("level").delete()
  50. }
  51. }
  52. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlLevel>{
  53. let query = mdlLevel.query(on: req.db)
  54. if let x = req.parameters.get("x"){
  55. let decryptedString = try CustomCrypto.DecryptString(input: x)
  56. let array = decryptedString.components(separatedBy: ":")
  57. ///This call is the standard get by id call
  58. if (array.count == 1){
  59. let id = Int(array[0])!
  60. return query.filter(\.$id == id)
  61. }
  62. ///Optional else if if there are multiple parameters in the call
  63. ///You must include an else for each call that has parameters
  64. ///Need to check the endpoint to get the right filter statements
  65. ///else if(x.count == 2){
  66. /// let username = x[0]
  67. /// let password = x[1]
  68. /// return query.filter(\.$username == username).filter(\.$password == password)
  69. ///}
  70. else{
  71. throw Abort(.badRequest)
  72. }
  73. }
  74. else{
  75. return query
  76. }
  77. }
  78. }