Created
August 26, 2017 16:21
-
-
Save JoeyBodnar/74e9230e249a85593810f6716c44d9aa to your computer and use it in GitHub Desktop.
This file contains 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
import Vapor | |
import FluentProvider | |
import HTTP | |
final class Pet: Model { | |
let storage = Storage() | |
var name: String | |
static let idKey = "id" | |
static let nameKey = "name" | |
init(name: String) { | |
self.name = name | |
} | |
init(row: Row) throws { | |
name = try row.get(Pet.nameKey) | |
} | |
func makeRow() throws -> Row { | |
var row = Row() | |
try row.set(Pet.nameKey, name) | |
return row | |
} | |
} | |
extension Pet: Preparation { | |
static func prepare(_ database: Database) throws { | |
try database.create(self) { builder in | |
builder.id() | |
builder.string(Pet.nameKey) | |
} | |
} | |
static func revert(_ database: Database) throws { | |
try database.delete(self) | |
} | |
} | |
extension Pet: JSONConvertible { | |
convenience init(json: JSON) throws { | |
try self.init( | |
name: json.get(Pet.nameKey) | |
) | |
} | |
func makeJSON() throws -> JSON { | |
var json = JSON() | |
try json.set(Pet.idKey, id) | |
try json.set(Pet.nameKey, name) | |
return json | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment