25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

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