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

106 строки
3.4 KiB

  1. //
  2. // mdlModel.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 mdlModel: Model, Content {
  12. static let schema = "model"
  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. @Parent(key: "productId") var product: mdlProduct
  18. @Parent(key: "makeId") var make: mdlMake
  19. @Children(for: \.$model) var templates: [mdlTemplate]
  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, productid: Int? = nil, makeid: Int? = nil) {
  26. self.id = id
  27. self.name = name
  28. self.description = description
  29. self.isactive = isactive
  30. self.$product.$id.value = productid!
  31. self.$make.$id.value = makeid!
  32. self.createuserid = createuserid
  33. self.updateuserid = updateuserid
  34. }
  35. }
  36. extension mdlModel{
  37. struct Create: AsyncMigration {
  38. func prepare(on database: Database) async throws {
  39. try await database.schema("model")
  40. .field("id", .int, .identifier(auto: true))
  41. .field("name", .string)
  42. .field("description", .string)
  43. .field("productId", .int, .references("product", "id"))
  44. .field("makeId", .int, .references("make", "id"))
  45. .field("isActive", .bool)
  46. .field("createDate", .datetime)
  47. .field("createUserId", .int)
  48. .field("updateDate", .datetime)
  49. .field("updateUserId", .int)
  50. .create()
  51. }
  52. func revert(on database: Database) async throws {
  53. try await database.schema("model").delete()
  54. }
  55. }
  56. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlModel>{
  57. let query = mdlModel.query(on: req.db)
  58. .with(\.$product)
  59. .with(\.$make)
  60. .with(\.$templates)
  61. if let x = req.parameters.get("x"){
  62. let decryptedString = try CustomCrypto.DecryptString(input: x)
  63. let array = decryptedString.components(separatedBy: ":")
  64. ///This call is the standard get by id call
  65. if (array.count == 1){
  66. let id = Int(array[0])!
  67. return query.filter(\.$id == id)
  68. }
  69. ///Optional else if if there are multiple parameters in the call
  70. ///You must include an else for each call that has parameters
  71. ///Need to check the endpoint to get the right filter statements
  72. ///else if(x.count == 2){
  73. /// let username = x[0]
  74. /// let password = x[1]
  75. /// return query.filter(\.$username == username).filter(\.$password == password)
  76. ///}
  77. else{
  78. throw Abort(.badRequest)
  79. }
  80. }
  81. else{
  82. return query
  83. }
  84. }
  85. }