diff --git a/.swiftpm/xcode/package.xcworkspace/xcuserdata/mcarman.xcuserdatad/UserInterfaceState.xcuserstate b/.swiftpm/xcode/package.xcworkspace/xcuserdata/mcarman.xcuserdatad/UserInterfaceState.xcuserstate index 947fea3..3691b57 100644 Binary files a/.swiftpm/xcode/package.xcworkspace/xcuserdata/mcarman.xcuserdatad/UserInterfaceState.xcuserstate and b/.swiftpm/xcode/package.xcworkspace/xcuserdata/mcarman.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Sources/App/Controllers/FuelEntryV2Controller.swift b/Sources/App/Controllers/FuelEntryV2Controller.swift index ef1d7f7..65c7bee 100644 --- a/Sources/App/Controllers/FuelEntryV2Controller.swift +++ b/Sources/App/Controllers/FuelEntryV2Controller.swift @@ -55,14 +55,26 @@ struct FuelEntryV2Controller: RouteCollection { return response } - fileprivate func parseDate(_ dateString: String, _ start: Bool = true) -> Date? { + fileprivate func parseDate(_ dateString: String, _ start: Bool = true) throws -> Date? { if(dateString.count == 0){ return nil } else { let parts = dateString.split(separator: "-") - let year = Int(parts[0])! - let month = Int(parts[1])! - let day = Int(parts[2])! + if(parts.count != 3) { + throw Abort(.custom(code: 500,reasonPhrase: "Value at path '\(start ? "startdate" : "enddate")' was not of format 'yyyy-MM-dd'. Data found at '\(start ? "startdate" : "enddate")' was not properly formatted.")) + } + let year = Int(parts[0]) ?? nil + if(year == nil) { + throw Abort(.custom(code: 400,reasonPhrase: "Value at path '\(start ? "startdate" : "enddate")' was not of format 'yyyy-MM-dd'. Data found at '\(start ? "startdate" : "enddate")' was not properly formatted. Year component must be in the format yyyy and must be an integer")) + } + let month = Int(parts[1]) ?? nil + if(month == nil) { + throw Abort(.custom(code: 400,reasonPhrase: "Value at path '\(start ? "startdate" : "enddate")' was not of format 'yyyy-MM-dd'. Data found at '\(start ? "startdate" : "enddate")' was not properly formatted. Month component must be in the format MM and must be an integer")) + } + let day = Int(parts[2]) ?? nil + if( day == nil ) { + throw Abort(.custom(code: 400,reasonPhrase: "Value at path '\(start ? "startdate" : "enddate")' was not of format 'yyyy-MM-dd'. Data found at '\(start ? "startdate" : "enddate")' was not properly formatted. Day component must be in the format dd and must be an integer")) + } var dateComponents = DateComponents() dateComponents.year = year dateComponents.month = month @@ -92,6 +104,7 @@ struct FuelEntryV2Controller: RouteCollection { let previous = try qstring.get(Bool?.self, at: "previous") ?? false + var query = FuelEntry.query(on: req.db) if(image){ query = query @@ -135,7 +148,7 @@ struct FuelEntryV2Controller: RouteCollection { query = query.group(.and){ grp in if( startdate != nil) { - grp.filter(\.$loaddate >= startdate!) + grp.filter(\.$loaddate >= startdate) } if( enddate != nil) { grp.filter(\.$loaddate <= enddate!) @@ -167,7 +180,7 @@ struct FuelEntryV2Controller: RouteCollection { return try await wimage.encodeResponse(for: req) } else { let woimage = try await query.paginate(for: req).map{ - img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, isOverride: img.isOverride, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: previous == true ? img.pulldate : Date()) + img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, image: nil, isOverride: img.isOverride, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: previous == true ? img.pulldate : Date()) } if(!previous) { woimage.items.forEach { @@ -209,7 +222,7 @@ struct FuelEntryV2Controller: RouteCollection { .field(\.$image) .field(\.$downloaded) .field(\.$loaddate) - //.filter(\.$image != nil) + } else { query = query @@ -259,7 +272,7 @@ struct FuelEntryV2Controller: RouteCollection { return try await wimage.encodeResponse(for: req) } else { let woimage = try await query.paginate(for: req).map{ - img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, isOverride: img.isOverride, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: Date()) + img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, image: nil, isOverride: img.isOverride, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: Date()) } return try await woimage.encodeResponse(for: req) } @@ -282,16 +295,114 @@ struct FuelEntryV2Controller: RouteCollection { try await fuelEntry.delete(on: req.db) return .noContent } + + @Sendable + func getrpm(req: Request) async throws -> Response { + let qstring = req.query + let startdate = try parseDate(qstring.get(String?.self, at: "startdate") ?? "") + let enddate = try parseDate(qstring.get(String?.self, at: "enddate") ?? "", false) + let image = try qstring.get(Bool?.self, at: "image") ?? false + let serial = try qstring.get(String?.self, at: "serial") ?? nil + let tank = try qstring.get(String?.self, at: "tank") ?? nil + let previous = try qstring.get(Bool?.self, at: "previous") ?? false + + + var query = FuelEntry.query(on: req.db) + if(image){ + query = query + .field(\.$id) + .field(\.$timestamp) + .field(\.$userID) + .field(\.$product) + .field(\.$make) + .field(\.$model) + .field(\.$serialNumber) + .field(\.$tankID) + .field(\.$fuelAmount) + .field(\.$fuelType) + .field(\.$isOverride) + .field(\.$latitude) + .field(\.$longitude) + .field(\.$image) + .field(\.$downloaded) + .field(\.$loaddate) + + + } else { + query = query + .field(\.$id) + .field(\.$timestamp) + .field(\.$userID) + .field(\.$product) + .field(\.$make) + .field(\.$model) + .field(\.$serialNumber) + .field(\.$tankID) + .field(\.$fuelAmount) + .field(\.$fuelType) + .field(\.$isOverride) + .field(\.$latitude) + .field(\.$longitude) + .field(\.$downloaded) + .field(\.$loaddate) + } + + query = query.group(.and){ + grp in + if( startdate != nil) { + grp.filter(\.$loaddate >= startdate!) + } + if( enddate != nil) { + grp.filter(\.$loaddate <= enddate!) + } + if( serial != nil) { + grp.filter(\.$serialNumber == serial!) + } + if( tank != nil) { + grp.filter(\.$tankID == tank!) + } + if(!previous){ + grp.filter(\.$pulldate == nil) + } + + } + query = query.sort(\.$loaddate, .ascending).sort(\.$timestamp).sort(\.$serialNumber) + + + + if(image){ + //query = query.filter(\.$image) + let wimage = try await query.paginate(for: req).map{ + img in FuelEntry.wimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, isOverride: img.isOverride, image: img.image == nil ? Data(): img.image, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: Date()) + + } + return try await wimage.encodeResponse(for: req) + } else { + let woimage = try await query.paginate(for: req).map{ + img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, image: nil, isOverride: img.isOverride, downloaded: img.downloaded == nil ? false : img.downloaded!, loaddate: img.loaddate == nil ? nil : img.loaddate, pulldate: Date()) + } + return try await woimage.encodeResponse(for: req) + } + } +} + +struct custMeta: Content { + let per: Int + let page: Int + let total: Int + let status: String? } struct PageRequest: Content { var page: Int var size: Int + var status: String? // Add any other properties needed for pagination - init(page: Int, size: Int) { + init(page: Int, size: Int, status: String? = nil) { self.page = page self.size = size + self.status = status } } diff --git a/Sources/App/Models/FuelEntry.swift b/Sources/App/Models/FuelEntry.swift index 12ff4c9..922499a 100755 --- a/Sources/App/Models/FuelEntry.swift +++ b/Sources/App/Models/FuelEntry.swift @@ -24,6 +24,7 @@ final class FuelEntry: Model, Content { let tankID: String let fuelAmount: Double let fuelType: String + let image: Data? let isOverride: Bool let downloaded: Bool? let loaddate: Date? @@ -43,6 +44,7 @@ final class FuelEntry: Model, Content { entry.fuelAmount = self.fuelAmount entry.fuelType = self.fuelType entry.isOverride = self.isOverride + entry.image = nil entry.downloaded = self.downloaded entry.loaddate = self.loaddate entry.pulldate = self.pulldate diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 073a191..ffaf095 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -10,7 +10,7 @@ public func configure(_ app: Application) async throws { app.routes.defaultMaxBodySize = "128mb" // let timeout = HTTPClient.Configuration.Timeout(connect: .seconds(300), read: .seconds(300)) // app.http.client.configuration.timeout = timeout - app.databases.use(.postgres(hostname: "127.0.0.1", port: 5432, username: "mcarman", password: "@ng31F@rm0823262", database: "fueling"), as: .psql) + app.databases.use(.postgres(hostname: "127.0.0.1", port: 5433, username: "mcarman", password: "@ng31F@rm0823262", database: "fueling"), as: .psql) // app.databases.use(DatabaseConfigurationFactory.postgres(configuration: .init( // hostname: Environment.get("DATABASE_HOST") ?? "localhost", // port: Environment.get("DATABASE_PORT").flatMap(Int.init(_:)) ?? 5433,