Quellcode durchsuchen

Update routes to use api/v2 and include with-images

Remove Todo files
v2_uploadImages
admin vor 2 Jahren
Ursprung
Commit
5e562aa8d8
5 geänderte Dateien mit 32 neuen und 107 gelöschten Zeilen
  1. +25
    -25
      Sources/App/Controllers/FuelEntryV2Controller.swift
  2. +0
    -31
      Sources/App/Controllers/TodoController.swift
  3. +0
    -14
      Sources/App/Migrations/CreateTodo.swift
  4. +0
    -19
      Sources/App/Models/Todo.swift
  5. +7
    -18
      Sources/App/routes.swift

+ 25
- 25
Sources/App/Controllers/FuelEntryV2Controller.swift Datei anzeigen

@@ -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")
// }
// }
}




+ 0
- 31
Sources/App/Controllers/TodoController.swift Datei anzeigen

@@ -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
}
}

+ 0
- 14
Sources/App/Migrations/CreateTodo.swift Datei anzeigen

@@ -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()
}
}

+ 0
- 19
Sources/App/Models/Todo.swift Datei anzeigen

@@ -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
}
}

+ 7
- 18
Sources/App/routes.swift Datei anzeigen

@@ -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)


Laden…
Abbrechen
Speichern