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

91 строка
3.1 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. 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. var fuelEntries = try req.content.decode([FuelEntry].self)
  36. let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  37. for index in fuelEntries.indices {
  38. let imagePath = directory + (fuelEntries[index].id?.uuidString ?? "Unknown") + ".png"
  39. fuelEntries[index].image = imagePath
  40. }
  41. try await fuelEntries.create(on: req.db)
  42. return fuelEntries
  43. }
  44. func delete(req: Request) async throws -> HTTPStatus {
  45. guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else {
  46. throw Abort(.notFound)
  47. }
  48. try await fuelEntry.delete(on: req.db)
  49. return .noContent
  50. }
  51. func uploadImage(req: Request) async throws -> FuelEntry {
  52. let fuelEntryID: UUID = try req.parameters.require("id")
  53. let file = try await req.content.decode(File.self)
  54. let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image
  55. let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  56. if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) {
  57. do {
  58. try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server
  59. } catch {
  60. throw Abort(.internalServerError, reason: "Failed to save image file")
  61. }
  62. // Update FuelEntry model after saving the image
  63. if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) {
  64. return fuelEntry
  65. } else {
  66. throw Abort(.notFound, reason: "FuelEntry not found")
  67. }
  68. } else {
  69. throw Abort(.badRequest, reason: "Failed to extract image data")
  70. }
  71. }
  72. }