Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

213 řádky
7.7 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. } else {
  67. query = query
  68. .field(\.$id)
  69. .field(\.$timestamp)
  70. .field(\.$userID)
  71. .field(\.$product)
  72. .field(\.$make)
  73. .field(\.$model)
  74. .field(\.$serialNumber)
  75. .field(\.$tankID)
  76. .field(\.$fuelAmount)
  77. .field(\.$fuelType)
  78. .field(\.$isOverride)
  79. .field(\.$latitude)
  80. .field(\.$longitude)
  81. }
  82. if(serial.count > 0){
  83. query = query
  84. .filter(\.$serialNumber == serial)
  85. }
  86. if(tank.count > 0){
  87. query = query
  88. .filter(\.$tankID == tank)
  89. }
  90. if(image){
  91. let wimage = try await query.paginate(for: req).map{
  92. 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!)
  93. }
  94. return try await wimage.encodeResponse(for: req)
  95. } else {
  96. let woimage = try await query.paginate(for: req).map{
  97. 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!)
  98. }
  99. return try await woimage.encodeResponse(for: req)
  100. }
  101. // let result = try await query.paginate(for: req)
  102. // return try await result.encodeResponse(for: req)
  103. }
  104. // func getAllEntriesWithImages(req: Request) async throws -> [FuelEntry] {
  105. // let fuelEntries = try await FuelEntry.query(on: req.db).all()
  106. //
  107. // for var fuelEntry in fuelEntries {
  108. // if let imagePath = fuelEntry.image {
  109. // let fileURL = URL(fileURLWithPath: imagePath)
  110. // if let imageData = try? Data(contentsOf: fileURL) {
  111. // fuelEntry.imageData = imageData
  112. // }
  113. // }
  114. // }
  115. // return fuelEntries
  116. // }
  117. func create(req: Request) async throws -> [FuelEntry] {
  118. let fuelEntry = try req.content.decode([FuelEntry].self)
  119. try await fuelEntry.create(on: req.db)
  120. return fuelEntry
  121. }
  122. //Create with image file written to server storage instead of database
  123. // func create(req: Request) async throws -> [FuelEntry] {
  124. // let fuelEntries = try await req.content.decode([FuelEntry].self)
  125. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  126. //
  127. // var updatedFuelEntries: [FuelEntry] = []
  128. //
  129. // for fuelEntry in fuelEntries {
  130. // let updatedFuelEntry = try await { () async throws -> FuelEntry in
  131. // var updatedFuelEntry = fuelEntry
  132. //
  133. // if let imageData = updatedFuelEntry.imageData {
  134. // let imageFilename = "\(updatedFuelEntry.id!).png"
  135. // let tempImageURL = URL(fileURLWithPath: directory + imageFilename)
  136. // try await imageData.write(to: tempImageURL)
  137. // updatedFuelEntry.image = tempImageURL.absoluteString
  138. // }
  139. //
  140. // do {
  141. // _ = try await updatedFuelEntry.save(on: req.db)
  142. // return updatedFuelEntry
  143. // } catch {
  144. // throw Abort(.internalServerError, reason: "Error saving FuelEntry: \(error)")
  145. // }
  146. // }()
  147. //
  148. // updatedFuelEntries.append(updatedFuelEntry)
  149. // }
  150. //
  151. // return updatedFuelEntries
  152. // }
  153. func delete(req: Request) async throws -> HTTPStatus {
  154. guard let fuelEntry = try await FuelEntry.find(req.parameters.get("fuelEntryID"), on: req.db) else {
  155. throw Abort(.notFound)
  156. }
  157. try await fuelEntry.delete(on: req.db)
  158. return .noContent
  159. }
  160. // func uploadImage(req: Request) async throws -> FuelEntry {
  161. // let fuelEntryID: UUID = try req.parameters.require("id")
  162. //
  163. // let file = try await req.content.decode(File.self)
  164. // let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image
  165. // let directory = DirectoryConfiguration.detect().workingDirectory + "Images/"
  166. //
  167. // if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) {
  168. // do {
  169. // try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server
  170. // } catch {
  171. // throw Abort(.internalServerError, reason: "Failed to save image file")
  172. // }
  173. //
  174. // // Update FuelEntry model after saving the image
  175. // if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) {
  176. // return fuelEntry
  177. // } else {
  178. // throw Abort(.notFound, reason: "FuelEntry not found")
  179. // }
  180. // } else {
  181. // throw Abort(.badRequest, reason: "Failed to extract image data")
  182. // }
  183. // }
  184. }
  185. struct PageRequest: Content {
  186. var page: Int
  187. var size: Int
  188. // Add any other properties needed for pagination
  189. init(page: Int, size: Int) {
  190. self.page = page
  191. self.size = size
  192. }
  193. }