25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 

97 satır
3.3 KiB

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