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.
 
 

102 line
3.2 KiB

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