Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 

87 righe
2.9 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. @Siblings(through: mdlXWorkOrderSegment.self, from: \.$segment, to: \.$workorder) var workorders: [mdlWorkOrder]
  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. }