|
|
|
@@ -11,6 +11,7 @@ struct FuelEntryV2Controller: RouteCollection { |
|
|
|
func boot(routes: RoutesBuilder) throws { |
|
|
|
let fuelEntries = routes.grouped("fuel_entries") |
|
|
|
fuelEntries.get(use: index) |
|
|
|
fuelEntries.get("with-images", use: getAllEntriesWithImages) |
|
|
|
fuelEntries.post(use: create) |
|
|
|
fuelEntries.post("upload-image", use: uploadImage) |
|
|
|
fuelEntries.group(":fuelEntryID") { fuelEntry in |
|
|
|
@@ -22,11 +23,31 @@ struct FuelEntryV2Controller: RouteCollection { |
|
|
|
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 getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] { |
|
|
|
let fuelEntries = try await FuelEntry.query(on: req.db).all() |
|
|
|
|
|
|
|
for var fuelEntry in fuelEntries { |
|
|
|
if let imagePath = fuelEntry.image { |
|
|
|
let fileURL = URL(fileURLWithPath: imagePath) |
|
|
|
if let imageData = try? Data(contentsOf: fileURL) { |
|
|
|
fuelEntry.imageData = imageData |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return fuelEntries |
|
|
|
} |
|
|
|
|
|
|
|
func create(req: Request) async throws -> [FuelEntry] { |
|
|
|
let fuelEntry = try req.content.decode([FuelEntry].self) |
|
|
|
try await fuelEntry.create(on: req.db) |
|
|
|
return fuelEntry |
|
|
|
var fuelEntries = try req.content.decode([FuelEntry].self) |
|
|
|
let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" |
|
|
|
|
|
|
|
for index in fuelEntries.indices { |
|
|
|
let imagePath = directory + (fuelEntries[index].id?.uuidString ?? "Unknown") + ".png" |
|
|
|
fuelEntries[index].image = imagePath |
|
|
|
} |
|
|
|
|
|
|
|
try await fuelEntries.create(on: req.db) |
|
|
|
return fuelEntries |
|
|
|
} |
|
|
|
|
|
|
|
func delete(req: Request) async throws -> HTTPStatus { |
|
|
|
|