| @@ -22,16 +22,29 @@ struct cntrlUser: RouteCollection { | |||
| // body: .type(mdlUser.self), | |||
| // response: .type(mdlUser.self) | |||
| // ) | |||
| rts.get("e", ":user", ":pwd", use: encrypt) | |||
| rts.post(use: create) | |||
| rts.delete(":x", use: delete) | |||
| } | |||
| func encrypt(req: Request) async throws -> String{ | |||
| let username = req.parameters.get("user") | |||
| let pwd = req.parameters.get("pwd") | |||
| let s = "\(username ?? "mcarman"):\(pwd ?? "password")" | |||
| let es = try CustomCrypto.EncryptString(input: s) | |||
| return es | |||
| } | |||
| func login(req: Request) async throws -> Response{ | |||
| guard let obj = try? await mdlUser.ObjQuery(req: req).first() else { | |||
| throw Abort(.unauthorized, reason: "Username or Password or Both are invalid.") | |||
| } | |||
| guard let response = try? await obj.encodeResponse(status: .ok, for: req) else { | |||
| var array: [Int] = [] | |||
| for wo in obj.workorders{ | |||
| array.append(wo.id!) | |||
| } | |||
| let retObj = mdlUser.Login(id: obj.id, firstName: obj.firstname, lastName: obj.lastname, roles: obj.roles, workorders: array) | |||
| guard let response = try? await retObj.encodeResponse(status: .ok, for: req) else { | |||
| throw Abort(.noContent, reason: "Something went wrong decoding data. User has been authenticated.") | |||
| } | |||
| return response | |||
| @@ -23,10 +23,10 @@ struct cntrlWorkOrder: RouteCollection { | |||
| func index(req: Request) async throws -> Response { | |||
| //let obj = try await mdlWorkOrder.query(on: req.db).all() | |||
| //let obj = try await mdlWorkOrder.ObjQuery(req: req).paginate(for: req) | |||
| guard let obj = try? await mdlWorkOrder.ObjQuery(req: req).paginate(for: req) else { | |||
| throw Abort(.badGateway, reason: "Something went wrong with this api.") | |||
| } | |||
| let obj = try await mdlWorkOrder.ObjQuery(req: req).paginate(for: req) | |||
| // guard let obj = try? await mdlWorkOrder.ObjQuery(req: req).paginate(for: req) else { | |||
| // throw Abort(.badGateway, reason: "Something went wrong with this api.") | |||
| // } | |||
| guard let response = try? await obj.encodeResponse(status: .ok, for: req) else{ | |||
| throw Abort(.noContent, reason: "Something went wrong decoding data. Array could not be shown.") | |||
| } | |||
| @@ -9,6 +9,7 @@ import Fluent | |||
| import Vapor | |||
| import Foundation | |||
| import FluentKit | |||
| import FluentSQL | |||
| final class mdlUser: Model, Content { | |||
| static let schema = "user" | |||
| @@ -36,6 +37,8 @@ final class mdlUser: Model, Content { | |||
| @Timestamp(key: "updateDate", on: .update) var updatedate: Date? | |||
| @Field(key: "updateUserId") var updateuserid: Int? | |||
| init() {} | |||
| init(id: Int? = nil, number: String? = nil, firstname: String? = nil, lastname: String? = nil, username: String? = nil, password: String? = nil, email: String? = nil, isactive: Bool? = nil, createuserid: Int? = nil, updateuserid: Int? = nil, divisionid: Int? = nil, locationid: Int? = nil, gateid: Int? = nil) { | |||
| @@ -58,6 +61,13 @@ final class mdlUser: Model, Content { | |||
| } | |||
| extension mdlUser{ | |||
| struct Login: Content{ | |||
| let id: Int? | |||
| let firstName: String? | |||
| let lastName: String? | |||
| let roles: [mdlRole] | |||
| let workorders: [Int] | |||
| } | |||
| struct Create: AsyncMigration { | |||
| func prepare(on database: Database) async throws { | |||
| try await database.schema("user") | |||
| @@ -83,6 +93,23 @@ extension mdlUser{ | |||
| try await database.schema("user").delete() | |||
| } | |||
| } | |||
| struct ReSeed: AsyncMigration{ | |||
| func prepare(on database: Database) async throws { | |||
| if let db = database as? SQLDatabase{ | |||
| var id = try await mdlUser.query(on: database).max(\.$id) | |||
| id = id! + 1 | |||
| let newSeed = "\(id ?? 1)" | |||
| let txt = "ALTER SEQUENCE public.\"user_id_seq\" RESTART WITH "+newSeed+";" | |||
| let sql = SQLQueryString(txt) | |||
| try await db.raw(sql).run() | |||
| } else{ | |||
| throw Abort(.badRequest) | |||
| } | |||
| } | |||
| func revert(on database: Database) async throws { | |||
| } | |||
| } | |||
| public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlUser>{ | |||
| let query = mdlUser.query(on: req.db) | |||
| @@ -16,7 +16,9 @@ final class mdlInspection: Model, Content { | |||
| @ID(custom: "id", generatedBy: .database) var id: Int? | |||
| @Parent(key: "workOrderSegmentId") var segment: mdlWorkOrderSegment | |||
| //@Parent(key: "workOrderSegmentId") var wosegment: mdlWorkOrderSegment | |||
| @Parent(key: "segmentId") var segment: mdlSegment | |||
| @Parent(key: "workOrderId") var workorder: mdlWorkOrder | |||
| @Parent(key: "inspectionLevelId") var level: mdlInspectionLevel | |||
| @Field(key: "name") var name: String? | |||
| @Field(key: "segment") var seg: String? | |||
| @@ -25,6 +27,7 @@ final class mdlInspection: Model, Content { | |||
| @Children(for: \.$inspection) var categories: [mdlInspectionCategory] | |||
| @Children(for: \.$inspection) var notes: [mdlNote] | |||
| @Timestamp(key: "createDate", on: .create) var createdate: Date? | |||
| @Field(key: "createUserId") var createuserid: Int? | |||
| @Timestamp(key: "updateDate", on: .update) var updatedate: Date? | |||
| @@ -37,6 +40,7 @@ final class mdlInspection: Model, Content { | |||
| interval: String? = nil, | |||
| seg: String? = nil, | |||
| segmentid: Int, | |||
| workorderid: Int, | |||
| levelid: Int, | |||
| createuserid: Int? = nil, | |||
| updateuserid: Int? = nil | |||
| @@ -46,6 +50,7 @@ final class mdlInspection: Model, Content { | |||
| self.interval = interval | |||
| self.seg = seg | |||
| self.$segment.$id.value = segmentid | |||
| self.$workorder.$id.value = workorderid | |||
| self.$level.$id.value = levelid | |||
| self.createuserid = createuserid | |||
| self.updateuserid = updateuserid | |||
| @@ -87,10 +92,65 @@ extension mdlInspection{ | |||
| .update() | |||
| } | |||
| } | |||
| struct Mod2: AsyncMigration{ | |||
| func prepare(on database: Database) async throws { | |||
| try await database.schema("inspection") | |||
| .field("segmentId", .int, .references("segment", "id")) | |||
| .field("workOrderId", .int, .references("workOrder", "id")) | |||
| .update() | |||
| } | |||
| func revert(on database: Database) async throws { | |||
| try await database.schema("inspection") | |||
| .deleteField("segmentId") | |||
| .deleteField("workOrderId") | |||
| .update() | |||
| } | |||
| } | |||
| // struct Update: AsyncMigration{ | |||
| // | |||
| // func prepare(on database: Database) async throws { | |||
| // let mdls = try await mdlInspection.query(on: database).with(\.$wosegment){wos in wos.with(\.$segment).with(\.$workorder)}.all()// have to change to optional parent first so the query will run | |||
| // for mdl in mdls { | |||
| // mdl.$segment.$id.value = mdl.wosegment.segment.id | |||
| // mdl.$workorder.$id.value = mdl.wosegment.workorder.id | |||
| // try await mdl.save(on: database) | |||
| // } | |||
| //// let mdls: [mdlSignatureType] = [ | |||
| //// .init(id: 1, name: "Technician", description: nil, isactive: true, isrequired: true, createuserid: -1, updateuserid: -1), | |||
| //// .init(id: 2, name: "Customer", description: nil, isactive: true, isrequired: false, createuserid: -1, updateuserid: -1) | |||
| //// ] | |||
| //// return mdls.map { mdl in | |||
| //// mdl.save(on: database) | |||
| //// } | |||
| //// .flatten(on: database.eventLoop) | |||
| // } | |||
| // | |||
| // func revert(on database: Database) async throws { | |||
| // try await mdlInspection.query(on: database).delete() | |||
| // } | |||
| // | |||
| // | |||
| // } | |||
| struct Mod3: AsyncMigration{ | |||
| func prepare(on database: Database) async throws { | |||
| try await database.schema("inspection") | |||
| .deleteField("workOrderSegmentId") | |||
| .update() | |||
| } | |||
| func revert(on database: Database) async throws { | |||
| try await database.schema("inspection") | |||
| .field("workOrderSegmentId", .int, .references("workOrderSegment", "id")) | |||
| .update() | |||
| } | |||
| } | |||
| public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspection>{ | |||
| let query = mdlInspection.query(on: req.db) | |||
| .with(\.$segment) | |||
| .with(\.$workorder) | |||
| .with(\.$categories) | |||
| .with(\.$level) | |||
| .with(\.$notes) | |||
| @@ -19,6 +19,7 @@ final class mdlInspectionCategory: Model, Content { | |||
| @Parent(key: "inspectionId") var inspection: mdlInspection | |||
| @Parent(key: "categoryId") var category: mdlCategory | |||
| @Field(key: "summary") var summary: String? | |||
| @Field(key: "sortOrder") var sortorder: Int? | |||
| @Children(for: \.$category) var inspectionitems: [mdlInspectionItem] | |||
| @@ -33,6 +34,7 @@ final class mdlInspectionCategory: Model, Content { | |||
| inspectionid: Int, | |||
| categoryid: Int, | |||
| summary: String? = nil, | |||
| sortorder: Int? = 0, | |||
| createuserid: Int? = nil, | |||
| updateuserid: Int? = nil | |||
| ) { | |||
| @@ -40,6 +42,7 @@ final class mdlInspectionCategory: Model, Content { | |||
| self.$inspection.$id.value = inspectionid | |||
| self.$category.$id.value = categoryid | |||
| self.summary = summary | |||
| self.sortorder = sortorder | |||
| self.createuserid = createuserid | |||
| self.updateuserid = updateuserid | |||
| } | |||
| @@ -63,6 +66,18 @@ extension mdlInspectionCategory{ | |||
| try await database.schema("inspectionCategory").delete() | |||
| } | |||
| } | |||
| struct Mod1: AsyncMigration{ | |||
| func prepare(on database: Database) async throws { | |||
| try await database.schema("inspectionCategory") | |||
| .field("sortOrder", .int) | |||
| .update() | |||
| } | |||
| func revert(on database: Database) async throws { | |||
| try await database.schema("inspectionCategory") | |||
| .deleteField("sortOrder") | |||
| .update() | |||
| } | |||
| } | |||
| public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlInspectionCategory>{ | |||
| let query = mdlInspectionCategory.query(on: req.db) | |||
| @@ -54,7 +54,7 @@ extension mdlInspectionLevel{ | |||
| .field("id", .int, .identifier(auto: true)) | |||
| .field("name", .string) | |||
| .field("description", .string) | |||
| .field("isactive", .bool) | |||
| .field("isActive", .bool) | |||
| .field("inspectionTypeId", .int) | |||
| .field("createDate", .datetime) | |||
| .field("createUserId", .int) | |||
| @@ -36,9 +36,13 @@ final class mdlWorkOrder: Model, Content { | |||
| @Siblings(through: mdlWorkOrderAttachment.self, from: \.$workorder, to: \.$equipment) var attachments: [mdlEquipment] | |||
| @Children(for: \.$workorder) var signatures: [mdlSignature] | |||
| @Children(for: \.$workorder) var notes: [mdlNote] | |||
| @Children(for: \.$workorder) var segments: [mdlWorkOrderSegment] | |||
| //@Children(for: \.$workorder) var segments: [mdlWorkOrderSegment] | |||
| @Children(for: \.$workorder) var inspections: [mdlInspection] | |||
| @Timestamp(key: "createDate", on: .create) var createdate: Date? | |||
| @Field(key: "createUserId") var createuserid: Int? | |||
| @@ -166,7 +170,18 @@ extension mdlWorkOrder{ | |||
| .with(\.$attachments) | |||
| .with(\.$signatures) | |||
| .with(\.$notes) | |||
| .with(\.$segments){ segment in segment.with(\.$inspections)} | |||
| .with(\.$inspections){ | |||
| ins in ins | |||
| .with(\.$level){ | |||
| l in l.with(\.$inspectiontype) | |||
| } | |||
| .with(\.$segment) | |||
| .with(\.$categories){ | |||
| c in c.with(\.$inspectionitems) | |||
| .with(\.$category) | |||
| } | |||
| } | |||
| //.with(\.$segments){ segment in segment.with(\.$inspections)} | |||
| if let x = req.parameters.get("x"){ | |||
| let decryptedString = try CustomCrypto.DecryptString(input: x) | |||
| @@ -12,6 +12,7 @@ import Foundation | |||
| import FluentKit | |||
| final class mdlWorkOrderSegment: Model, Content { | |||
| static let schema = "workOrderSegment" | |||
| @ID(custom: "id", generatedBy: .database) var id: Int? | |||
| @@ -19,10 +20,10 @@ final class mdlWorkOrderSegment: Model, Content { | |||
| @Parent(key: "workOrderId") var workorder: mdlWorkOrder | |||
| @Parent(key: "segmentId") var segment: mdlSegment | |||
| @OptionalParent(key: "inspectionId") var inspection: mdlInspection? | |||
| @Children(for: \.$segment) var parts: [mdlWorkOrderPart] | |||
| @Children(for: \.$segment) var inspections: [mdlInspection] | |||
| //@Children(for: \.$segment) var inspections: [mdlInspection] | |||
| @Children(for: \.$segment) var samples: [mdlOilSample] | |||
| @Children(for: \.$segment) var consumptions: [mdlConsumption] | |||
| @@ -85,11 +86,23 @@ extension mdlWorkOrderSegment{ | |||
| .update() | |||
| } | |||
| } | |||
| struct Mod2: AsyncMigration { | |||
| func prepare(on database: Database) async throws { | |||
| try await database.schema("workOrderSegment") | |||
| .field("inspectionId", .int, .references("inspection", "id")) | |||
| .update() | |||
| } | |||
| func revert(on database: Database) async throws { | |||
| try await database.schema("workOrderSegment") | |||
| .deleteField("inspectionId") | |||
| .update() | |||
| } | |||
| } | |||
| public static func ObjQuery(req: Request) async throws -> QueryBuilder<mdlWorkOrderSegment>{ | |||
| let query = mdlWorkOrderSegment.query(on: req.db) | |||
| .with(\.$parts) | |||
| .with(\.$inspections) | |||
| //.with(\.$inspections) | |||
| .with(\.$samples) | |||
| .with(\.$consumptions) | |||
| @@ -154,6 +154,11 @@ public func configure(_ app: Application) async throws { | |||
| app.migrations.add(mdlInspection.Mod1()) | |||
| app.migrations.add(mdlWorkOrder.Mod2()) | |||
| app.migrations.add(mdlSignature.Mode1()) | |||
| app.migrations.add(mdlWorkOrderSegment.Mod2()) | |||
| app.migrations.add(mdlInspection.Mod2()) | |||
| //app.migrations.add(mdlInspection.Update()) | |||
| app.migrations.add(mdlInspection.Mod3()) | |||
| app.migrations.add(mdlInspectionCategory.Mod1()) | |||
| app.logger.logLevel = .debug | |||
| @@ -12,7 +12,7 @@ final class UserTests: XCTestCase { | |||
| let fn = "Mike" | |||
| let ln = "Carman" | |||
| let email = "mcarman@rpmindustries.com" | |||
| let username = "bryckman" | |||
| let username = "jrob" | |||
| let password = "password" | |||
| let isactive = true | |||
| let cuserid = -1 | |||