Created
September 29, 2017 13:34
-
-
Save bennibau/682f7f950611b88ab70bc26e16699f57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Capital.swift | |
// App | |
// | |
// Created by Benjamin Baumann on 28.09.17. | |
// | |
import Vapor | |
import FluentProvider | |
import HTTP | |
final class Capital: Model { | |
let storage = Storage() | |
/// The name of the Capital | |
var name: String | |
/// one to one relation to country | |
var country_id : Identifier? | |
/// Creates a new Capital | |
init(name: String) { | |
self.name = name | |
} | |
/// Initializes the Capital from the | |
/// database row | |
init(row: Row) throws { | |
name = try row.get("name") | |
country_id = try row.get("country_id") | |
} | |
// Serializes the Capital to the database | |
func makeRow() throws -> Row { | |
var row = Row() | |
try row.set("name", name) | |
try row.set("country_id",country_id) | |
return row | |
} | |
} | |
// MARK: Fluent Preparation | |
extension Capital: 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: Country.self, optional: true, unique: false, foreignIdKey: "country_id", foreignKeyName: "country_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 Capital: 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) | |
return json | |
} | |
} | |
// MARK: HTTP | |
// This allows Post models to be returned | |
// directly in route closures | |
extension Capital: ResponseRepresentable { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment