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.

115 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. @Timestamp(key: "createDate", on: .create) var createdate: Date?
  19. @Field(key: "createUserId") var createuserid: Int?
  20. @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
  21. @Field(key: "updateUserId") var updateuserid: Int?
  22. init() { }
  23. init(id: Int? = nil, name: String? = nil, code: String? = nil, description: String? = nil, isactive: Bool? = true, createuserid: Int? = nil, updateuserid: Int? = nil) {
  24. self.id = id
  25. self.name = name
  26. self.code = code
  27. self.description = description
  28. self.isactive = isactive
  29. self.createuserid = createuserid
  30. self.updateuserid = updateuserid
  31. }
  32. }
  33. extension mdlSegment{
  34. struct Create: AsyncMigration {
  35. func prepare(on database: Database) async throws {
  36. try await database.schema("segment")
  37. .field("id", .int, .identifier(auto: true))
  38. .field("name", .string)
  39. .field("code", .string)
  40. .field("description", .string)
  41. .field("isActive", .bool)
  42. .field("createDate", .datetime)
  43. .field("createUserId", .int)
  44. .field("updateDate", .datetime)
  45. .field("updateUserId", .int)
  46. .create()
  47. }
  48. func revert(on database: Database) async throws {
  49. try await database.schema("segment").delete()
  50. }
  51. }
  52. struct Seed: Migration{
  53. func prepare(on database: Database) -> EventLoopFuture<Void> {
  54. let mdls: [mdlSegment] = [
  55. .init(id: nil, name: "Inspection", code: "0001", description: nil, createuserid: -1, updateuserid: -1),
  56. .init(id: nil, name: "Oil Sample", code: "0002", description: nil, createuserid: -1, updateuserid: -1),
  57. .init(id: nil, name: "Consumption", code: "0003", description: nil, createuserid: -1, updateuserid: -1),
  58. .init(id: nil, name: "Part", code: "0004", description: nil, createuserid: -1, updateuserid: -1)
  59. ]
  60. return mdls.map { mdl in
  61. mdl.save(on: database)
  62. }
  63. .flatten(on: database.eventLoop)
  64. }
  65. func revert(on database: Database) -> EventLoopFuture<Void> {
  66. mdlSegment.query(on: database).delete()
  67. }
  68. }
  69. public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlSegment>{
  70. let query = mdlSegment.query(on: req.db)
  71. if let x = req.parameters.get("x"){
  72. let dataQuery = req.parameters.get("x")
  73. let decryptedString = try CustomCrypto.DecryptString(input: dataQuery!)
  74. let x = decryptedString.components(separatedBy: ":")
  75. ///This call is the standard get by id call
  76. if (x.count == 1){
  77. let id = Int(x[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. }