Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

116 строки
4.3 KiB

  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. //
  25. // for var fuelEntry in fuelEntries {
  26. // if let imagePath = fuelEntry.image {
  27. // let fileURL = URL(fileURLWithPath: imagePath)
  28. // if let imageData = try? Data(contentsOf: fileURL) {
  29. // fuelEntry.imageData = imageData
  30. // }
  31. // }
  32. // }
  33. // return fuelEntries
  34. // }
  35. func create(req: Request) async throws -> [FuelEntry] {
  36. let fuelEntry = try req.content.decode([FuelEntry].self)
  37. try await fuelEntry.create(on: req.db)
  38. return fuelEntry
  39. }
  40. //Create with image file written to server storage instead of database
  41. // func create(req: Request) async throws -> [FuelEntry] {
  42. // let fuelEntries = try await req.content.decode([FuelEntry].self)
  43. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  44. //
  45. // var updatedFuelEntries: [FuelEntry] = []
  46. //
  47. // for fuelEntry in fuelEntries {
  48. // let updatedFuelEntry = try await { () async throws -> FuelEntry in
  49. // var updatedFuelEntry = fuelEntry
  50. //
  51. // if let imageData = updatedFuelEntry.imageData {
  52. // let imageFilename = "\(updatedFuelEntry.id!).png"
  53. // let tempImageURL = URL(fileURLWithPath: directory + imageFilename)
  54. // try await imageData.write(to: tempImageURL)
  55. // updatedFuelEntry.image = tempImageURL.absoluteString
  56. // }
  57. //
  58. // do {
  59. // _ = try await updatedFuelEntry.save(on: req.db)
  60. // return updatedFuelEntry
  61. // } catch {
  62. // throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)")
  63. // }
  64. // }()
  65. //
  66. // updatedFuelEntries.append(updatedFuelEntry)
  67. // }
  68. //
  69. // return updatedFuelEntries
  70. // }
  71. func delete(req: Request) async throws -> HTTPStatus {
  72. guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else {
  73. throw Abort(.notFound)
  74. }
  75. try await fuelEntry.delete(on: req.db)
  76. return .noContent
  77. }
  78. // func uploadImage(req: Request) async throws -> FuelEntry {
  79. // let fuelEntryID: UUID = try req.parameters.require("id")
  80. //
  81. // let file = try await req.content.decode(File.self)
  82. // let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image
  83. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  84. //
  85. // if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) {
  86. // do {
  87. // try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server
  88. // } catch {
  89. // throw Abort(.internalServerError, reason: "Failed to save image file")
  90. // }
  91. //
  92. // // Update FuelEntry model after saving the image
  93. // if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) {
  94. // return fuelEntry
  95. // } else {
  96. // throw Abort(.notFound, reason: "FuelEntry not found")
  97. // }
  98. // } else {
  99. // throw Abort(.badRequest, reason: "Failed to extract image data")
  100. // }
  101. // }
  102. }