Update to accept array on POST request.master
| @@ -21,9 +21,9 @@ struct FuelEntryController: RouteCollection { | |||||
| try await FuelEntry.query(on: req.db).all() | 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 | return fuelEntry | ||||
| } | } | ||||
| @@ -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() | |||||
| } | |||||
| } | |||||
| @@ -20,11 +20,14 @@ final class FuelEntry: Model, Content { | |||||
| @Field(key: "user_id") | @Field(key: "user_id") | ||||
| var userID: String | 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") | @Field(key: "product") | ||||
| var product: String | var product: String | ||||
| @@ -52,12 +55,13 @@ final class FuelEntry: Model, Content { | |||||
| init() {} | 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.id = id | ||||
| self.timestamp = timestamp | self.timestamp = timestamp | ||||
| self.userID = userID | self.userID = userID | ||||
| self.gpsLocation = gpsLocation | |||||
| self.image = image | |||||
| self.latitude = latitude | |||||
| self.longitude = longitude | |||||
| // self.image = image | |||||
| self.product = product | self.product = product | ||||
| self.make = make | self.make = make | ||||
| self.model = model | self.model = model | ||||
| @@ -68,38 +72,3 @@ final class FuelEntry: Model, Content { | |||||
| self.isOverride = isOverride | self.isOverride = isOverride | ||||
| } | } | ||||
| } | } | ||||
| //extension FuelEntry: Content {} | |||||
| //extension FuelEntry: Migration { | |||||
| // func prepare(on database: FluentKit.Database) -> NIOCore.EventLoopFuture<Void> { | |||||
| // <#code#> | |||||
| // } | |||||
| // | |||||
| // func revert(on database: FluentKit.Database) -> NIOCore.EventLoopFuture<Void> { | |||||
| // <#code#> | |||||
| // } | |||||
| // func prepare(on database: Database) -> EventLoopFuture<Void> { | |||||
| // 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<Void> { | |||||
| // return database.schema("fuel_entries").delete() | |||||
| // } | |||||
| //} | |||||
| //extension FuelEntry: Parameter {} | |||||
| @@ -8,20 +8,9 @@ public func configure(_ app: Application) async throws { | |||||
| // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory)) | // 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: "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.http.server.configuration.port = 3001 | ||||
| // app.migrations.add(CreateTodo()) | |||||
| app.migrations.add(CreateFuelEntry()) | app.migrations.add(CreateFuelEntry()) | ||||
| app.migrations.add(AddLatitudeAndLongitudeAndRemoveGpsLocationAndImage()) | |||||
| try await app.autoMigrate().get() | try await app.autoMigrate().get() | ||||
| // register routes | // register routes | ||||