|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- //
- // Unit.swift
- //
- //
- // Created by Michiel Carman on 10/24/21.
- //
-
- import Foundation
- import Vapor
- import Fluent
- import FluentPostgresDriver
-
- final class mdlUnit: Model, Content{
- struct Input: Content{
- let id: Int?
- let name: String
- let description: String
- let isactive: Bool
- let createdate: Date?
- let createuserid: Int?
- let updatedate: Date?
- let updateuserid: Int?
- let datatype: mdlDataType
- let locations: [mdlLocation]?
- let inspectioncategoryitemspecitems: [mdlInspectionCategoryItemSpecItem]?
- }
-
- struct Output: Content{
- let id: Int?
- let name: String
- let description: String
- let isactive: Bool
- let createdate: Date?
- let createuserid: Int?
- let updatedate: Date?
- let updateuserid: Int?
- let datatype: mdlDataType
- let locations: [mdlLocation]?
- let inspectioncategoryitemspecitems: [mdlInspectionCategoryItemSpecItem]?
- }
-
- static let schema = "Unit"
-
- @ID(custom: "id", generatedBy: .database) var id: Int?
- @Field(key: "name") var name: String
- @Field(key: "description") var description: String
- @Field(key: "isActive") var isactive: Bool
- @Field(key: "createUserId" ) var createuserid: Int?
- @Timestamp(key: "createDate", on: .create) var createdate: Date?
- @Field(key: "updateUserId") var updateuserid: Int?
- @Timestamp(key: "updateDate", on: .update) var updatedate: Date?
-
- @Parent(key: "dataTypeId") var datatype: mdlDataType
-
- @Children(for: \.$unit) var locations: [mdlLocation]
- @Children(for: \.$unit) var inspectioncategoryitemspecitems: [mdlInspectionCategoryItemSpecItem]
-
- init(){}
-
- init(id: Int? = nil){
-
- self.id = id
- }
-
- init(id: Int? = nil, name: String, description: String, isactive: Bool = true, createuserid: Int? = -1, createdate: Date? = nil, updateuserid: Int? = -1, updatedate: Date? = nil, datatype: mdlDataType, locations: [mdlLocation]?, inspectioncategoryitemspecitems: [mdlInspectionCategoryItemSpecItem]?){
- self.id = id
- self.name = name
- self.description = description
- self.isactive = isactive
- self.createuserid = createuserid
- self.createdate = createdate
- self.updateuserid = updateuserid
- self.updatedate = updatedate
- self.$datatype.id = datatype.id!
- self.$locations.value = locations
- self.$inspectioncategoryitemspecitems.value = inspectioncategoryitemspecitems
- }
- }
-
- extension mdlUnit {
- struct create: Migration{
- func prepare(on database: Database) -> EventLoopFuture<Void> {
- return database.schema("Unit")
- .field("id", .int, .identifier(auto: true))
- .field("name", .string)
- .field("description", .string)
- .field("isActive", .bool)
- .field("dataTypeId", .int, .required, .references("DataType", "id"))
- .field("createUserId", .int)
- .field("createDate", .datetime)
- .field("updateUserId", .int)
- .field("updateDate", .datetime)
- .create()
- }
- func revert(on database: Database) -> EventLoopFuture<Void> {
- database.schema("Unit").delete()
- }
- }
- struct seed: Migration {
- func prepare(on database: Database) -> EventLoopFuture<Void> {
- // 1
- let array: [mdlUnit] = [
- .init(name: "in", description: "Inches", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Text", description: "Unit type for text based responses", datatype: mdlDataType(id: 1), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "sec", description: "Seconds", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "A", description: "Amps", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "hrs", description: "Hours", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Date", description: "DateTime", datatype: mdlDataType(id: 4), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Bool", description: "Boolean", datatype: mdlDataType(id: 5), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "String", description: "String", datatype: mdlDataType(id: 1), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Integer", description: "Integer", datatype: mdlDataType(id: 2), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Decimal", description: "Decimal", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Gallons", description: "Gallons", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Liters", description: "Liters", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "mm", description: "millimeters", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "32nds", description: "32nds", datatype: mdlDataType(id: 2), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "R.P.M.", description: "Revolutions Per Minute", datatype: mdlDataType(id: 2), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "PSI", description: "Pounds per Square Inch", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "in^3", description: "Cubic Inches", datatype: mdlDataType(id: 3), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "Amps", description: "Amperage", datatype: mdlDataType(id: 2), locations: [], inspectioncategoryitemspecitems: []),
- .init(name: "%", description: "PERCENTAGE", datatype: mdlDataType(id: 2), locations: [], inspectioncategoryitemspecitems: [])
- ]
- // 2
- return array.map { obj in
- obj.save(on: database)
- }
- .flatten(on: database.eventLoop)
- }
-
- func revert(on database: Database) -> EventLoopFuture<Void> {
- // 3
- mdlUnit.query(on: database).delete()
- }
- }
- }
|