Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

FuelEntryV2Controller.swift 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // FuelEntryController.swift
  3. //
  4. //
  5. // Created by Bill Ryckman on 4/24/23.
  6. //
  7. import Vapor
  8. struct FuelEntryV2Controller: RouteCollection {
  9. func boot(routes: RoutesBuilder) throws {
  10. let fuelEntries = routes.grouped("fuel_entries")
  11. fuelEntries.get(use: index)
  12. fuelEntries.get("with-images", use: getAllEntriesWithImages)
  13. fuelEntries.post(use: create)
  14. // fuelEntries.post("upload-image", use: uploadImage)
  15. fuelEntries.group(":fuelEntryID") { fuelEntry in
  16. fuelEntry.delete(use: delete)
  17. }
  18. }
  19. func index(req: Request) async throws -> [FuelEntry] {
  20. try await FuelEntry.query(on: req.db).all()
  21. }
  22. func getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] {
  23. let fuelEntries = try await FuelEntry.query(on: req.db).all()
  24. for var fuelEntry in fuelEntries {
  25. if let imagePath = fuelEntry.image {
  26. let fileURL = URL(fileURLWithPath: imagePath)
  27. if let imageData = try? Data(contentsOf: fileURL) {
  28. fuelEntry.imageData = imageData
  29. }
  30. }
  31. }
  32. return fuelEntries
  33. }
  34. func create(req: Request) async throws -> [FuelEntry] {
  35. let fuelEntries = try await req.content.decode([FuelEntry].self)
  36. let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  37. var updatedFuelEntries: [FuelEntry] = []
  38. for fuelEntry in fuelEntries {
  39. let updatedFuelEntry = try await { () async throws -> FuelEntry in
  40. var updatedFuelEntry = fuelEntry
  41. if let imageData = updatedFuelEntry.imageData {
  42. let imageFilename = "\(updatedFuelEntry.id!).png"
  43. let tempImageURL = URL(fileURLWithPath: directory + imageFilename)
  44. try await imageData.write(to: tempImageURL)
  45. updatedFuelEntry.image = tempImageURL.absoluteString
  46. }
  47. do {
  48. _ = try await updatedFuelEntry.save(on: req.db)
  49. return updatedFuelEntry
  50. } catch {
  51. throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)")
  52. }
  53. }()
  54. updatedFuelEntries.append(updatedFuelEntry)
  55. }
  56. return updatedFuelEntries
  57. }
  58. func delete(req: Request) async throws -> HTTPStatus {
  59. guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else {
  60. throw Abort(.notFound)
  61. }
  62. try await fuelEntry.delete(on: req.db)
  63. return .noContent
  64. }
  65. // func uploadImage(req: Request) async throws -> FuelEntry {
  66. // let fuelEntryID: UUID = try req.parameters.require("id")
  67. //
  68. // let file = try await req.content.decode(File.self)
  69. // let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image
  70. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  71. //
  72. // if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) {
  73. // do {
  74. // try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server
  75. // } catch {
  76. // throw Abort(.internalServerError, reason: "Failed to save image file")
  77. // }
  78. //
  79. // // Update FuelEntry model after saving the image
  80. // if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) {
  81. // return fuelEntry
  82. // } else {
  83. // throw Abort(.notFound, reason: "FuelEntry not found")
  84. // }
  85. // } else {
  86. // throw Abort(.badRequest, reason: "Failed to extract image data")
  87. // }
  88. // }
  89. }