Просмотр исходного кода

Added getAllEntriesWithImages to bring down pictures with data if need be. Left default route intact without images.

Added the creation of the image field with the imagePath.
v2_uploadImages
admin 2 лет назад
Родитель
Сommit
934d4d9c25
2 измененных файлов: 30 добавлений и 5 удалений
  1. +25
    -4
      Sources/App/Controllers/FuelEntryV2Controller.swift
  2. +5
    -1
      Sources/App/Models/FuelEntry.swift

+ 25
- 4
Sources/App/Controllers/FuelEntryV2Controller.swift Просмотреть файл

@@ -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 {


+ 5
- 1
Sources/App/Models/FuelEntry.swift Просмотреть файл

@@ -52,10 +52,13 @@ final class FuelEntry: Model, Content {

@Field(key: "is_override")
var isOverride: Bool
@Field(key: "imageData")
var imageData: Data?

init() {}

init(id: UUID? = nil, timestamp: Date, userID: String, latitude: Double, longitude: Double, image: String?, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool) {
init(id: UUID? = nil, timestamp: Date, userID: String, latitude: Double, longitude: Double, image: String?, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool, imageData: Data?) {
self.id = id
self.timestamp = timestamp
self.userID = userID
@@ -70,5 +73,6 @@ final class FuelEntry: Model, Content {
self.fuelAmount = fuelAmount
self.fuelType = fuelType
self.isOverride = isOverride
self.imageData = imageData
}
}

Загрузка…
Отмена
Сохранить