浏览代码

Update to add latitude and longitude and remove gpsLocation and image.

Update to accept array on POST request.
master
admin 3 年前
父节点
当前提交
18a047eb5d
共有 4 个文件被更改,包括 35 次插入57 次删除
  1. +3
    -3
      Sources/App/Controllers/FuelEntryController.swift
  2. +20
    -0
      Sources/App/Migrations/CreateFuelEntry.swift
  3. +11
    -42
      Sources/App/Models/FuelEntry.swift
  4. +1
    -12
      Sources/App/configure.swift

+ 3
- 3
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
}



+ 20
- 0
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()
}
}


+ 11
- 42
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<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 {}


+ 1
- 12
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


正在加载...
取消
保存