Remove Todo filesv2_uploadImages
| @@ -13,7 +13,7 @@ struct FuelEntryV2Controller: RouteCollection { | |||||
| fuelEntries.get(use: index) | fuelEntries.get(use: index) | ||||
| fuelEntries.get("with-images", use: getAllEntriesWithImages) | fuelEntries.get("with-images", use: getAllEntriesWithImages) | ||||
| fuelEntries.post(use: create) | fuelEntries.post(use: create) | ||||
| fuelEntries.post("upload-image", use: uploadImage) | |||||
| // fuelEntries.post("upload-image", use: uploadImage) | |||||
| fuelEntries.group(":fuelEntryID") { fuelEntry in | fuelEntries.group(":fuelEntryID") { fuelEntry in | ||||
| fuelEntry.delete(use: delete) | fuelEntry.delete(use: delete) | ||||
| } | } | ||||
| @@ -76,30 +76,30 @@ struct FuelEntryV2Controller: RouteCollection { | |||||
| return .noContent | 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") | |||||
| // } | |||||
| // } | |||||
| } | } | ||||
| @@ -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 | |||||
| } | |||||
| } | |||||
| @@ -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() | |||||
| } | |||||
| } | |||||
| @@ -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 | |||||
| } | |||||
| } | |||||
| @@ -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 | import Vapor | ||||
| func routes(_ app: Application) throws { | func routes(_ app: Application) throws { | ||||
| @@ -38,13 +23,17 @@ func routes(_ app: Application) throws { | |||||
| try await fuelEntryV2Controller.index(req: req) | 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 | v2.post("fuel-entries") {req in | ||||
| try await fuelEntryV2Controller.create(req: req) | 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 | v2.delete("fuel-entries", ":fuelEntryID") { req in | ||||
| try await fuelEntryV2Controller.delete(req: req) | try await fuelEntryV2Controller.delete(req: req) | ||||