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.
 
 

98 regels
3.1 KiB

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