You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.5 KiB

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