Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

215 linhas
7.8 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("test", use: filtered)
  14. // fuelEntries.get("with-images", use: getAllEntriesWithImages)
  15. fuelEntries.post(use: create)
  16. // fuelEntries.post("upload-image", use: uploadImage)
  17. fuelEntries.group(":fuelEntryID") { fuelEntry in
  18. fuelEntry.delete(use: delete)
  19. }
  20. }
  21. func index(req: Request) async throws -> [FuelEntry] {
  22. return try await FuelEntry.query(on: req.db).all()
  23. }
  24. struct filteredreturn: Content{
  25. var fuelentries: [FuelEntry]
  26. var pages: Int
  27. }
  28. @Sendable
  29. func filtered(req: Request) async throws -> Response {
  30. var image: Bool
  31. var serial: String
  32. var tank: String
  33. let qstring = req.query
  34. if let qimage = try? qstring.get(Bool.self, at: "image"){
  35. image = qimage
  36. } else {
  37. image = false
  38. }
  39. if let qserial = try? qstring.get(String.self, at: "serial"){
  40. serial = qserial
  41. } else {
  42. serial = ""
  43. }
  44. if let qtank = try? qstring.get(String.self, at: "tank"){
  45. tank = qtank
  46. } else {
  47. tank = ""
  48. }
  49. var query = FuelEntry.query(on: req.db)
  50. if(image){
  51. query = query
  52. .field(\.$id)
  53. .field(\.$timestamp)
  54. .field(\.$userID)
  55. .field(\.$product)
  56. .field(\.$make)
  57. .field(\.$model)
  58. .field(\.$serialNumber)
  59. .field(\.$tankID)
  60. .field(\.$fuelAmount)
  61. .field(\.$fuelType)
  62. .field(\.$isOverride)
  63. .field(\.$latitude)
  64. .field(\.$longitude)
  65. .field(\.$image)
  66. .field(\.$downloaded)
  67. } else {
  68. query = query
  69. .field(\.$id)
  70. .field(\.$timestamp)
  71. .field(\.$userID)
  72. .field(\.$product)
  73. .field(\.$make)
  74. .field(\.$model)
  75. .field(\.$serialNumber)
  76. .field(\.$tankID)
  77. .field(\.$fuelAmount)
  78. .field(\.$fuelType)
  79. .field(\.$isOverride)
  80. .field(\.$latitude)
  81. .field(\.$longitude)
  82. .field(\.$downloaded)
  83. }
  84. if(serial.count > 0){
  85. query = query
  86. .filter(\.$serialNumber == serial)
  87. }
  88. if(tank.count > 0){
  89. query = query
  90. .filter(\.$tankID == tank)
  91. }
  92. if(image){
  93. let wimage = try await query.paginate(for: req).map{
  94. img in FuelEntry.wimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, isOverride: img.isOverride, image: img.image!, downloaded: img.downloaded!)
  95. }
  96. return try await wimage.encodeResponse(for: req)
  97. } else {
  98. let woimage = try await query.paginate(for: req).map{
  99. img in FuelEntry.woimage(id: img.id, timestamp: img.timestamp, userID: img.userID, latitude: img.latitude, longitutde: img.longitude, product: img.product, make: img.make, model: img.model, serialNumber: img.serialNumber, tankID: img.tankID, fuelAmount: img.fuelAmount, fuelType: img.fuelType, isOverride: img.isOverride, downloaded: img.downloaded!)
  100. }
  101. return try await woimage.encodeResponse(for: req)
  102. }
  103. // let result = try await query.paginate(for: req)
  104. // return try await result.encodeResponse(for: req)
  105. }
  106. // func getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] {
  107. // let fuelEntries = try await FuelEntry.query(on: req.db).all()
  108. //
  109. // for var fuelEntry in fuelEntries {
  110. // if let imagePath = fuelEntry.image {
  111. // let fileURL = URL(fileURLWithPath: imagePath)
  112. // if let imageData = try? Data(contentsOf: fileURL) {
  113. // fuelEntry.imageData = imageData
  114. // }
  115. // }
  116. // }
  117. // return fuelEntries
  118. // }
  119. func create(req: Request) async throws -> [FuelEntry] {
  120. let fuelEntry = try req.content.decode([FuelEntry].self)
  121. try await fuelEntry.create(on: req.db)
  122. return fuelEntry
  123. }
  124. //Create with image file written to server storage instead of database
  125. // func create(req: Request) async throws -> [FuelEntry] {
  126. // let fuelEntries = try await req.content.decode([FuelEntry].self)
  127. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  128. //
  129. // var updatedFuelEntries: [FuelEntry] = []
  130. //
  131. // for fuelEntry in fuelEntries {
  132. // let updatedFuelEntry = try await { () async throws -> FuelEntry in
  133. // var updatedFuelEntry = fuelEntry
  134. //
  135. // if let imageData = updatedFuelEntry.imageData {
  136. // let imageFilename = "\(updatedFuelEntry.id!).png"
  137. // let tempImageURL = URL(fileURLWithPath: directory + imageFilename)
  138. // try await imageData.write(to: tempImageURL)
  139. // updatedFuelEntry.image = tempImageURL.absoluteString
  140. // }
  141. //
  142. // do {
  143. // _ = try await updatedFuelEntry.save(on: req.db)
  144. // return updatedFuelEntry
  145. // } catch {
  146. // throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)")
  147. // }
  148. // }()
  149. //
  150. // updatedFuelEntries.append(updatedFuelEntry)
  151. // }
  152. //
  153. // return updatedFuelEntries
  154. // }
  155. func delete(req: Request) async throws -> HTTPStatus {
  156. guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else {
  157. throw Abort(.notFound)
  158. }
  159. try await fuelEntry.delete(on: req.db)
  160. return .noContent
  161. }
  162. // func uploadImage(req: Request) async throws -> FuelEntry {
  163. // let fuelEntryID: UUID = try req.parameters.require("id")
  164. //
  165. // let file = try await req.content.decode(File.self)
  166. // let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image
  167. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  168. //
  169. // if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) {
  170. // do {
  171. // try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server
  172. // } catch {
  173. // throw Abort(.internalServerError, reason: "Failed to save image file")
  174. // }
  175. //
  176. // // Update FuelEntry model after saving the image
  177. // if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) {
  178. // return fuelEntry
  179. // } else {
  180. // throw Abort(.notFound, reason: "FuelEntry not found")
  181. // }
  182. // } else {
  183. // throw Abort(.badRequest, reason: "Failed to extract image data")
  184. // }
  185. // }
  186. }
  187. struct PageRequest: Content {
  188. var page: Int
  189. var size: Int
  190. // Add any other properties needed for pagination
  191. init(page: Int, size: Int) {
  192. self.page = page
  193. self.size = size
  194. }
  195. }