diff --git a/Sources/App/Controllers/FuelEntryController.swift b/Sources/App/Controllers/FuelEntryController.swift index fa8e6b9..04b7051 100755 --- a/Sources/App/Controllers/FuelEntryController.swift +++ b/Sources/App/Controllers/FuelEntryController.swift @@ -21,9 +21,9 @@ struct FuelEntryController: RouteCollection { try await FuelEntry.query(on: req.db).all() } - func create(req: Request) async throws -> FuelEntry { - let fuelEntry = try req.content.decode(FuelEntry.self) - try await fuelEntry.save(on: req.db) + func create(req: Request) async throws -> [FuelEntry] { + let fuelEntry = try req.content.decode([FuelEntry].self) + try await fuelEntry.create(on: req.db) return fuelEntry } diff --git a/Sources/App/Migrations/CreateFuelEntry.swift b/Sources/App/Migrations/CreateFuelEntry.swift index 1b61d55..99a6215 100755 --- a/Sources/App/Migrations/CreateFuelEntry.swift +++ b/Sources/App/Migrations/CreateFuelEntry.swift @@ -32,3 +32,23 @@ struct CreateFuelEntry: AsyncMigration { } } +struct AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage: AsyncMigration { + func prepare(on database: Database) async throws { + try await database.schema("fuel-entries") + .field("latitude", .double) + .field("longitude", .double) + .deleteField("gps_location") + .deleteField("image") + .update() + } + + func revert(on database: Database) async throws { + try await database.schema("fuel-entries") + .deleteField("latitude") + .deleteField("longitude") + .field("gps_location", .string) + .field("image", .string) + .update() + } +} + diff --git a/Sources/App/Models/FuelEntry.swift b/Sources/App/Models/FuelEntry.swift index 073d0ef..cfa0099 100755 --- a/Sources/App/Models/FuelEntry.swift +++ b/Sources/App/Models/FuelEntry.swift @@ -20,11 +20,14 @@ final class FuelEntry: Model, Content { @Field(key: "user_id") var userID: String - @Field(key: "gps_location") - var gpsLocation: String + @Field(key: "latitude") + var latitude: String + + @Field(key: "longitude") + var longitude: String - @Field(key: "image") - var image: String +// @Field(key: "image") +// var image: String @Field(key: "product") var product: String @@ -52,12 +55,13 @@ final class FuelEntry: Model, Content { init() {} - init(id: UUID? = nil, timestamp: Date, userID: String, gpsLocation: String, image: String, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool) { + init(id: UUID? = nil, timestamp: Date, userID: String, latitude: String, longitude: String, product: String, make: String, model: String, serialNumber: String, tankID: String, fuelAmount: Double, fuelType: String, isOverride: Bool) { self.id = id self.timestamp = timestamp self.userID = userID - self.gpsLocation = gpsLocation - self.image = image + self.latitude = latitude + self.longitude = longitude +// self.image = image self.product = product self.make = make self.model = model @@ -68,38 +72,3 @@ final class FuelEntry: Model, Content { self.isOverride = isOverride } } - - -//extension FuelEntry: Content {} -//extension FuelEntry: Migration { -// func prepare(on database: FluentKit.Database) -> NIOCore.EventLoopFuture { -// <#code#> -// } -// -// func revert(on database: FluentKit.Database) -> NIOCore.EventLoopFuture { -// <#code#> -// } -// func prepare(on database: Database) -> EventLoopFuture { -// return database.schema("fuel_entries") -// .id() -// .field("timestamp", .datetime) -// .field("user_id", .string) -// .field("gps_location", .string) -// .field("image", .string) -// .field("product", .string) -// .field("make", .string) -// .field("model", .string) -// .field("serial_number", .string) -// .field("tank_id", .string) -// .field("fuel_amount", .double) -// .field("fuel_type", .string) -// .field("is_override", .bool) -// .create() -// } -// -// func revert(on database: Database) -> EventLoopFuture { -// return database.schema("fuel_entries").delete() -// } -//} -//extension FuelEntry: Parameter {} - diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 63e3a31..713844e 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -8,20 +8,9 @@ public func configure(_ app: Application) async throws { // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) app.databases.use(.postgres(hostname: "localhost", username: "mcarman", password: "@ng31F@rm0823262", database: "fueling"), as: .psql) - -// app.databases.use(.postgres( -// hostname: Environment.get("localhost") ?? "localhost", -// port: Environment.get("5432").flatMap(Int.init(_:)) ?? PostgresConfiguration.ianaPortNumber, -// username: Environment.get("mcarman") ?? "vapor_username", -// //username: Environment.get("bryckman") ?? "vapor_username", -// password: Environment.get("@ng31F@rm0823262" ?? "vapor_password"), -// //password: Environment.get("55H1ck0ry1669") ?? "vapor_password", -// database: Environment.get("fueling") ?? "vapor_database" -// ), as: .psql) app.http.server.configuration.port = 3001 - -// app.migrations.add(CreateTodo()) app.migrations.add(CreateFuelEntry()) + app.migrations.add(AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage()) try await app.autoMigrate().get() // register routes