| @@ -1,6 +1,6 @@ | |||||
| // | // | ||||
| // FuelEntryController.swift | // FuelEntryController.swift | ||||
| // | |||||
| // | |||||
| // | // | ||||
| // Created by Bill Ryckman on 4/24/23. | // Created by Bill Ryckman on 4/24/23. | ||||
| // | // | ||||
| @@ -12,7 +12,6 @@ struct FuelEntryController: RouteCollection { | |||||
| let fuelEntries = routes.grouped("fuel_entries") | let fuelEntries = routes.grouped("fuel_entries") | ||||
| fuelEntries.get(use: index) | fuelEntries.get(use: index) | ||||
| fuelEntries.post(use: create) | fuelEntries.post(use: create) | ||||
| fuelEntries.post("upload-image", use: uploadImage) | |||||
| fuelEntries.group(":fuelEntryID") { fuelEntry in | fuelEntries.group(":fuelEntryID") { fuelEntry in | ||||
| fuelEntry.delete(use: delete) | fuelEntry.delete(use: delete) | ||||
| } | } | ||||
| @@ -35,33 +34,9 @@ struct FuelEntryController: RouteCollection { | |||||
| try await fuelEntry.delete(on: req.db) | try await fuelEntry.delete(on: req.db) | ||||
| return .noContent | return .noContent | ||||
| } | } | ||||
| func uploadImage(req: Request) async throws -> FuelEntry { | |||||
| let fuelEntryID: UUID = try req.parameters.require("id") | |||||
| let file = try await req.content.decode(File.self) | |||||
| let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image | |||||
| let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" | |||||
| if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { | |||||
| do { | |||||
| try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server | |||||
| } catch { | |||||
| throw Abort(.internalServerError, reason: "Failed to save image file") | |||||
| } | |||||
| // Update FuelEntry model after saving the image | |||||
| if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { | |||||
| return fuelEntry | |||||
| } else { | |||||
| throw Abort(.notFound, reason: "FuelEntry not found") | |||||
| } | |||||
| } else { | |||||
| throw Abort(.badRequest, reason: "Failed to extract image data") | |||||
| } | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,69 @@ | |||||
| // | |||||
| // FuelEntryController.swift | |||||
| // | |||||
| // | |||||
| // Created by Bill Ryckman on 4/24/23. | |||||
| // | |||||
| import Vapor | |||||
| struct FuelEntryV2Controller: RouteCollection { | |||||
| func boot(routes: RoutesBuilder) throws { | |||||
| let fuelEntries = routes.grouped("fuel_entries") | |||||
| fuelEntries.get(use: index) | |||||
| fuelEntries.post(use: create) | |||||
| fuelEntries.post("upload-image", use: uploadImage) | |||||
| fuelEntries.group(":fuelEntryID") { fuelEntry in | |||||
| fuelEntry.delete(use: delete) | |||||
| } | |||||
| } | |||||
| func index(req: Request) async throws -> [FuelEntry] { | |||||
| try await FuelEntry.query(on: req.db).all() | |||||
| } | |||||
| //Need to create image file path at time of FuelEntry creation. UploadImage to store file appropriately | |||||
| func create(req: Request) async throws -> [FuelEntry] { | |||||
| let fuelEntry = try req.content.decode([FuelEntry].self) | |||||
| try await fuelEntry.create(on: req.db) | |||||
| return fuelEntry | |||||
| } | |||||
| func delete(req: Request) async throws -> HTTPStatus { | |||||
| guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else { | |||||
| throw Abort(.notFound) | |||||
| } | |||||
| try await fuelEntry.delete(on: req.db) | |||||
| return .noContent | |||||
| } | |||||
| func uploadImage(req: Request) async throws -> FuelEntry { | |||||
| let fuelEntryID: UUID = try req.parameters.require("id") | |||||
| let file = try await req.content.decode(File.self) | |||||
| let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image | |||||
| let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" | |||||
| if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { | |||||
| do { | |||||
| try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server | |||||
| } catch { | |||||
| throw Abort(.internalServerError, reason: "Failed to save image file") | |||||
| } | |||||
| // Update FuelEntry model after saving the image | |||||
| if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { | |||||
| return fuelEntry | |||||
| } else { | |||||
| throw Abort(.notFound, reason: "FuelEntry not found") | |||||
| } | |||||
| } else { | |||||
| throw Abort(.badRequest, reason: "Failed to extract image data") | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -13,6 +13,7 @@ public func configure(_ app: Application) async throws { | |||||
| app.http.server.configuration.port = 3001 | app.http.server.configuration.port = 3001 | ||||
| app.migrations.add(CreateFuelEntry()) | app.migrations.add(CreateFuelEntry()) | ||||
| app.migrations.add(AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage()) | app.migrations.add(AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage()) | ||||
| app.migrations.add(AddImageBackIn()) | |||||
| try await app.autoMigrate().get() | try await app.autoMigrate().get() | ||||
| // register routes | // register routes | ||||
| @@ -32,22 +32,22 @@ func routes(_ app: Application) throws { | |||||
| // V2 API routes with image upload | // V2 API routes with image upload | ||||
| let v2Routes = app.group("api", "v2") { v2 in | let v2Routes = app.group("api", "v2") { v2 in | ||||
| let fuelEntryController = FuelEntryController() | |||||
| let fuelEntryV2Controller = FuelEntryV2Controller() | |||||
| v2.get("fuel-entries") {req in | v2.get("fuel-entries") {req in | ||||
| try await fuelEntryController.index(req: req) | |||||
| try await fuelEntryV2Controller.index(req: req) | |||||
| } | } | ||||
| v2.post("fuel-entries") {req in | v2.post("fuel-entries") {req in | ||||
| try await fuelEntryController.create(req: req) | |||||
| try await fuelEntryV2Controller.create(req: req) | |||||
| } | } | ||||
| v2.post("fuel-entries", ":id", "upload-image") { req -> FuelEntry in | v2.post("fuel-entries", ":id", "upload-image") { req -> FuelEntry in | ||||
| return try await fuelEntryController.uploadImage(req: req) | |||||
| return try await fuelEntryV2Controller.uploadImage(req: req) | |||||
| } | } | ||||
| v2.delete("fuel-entries", ":fuelEntryID") { req in | v2.delete("fuel-entries", ":fuelEntryID") { req in | ||||
| try await fuelEntryController.delete(req: req) | |||||
| try await fuelEntryV2Controller.delete(req: req) | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||