Skip to content

Instantly share code, notes, and snippets.

@bennibau
Created September 29, 2017 13:33
Show Gist options
  • Save bennibau/d53a2a8628b7ef41b60059a3e5f4b5b2 to your computer and use it in GitHub Desktop.
Save bennibau/d53a2a8628b7ef41b60059a3e5f4b5b2 to your computer and use it in GitHub Desktop.
//
// Country.swift
// App
//
// Created by Benjamin Baumann on 28.09.17.
//
import Vapor
import FluentProvider
import HTTP
final class Country: Model {
let storage = Storage()
/// The name of the country
var name: String
/// one to one relation to capital
var capital_id : Identifier?
/// Creates a new Country
init(name: String) {
self.name = name
}
/// Initializes the Country from the
/// database row
init(row: Row) throws {
name = try row.get("name")
capital_id = try row.get("capital_id")
}
// Serializes the Country to the database
func makeRow() throws -> Row {
var row = Row()
try row.set("name", name)
try row.set("capital_id", capital_id)
return row
}
}
// MARK: Fluent Preparation
extension Country: Preparation {
/// Prepares a table/collection in the database
/// for storing Countries
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string("name")
builder.foreignId(for: Capital.self, optional: true, unique: false, foreignIdKey: "capital_id", foreignKeyName: "capital_id")
}
}
/// Undoes what was done in `prepare`
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
// MARK: JSON
// How the model converts from / to JSON.
extension Country: JSONConvertible {
convenience init(json: JSON) throws {
try self.init(
name: json.get("name")
)
}
func makeJSON() throws -> JSON {
var json = JSON()
try json.set("id", id)
try json.set("name", name)
try json.set("capital_id", capital_id)
return json
}
}
// MARK: HTTP
// This allows Post models to be returned
// directly in route closures
extension Country: ResponseRepresentable { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment