Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

126 linhas
4.3 KiB

  1. //
  2. // mdlConsumption.swift
  3. //
  4. //
  5. // Created by Michiel Carman on 2/16/24.
  6. //
  7. import Fluent
  8. import Vapor
  9. import Foundation
  10. import FluentKit
  11. final class mdlConsumption: Model, Content {
  12. static let schema = "consumption"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Parent(key: "workOrderSegmentId") var segment: mdlWorkOrderSegment
  15. @Parent(key: "fluidId") var fluid: mdlFluid
  16. @Parent(key: "viscosityId") var viscosity: mdlViscosity
  17. @Field(key: "quantity") var quantity: Float?
  18. @Field(key: "sortOrder") var sortorder: Int?
  19. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  20. @Field(key: "createUserId") var createuserid: Int?
  21. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  22. @Field(key: "updateUserId") var updateuserid: Int?
  23. init() { }
  24. init(id: Int? = nil,
  25. segmentid: Int,
  26. fluidid: Int,
  27. viscosityid: Int,
  28. quantity: Float? = nil,
  29. sortorder: Int? = nil,
  30. createuserid: Int? = nil,
  31. updateuserid: Int? = nil
  32. ) {
  33. self.id = id
  34. self.$segment.$id.value = segmentid
  35. self.$fluid.$id.value = fluidid
  36. self.$viscosity.$id.value = viscosityid
  37. self.quantity = quantity
  38. self.sortorder = sortorder
  39. self.createuserid = createuserid
  40. self.updateuserid = updateuserid
  41. }
  42. }
  43. extension mdlConsumption{
  44. struct Create: AsyncMigration {
  45. func prepare(on database: Database) async throws {
  46. try await database.schema("consumption")
  47. .field("id", .int, .identifier(auto: true))
  48. .field("workOrderSegmentId", .int, .references("workOrderSegment", "id"))
  49. .field("fluid", .int, .references("fluid", "id"))
  50. .field("viscosity", .int, .references("viscosity", "id"))
  51. .field("quantity", .float)
  52. .field("sortOrder", .int)
  53. .field("createDate", .datetime)
  54. .field("createUserId", .int)
  55. .field("updateDate", .datetime)
  56. .field("updateUserId", .int)
  57. .create()
  58. }
  59. func revert(on database: Database) async throws {
  60. try await database.schema("consumption").delete()
  61. }
  62. }
  63. struct Mod1: AsyncMigration {
  64. func prepare(on database: Database) async throws {
  65. try await database.schema("consumption")
  66. .deleteField("fluid")
  67. .deleteField("viscosity")
  68. .field("fluidId", .int, .references("fluid", "id"))
  69. .field("viscosityId", .int, .references("viscosity", "id"))
  70. .update()
  71. }
  72. func revert(on database: Database) async throws {
  73. try await database.schema("consumption")
  74. .deleteField("fluidId")
  75. .deleteField("viscosityId")
  76. .field("fluid", .int, .references("fluid", "id"))
  77. .field("viscosity", .int, .references("viscosity", "id"))
  78. .update()
  79. }
  80. }
  81. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlConsumption>{
  82. let query = mdlConsumption.query(on: req.db)
  83. .with(\.$segment)
  84. .with(\.$fluid)
  85. .with(\.$viscosity)
  86. if let x = req.parameters.get("x"){
  87. let decryptedString = try CustomCrypto.DecryptString(input: x)
  88. let array = decryptedString.components(separatedBy: ":")
  89. ///This call is the standard get by id call
  90. if (array.count == 1){
  91. let id = Int(array[0])!
  92. return query.filter(\.$id == id)
  93. }
  94. ///Optional else if if there are multiple parameters in the call
  95. ///You must include an else for each call that has parameters
  96. ///Need to check the endpoint to get the right filter statements
  97. ///else if(x.count == 2){
  98. /// let username = x[0]
  99. /// let password = x[1]
  100. /// return query.filter(\.$username == username).filter(\.$password == password)
  101. ///}
  102. else{
  103. throw Abort(.badRequest)
  104. }
  105. }
  106. else{
  107. return query
  108. }
  109. }
  110. }