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.

99 lines
3.1 KiB

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