From 5e562aa8d82af8039c39b539f92a51aa74210de4 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 12 Mar 2024 16:38:22 -0400 Subject: [PATCH] Update routes to use api/v2 and include with-images Remove Todo files --- .../Controllers/FuelEntryV2Controller.swift | 50 +++++++++---------- Sources/App/Controllers/TodoController.swift | 31 ------------ Sources/App/Migrations/CreateTodo.swift | 14 ------ Sources/App/Models/Todo.swift | 19 ------- Sources/App/routes.swift | 25 +++------- 5 files changed, 32 insertions(+), 107 deletions(-) delete mode 100755 Sources/App/Controllers/TodoController.swift delete mode 100755 Sources/App/Migrations/CreateTodo.swift delete mode 100755 Sources/App/Models/Todo.swift diff --git a/Sources/App/Controllers/FuelEntryV2Controller.swift b/Sources/App/Controllers/FuelEntryV2Controller.swift index f7d4e59..52aecd5 100644 --- a/Sources/App/Controllers/FuelEntryV2Controller.swift +++ b/Sources/App/Controllers/FuelEntryV2Controller.swift @@ -13,7 +13,7 @@ struct FuelEntryV2Controller: RouteCollection { fuelEntries.get(use: index) fuelEntries.get("with-images", use: getAllEntriesWithImages) fuelEntries.post(use: create) - fuelEntries.post("upload-image", use: uploadImage) +// fuelEntries.post("upload-image", use: uploadImage) fuelEntries.group(":fuelEntryID") { fuelEntry in fuelEntry.delete(use: delete) } @@ -76,30 +76,30 @@ struct FuelEntryV2Controller: RouteCollection { return .noContent } - func uploadImage(req: Request) async throws -> FuelEntry { - let fuelEntryID: UUID = try req.parameters.require("id") - - let file = try await req.content.decode(File.self) - let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image - let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" - - if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { - do { - try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server - } catch { - throw Abort(.internalServerError, reason: "Failed to save image file") - } - - // Update FuelEntry model after saving the image - if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { - return fuelEntry - } else { - throw Abort(.notFound, reason: "FuelEntry not found") - } - } else { - throw Abort(.badRequest, reason: "Failed to extract image data") - } - } +// func uploadImage(req: Request) async throws -> FuelEntry { +// let fuelEntryID: UUID = try req.parameters.require("id") +// +// let file = try await req.content.decode(File.self) +// let filename = fuelEntryID.uuidString + ".png" // Generate a unique filename for the uploaded image +// let directory = DirectoryConfiguration.detect().workingDirectory + "Images/" +// +// if let imageData = file.data.getData(at: 0, length: file.data.readableBytes) { +// do { +// try Data(imageData).write(to: URL(fileURLWithPath: directory + filename)) // Save the uploaded image to server +// } catch { +// throw Abort(.internalServerError, reason: "Failed to save image file") +// } +// +// // Update FuelEntry model after saving the image +// if let fuelEntry = try await FuelEntry.find(fuelEntryID, on: req.db) { +// return fuelEntry +// } else { +// throw Abort(.notFound, reason: "FuelEntry not found") +// } +// } else { +// throw Abort(.badRequest, reason: "Failed to extract image data") +// } +// } } diff --git a/Sources/App/Controllers/TodoController.swift b/Sources/App/Controllers/TodoController.swift deleted file mode 100755 index 265228d..0000000 --- a/Sources/App/Controllers/TodoController.swift +++ /dev/null @@ -1,31 +0,0 @@ -import Fluent -import Vapor - -struct TodoController: RouteCollection { - func boot(routes: RoutesBuilder) throws { - let todos = routes.grouped("todos") - todos.get(use: index) - todos.post(use: create) - todos.group(":todoID") { todo in - todo.delete(use: delete) - } - } - - func index(req: Request) async throws -> [Todo] { - try await Todo.query(on: req.db).all() - } - - func create(req: Request) async throws -> Todo { - let todo = try req.content.decode(Todo.self) - try await todo.save(on: req.db) - return todo - } - - func delete(req: Request) async throws -> HTTPStatus { - guard let todo = try await Todo.find(req.parameters.get("todoID"), on: req.db) else { - throw Abort(.notFound) - } - try await todo.delete(on: req.db) - return .noContent - } -} diff --git a/Sources/App/Migrations/CreateTodo.swift b/Sources/App/Migrations/CreateTodo.swift deleted file mode 100755 index bf6d945..0000000 --- a/Sources/App/Migrations/CreateTodo.swift +++ /dev/null @@ -1,14 +0,0 @@ -import Fluent - -struct CreateTodo: AsyncMigration { - func prepare(on database: Database) async throws { - try await database.schema("todos") - .id() - .field("title", .string, .required) - .create() - } - - func revert(on database: Database) async throws { - try await database.schema("todos").delete() - } -} diff --git a/Sources/App/Models/Todo.swift b/Sources/App/Models/Todo.swift deleted file mode 100755 index c3c9eac..0000000 --- a/Sources/App/Models/Todo.swift +++ /dev/null @@ -1,19 +0,0 @@ -import Fluent -import Vapor - -final class Todo: Model, Content { - static let schema = "todos" - - @ID(key: .id) - var id: UUID? - - @Field(key: "title") - var title: String - - init() { } - - init(id: UUID? = nil, title: String) { - self.id = id - self.title = title - } -} diff --git a/Sources/App/routes.swift b/Sources/App/routes.swift index 8e6d292..61e693e 100755 --- a/Sources/App/routes.swift +++ b/Sources/App/routes.swift @@ -1,18 +1,3 @@ -//import Fluent -//import Vapor -// -//func routes(_ app: Application) throws { -// app.get { req async in -// "It works!" -// } -// -// app.get("hello") { req async -> String in -// "Hello, world!" -// } -// -// try app.register(collection: TodoController()) -//} - import Vapor func routes(_ app: Application) throws { @@ -38,13 +23,17 @@ func routes(_ app: Application) throws { try await fuelEntryV2Controller.index(req: req) } + v2.get("fuel-entries", "with-images") {req in + try await fuelEntryV2Controller.getAllEntriesWithImages(req: req) + } + v2.post("fuel-entries") {req in try await fuelEntryV2Controller.create(req: req) } - v2.post("fuel-entries", ":id", "upload-image") { req -> FuelEntry in - return try await fuelEntryV2Controller.uploadImage(req: req) - } +// v2.post("fuel-entries", ":id", "upload-image") { req -> FuelEntry in +// return try await fuelEntryV2Controller.uploadImage(req: req) +// } v2.delete("fuel-entries", ":fuelEntryID") { req in try await fuelEntryV2Controller.delete(req: req)