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.

116 lines
4.0 KiB

  1. //
  2. // mdlSegment.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 mdlSegment: Model, Content {
  12. static let schema = "segment"
  13. @ID(custom: "id", generatedBy: .database) var id: Int?
  14. @Field(key: "name") var name: String?
  15. @Field(key: "code") var code: String?
  16. @Field(key: "description") var description: String?
  17. @Field(key: "isActive") var isactive: Bool?
  18. @Children(for: \.$segment) var workordersegments: [mdlWorkOrderSegment]
  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, code: String? = nil, description: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil) {
  25. self.id = id
  26. self.name = name
  27. self.code = code
  28. self.description = description
  29. self.isactive = isactive
  30. self.createuserid = createuserid
  31. self.updateuserid = updateuserid
  32. }
  33. }
  34. extension mdlSegment{
  35. struct Create: AsyncMigration {
  36. func prepare(on database: Database) async throws {
  37. try await database.schema("segment")
  38. .field("id", .int, .identifier(auto: true))
  39. .field("name", .string)
  40. .field("code", .string)
  41. .field("description", .string)
  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("segment").delete()
  51. }
  52. }
  53. struct Seed: Migration{
  54. func prepare(on database: Database) -> EventLoopFuture<Void> {
  55. let mdls: [mdlSegment] = [
  56. .init(id: nil, name: "Inspection", code: "0001", description: nil, createuserid: -1, updateuserid: -1),
  57. .init(id: nil, name: "Oil Sample", code: "0002", description: nil, createuserid: -1, updateuserid: -1),
  58. .init(id: nil, name: "Consumption", code: "0003", description: nil, createuserid: -1, updateuserid: -1),
  59. .init(id: nil, name: "Part", code: "0004", description: nil, createuserid: -1, updateuserid: -1)
  60. ]
  61. return mdls.map { mdl in
  62. mdl.save(on: database)
  63. }
  64. .flatten(on: database.eventLoop)
  65. }
  66. func revert(on database: Database) -> EventLoopFuture<Void> {
  67. mdlSegment.query(on: database).delete()
  68. }
  69. }
  70. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlSegment>{
  71. let query = mdlSegment.query(on: req.db)
  72. if let x = req.parameters.get("x"){
  73. let decryptedString = try CustomCrypto.DecryptString(input: x)
  74. let array = decryptedString.components(separatedBy: ":")
  75. ///This call is the standard get by id call
  76. if (array.count == 1){
  77. let id = Int(array[0])!
  78. return query.filter(\.$id == id)
  79. }
  80. ///Optional else if if there are multiple parameters in the call
  81. ///You must include an else for each call that has parameters
  82. ///Need to check the endpoint to get the right filter statements
  83. ///else if(x.count == 2){
  84. /// let username = x[0]
  85. /// let password = x[1]
  86. /// return query.filter(\.$username == username).filter(\.$password == password)
  87. ///}
  88. else{
  89. throw Abort(.badRequest)
  90. }
  91. }
  92. else{
  93. return query
  94. }
  95. }
  96. }