|
|
|
@@ -38,16 +38,34 @@ struct FuelEntryV2Controller: RouteCollection { |
|
|
|
} |
|
|
|
|
|
|
|
func create(req: Request) async throws -> [FuelEntry] { |
|
|
|
var fuelEntries = try req.content.decode([FuelEntry].self) |
|
|
|
let fuelEntries = try await 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 |
|
|
|
var updatedFuelEntries: [FuelEntry] = [] |
|
|
|
|
|
|
|
for fuelEntry in fuelEntries { |
|
|
|
let updatedFuelEntry = try await { () async throws -> FuelEntry in |
|
|
|
var updatedFuelEntry = fuelEntry |
|
|
|
|
|
|
|
if let imageData = updatedFuelEntry.imageData { |
|
|
|
let imageFilename = "\(updatedFuelEntry.id!).png" |
|
|
|
let tempImageURL = URL(fileURLWithPath: directory + imageFilename) |
|
|
|
try await imageData.write(to: tempImageURL) |
|
|
|
updatedFuelEntry.image = tempImageURL.absoluteString |
|
|
|
} |
|
|
|
|
|
|
|
do { |
|
|
|
_ = try await updatedFuelEntry.save(on: req.db) |
|
|
|
return updatedFuelEntry |
|
|
|
} catch { |
|
|
|
throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)") |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
updatedFuelEntries.append(updatedFuelEntry) |
|
|
|
} |
|
|
|
|
|
|
|
try await fuelEntries.create(on: req.db) |
|
|
|
return fuelEntries |
|
|
|
return updatedFuelEntries |
|
|
|
} |
|
|
|
|
|
|
|
func delete(req: Request) async throws -> HTTPStatus { |
|
|
|
|