|
|
@@ -12,6 +12,7 @@ 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) |
|
|
} |
|
|
} |
|
|
@@ -20,13 +21,13 @@ struct FuelEntryController: RouteCollection { |
|
|
func index(req: Request) async throws -> [FuelEntry] { |
|
|
func index(req: Request) async throws -> [FuelEntry] { |
|
|
try await FuelEntry.query(on: req.db).all() |
|
|
try await FuelEntry.query(on: req.db).all() |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func create(req: Request) async throws -> [FuelEntry] { |
|
|
func create(req: Request) async throws -> [FuelEntry] { |
|
|
let fuelEntry = try req.content.decode([FuelEntry].self) |
|
|
let fuelEntry = try req.content.decode([FuelEntry].self) |
|
|
try await fuelEntry.create(on: req.db) |
|
|
try await fuelEntry.create(on: req.db) |
|
|
return fuelEntry |
|
|
return fuelEntry |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func delete(req: Request) async throws -> HTTPStatus { |
|
|
func delete(req: Request) async throws -> HTTPStatus { |
|
|
guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else { |
|
|
guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else { |
|
|
throw Abort(.notFound) |
|
|
throw Abort(.notFound) |
|
|
@@ -34,6 +35,31 @@ 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") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|