diff --git a/Sources/App/Controllers/AuthControllers/cntrlUser.swift b/Sources/App/Controllers/AuthControllers/cntrlUser.swift index 14fa09a..14061ad 100644 --- a/Sources/App/Controllers/AuthControllers/cntrlUser.swift +++ b/Sources/App/Controllers/AuthControllers/cntrlUser.swift @@ -15,7 +15,7 @@ struct cntrlUser: RouteCollection { func boot(routes: RoutesBuilder) throws { let rts = routes.grouped("users") rts.get(use: index) - rts.get(":credentials", use: login) + rts.get("login", ":credentials", use: login) rts.post(use: create) rts.put(use: update) rts.group(":id") { todo in @@ -43,7 +43,7 @@ struct cntrlUser: RouteCollection { throw Abort(.badRequest) } let data = Data(base64Encoded: dataQuery) - let encryptionKey = SymmetricKey(size: .bits256) + let encryptionKey = SymmetricKey(data: "254595778216A74E21211ABCA7AC9".data(using: .utf8)!) let sealedBox = try AES.GCM.SealedBox(combined: data!) let decryptedData = try AES.GCM.open(sealedBox, using: encryptionKey) guard let decryptedString = String(data: decryptedData, encoding: .utf8) else{ diff --git a/Sources/App/Controllers/cntrlDivision.swift b/Sources/App/Controllers/cntrlDivision.swift index 98ce263..3377595 100644 --- a/Sources/App/Controllers/cntrlDivision.swift +++ b/Sources/App/Controllers/cntrlDivision.swift @@ -1,8 +1,49 @@ // -// File.swift -// +// Division.swift +// // // Created by Michiel Carman on 4/1/24. // -import Foundation +import Fluent +import Vapor + +struct cntrlDivision: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("divisions") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlDivision] { + try await mdlDivision.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlDivision { + let obj = try req.content.decode(mdlDivision.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlDivision{ + let obj = try req.content.decode(mdlDivision.self) + let dbobj = try await mdlDivision.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlDivision.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/cntrlGate.swift b/Sources/App/Controllers/cntrlGate.swift index 98ce263..2a79915 100644 --- a/Sources/App/Controllers/cntrlGate.swift +++ b/Sources/App/Controllers/cntrlGate.swift @@ -1,8 +1,49 @@ // -// File.swift -// +// Gate.swift +// // // Created by Michiel Carman on 4/1/24. // -import Foundation +import Fluent +import Vapor + +struct cntrlGate: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("gates") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlGate] { + try await mdlGate.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlGate { + let obj = try req.content.decode(mdlGate.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlGate{ + let obj = try req.content.decode(mdlGate.self) + let dbobj = try await mdlGate.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlGate.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/cntrlLocation.swift b/Sources/App/Controllers/cntrlLocation.swift index 98ce263..6e6df12 100644 --- a/Sources/App/Controllers/cntrlLocation.swift +++ b/Sources/App/Controllers/cntrlLocation.swift @@ -1,8 +1,49 @@ // -// File.swift -// +// Location.swift +// // // Created by Michiel Carman on 4/1/24. // -import Foundation +import Fluent +import Vapor + +struct cntrlLocation: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("locations") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlLocation] { + try await mdlLocation.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlLocation { + let obj = try req.content.decode(mdlLocation.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlLocation{ + let obj = try req.content.decode(mdlLocation.self) + let dbobj = try await mdlLocation.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlLocation.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/cntrlResponseType.swift b/Sources/App/Controllers/cntrlResponseType.swift index a0490df..0705e56 100644 --- a/Sources/App/Controllers/cntrlResponseType.swift +++ b/Sources/App/Controllers/cntrlResponseType.swift @@ -1,8 +1,49 @@ // -// File.swift -// +// cntrlResponseType.swift // -// Created by Michiel Carman on 4/2/24. // +// Created by Michiel Carman on 4/1/24. +// + +import Fluent +import Vapor -import Foundation +struct cntrlResponseType: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("responsetypes") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlResponseType] { + try await mdlResponseType.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlResponseType { + let obj = try req.content.decode(mdlResponseType.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlResponseType{ + let obj = try req.content.decode(mdlResponseType.self) + let dbobj = try await mdlResponseType.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlResponseType.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/cntrlUnit.swift b/Sources/App/Controllers/cntrlUnit.swift index a0490df..6ebea9a 100644 --- a/Sources/App/Controllers/cntrlUnit.swift +++ b/Sources/App/Controllers/cntrlUnit.swift @@ -1,8 +1,49 @@ // -// File.swift -// +// cntrlUnit.swift // -// Created by Michiel Carman on 4/2/24. // +// Created by Michiel Carman on 4/1/24. +// + +import Fluent +import Vapor -import Foundation +struct cntrlUnit: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("units") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlUnit] { + try await mdlUnit.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlUnit { + let obj = try req.content.decode(mdlUnit.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlUnit{ + let obj = try req.content.decode(mdlUnit.self) + let dbobj = try await mdlUnit.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlUnit.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 70dd69e..538f715 100644 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -1,5 +1,13 @@ import Fluent import Vapor +import Crypto + + +let + encryptionKey = +SymmetricKey(data: +Environment.get("ENCRYPTION_KEY")!.data(using: + .utf8)!) func routes(_ app: Application) throws { app.routes.caseInsensitive = true @@ -13,5 +21,12 @@ func routes(_ app: Application) throws { try baseApp.register(collection: cntrlDataTypes()) try baseApp.register(collection: cntrlRole()) try baseApp.register(collection: cntrlUser()) + try baseApp.register(collection: cntrlLocation()) + try baseApp.register(collection: cntrlDivision()) + try baseApp.register(collection: cntrlGate()) + try baseApp.register(collection: cntrlSegment()) + try baseApp.register(collection: cntrlResponseType()) + try baseApp.register(collection: cntrlUnit()) + }