您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

95 行
3.2 KiB

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