Skip to content

Instantly share code, notes, and snippets.

@joshuawright11
Created March 9, 2020 17:09
Show Gist options
  • Save joshuawright11/47e6e920e3c32c12798d3fc8d51972ff to your computer and use it in GitHub Desktop.
Save joshuawright11/47e6e920e3c32c12798d3fc8d51972ff to your computer and use it in GitHub Desktop.
DB wrapper for `Trip`
import FluentPostgresDriver
import RoamShared
import Vapor
final class TripModel: Model {
static var schema: String = "trips"
@ID(key: .id)
var id: UUID?
/// References
@Parent(key: "userID")
var user: UserModel
@Parent(key: "originID")
var origin: PlaceModel
@Parent(key: "destinationID")
var destination: PlaceModel
/// Date properties
@Field(key: "dotwStart")
var dotwStart: DOTW?
@Field(key: "dotwEnd")
var dotwEnd: DOTW?
@Field(key: "additionalWeeks")
var additionalWeeks: Int?
/// Filter properties
@Field(key: "outboundDepartureRange")
var outboundDepartureRange: TimeFilter.Range?
@Field(key: "outboundDepartureTime")
var outboundDepartureTime: MinuteOfDay?
@Field(key: "outboundArrivalRange")
var outboundArrivalRange: TimeFilter.Range?
@Field(key: "outboundArrivalTime")
var outboundArrivalTime: MinuteOfDay?
@Field(key: "returnDepartureRange")
var returnDepartureRange: TimeFilter.Range?
@Field(key: "returnDepartureTime")
var returnDepartureTime: MinuteOfDay?
@Field(key: "returnArrivalRange")
var returnArrivalRange: TimeFilter.Range?
@Field(key: "returnArrivalTime")
var returnArrivalTime: MinuteOfDay?
@Field(key: "maxStops")
var maxStops: Int?
init() {}
init(user: UserModel, origin: PlaceModel, destination: PlaceModel) throws {
self.$user.id = try user.requireID()
self.$origin.id = try origin.requireID()
self.$destination.id = try destination.requireID()
}
}
struct CreateTrip: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema(TripModel.schema)
/// Main properties
.field("id", .uuid, .identifier(auto: false))
.field("userID", .uuid)
.foreignKey("userID", references: UserModel.schema, "id", onDelete: .cascade)
.field("originID", .uuid)
.foreignKey("originID", references: PlaceModel.schema, "id")
.field("destinationID", .uuid)
.foreignKey("destinationID", references: PlaceModel.schema, "id")
/// Date properties
.field("dotwStart", .int)
.field("dotwEnd", .int)
.field("additionalWeeks", .int)
/// Filter properties
.field("outboundDepartureRange", .int)
.field("outboundDepartureTime", .int)
.field("outboundArrivalRange", .int)
.field("outboundArrivalTime", .int)
.field("returnDepartureRange", .int)
.field("returnDepartureTime", .int)
.field("returnArrivalRange", .int)
.field("returnArrivalTime", .int)
.field("maxStops", .int)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema(TripModel.schema).delete()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment