Add first database version and model
This commit is contained in:
32
Sources/App/Model/Migrations/UserTableMigration.swift
Normal file
32
Sources/App/Model/Migrations/UserTableMigration.swift
Normal file
@ -0,0 +1,32 @@
|
||||
import FluentSQLiteDriver
|
||||
|
||||
struct UserTableMigration: Migration {
|
||||
|
||||
func prepare(on database: FluentSQLiteDriver.Database) -> EventLoopFuture<Void> {
|
||||
let one = database.schema(User.schema)
|
||||
.id()
|
||||
.field(User.Key.name.key, .string, .required)
|
||||
.field(User.Key.hash.key, .string, .required)
|
||||
.field(User.Key.points.key, .int, .required)
|
||||
.field(User.Key.table.key, .uuid, .references(Table.schema, Table.Key.id.key))
|
||||
.create()
|
||||
let two = database.enum(Table.Key.language.rawValue)
|
||||
.case(SupportedLanguage.german.rawValue)
|
||||
.case(SupportedLanguage.english.rawValue)
|
||||
.create()
|
||||
|
||||
let three = database.enum(Table.Key.language.rawValue).read().flatMap { lang in
|
||||
database.schema(Table.schema)
|
||||
.id()
|
||||
.field(Table.Key.name.key, .string, .required)
|
||||
.field(Table.Key.isPublic.key, .bool, .required)
|
||||
.field(Table.Key.language.key, lang, .required)
|
||||
.create()
|
||||
}
|
||||
return one.and(two).and(three).map { _ in }
|
||||
}
|
||||
|
||||
func revert(on database: FluentSQLiteDriver.Database) -> EventLoopFuture<Void> {
|
||||
database.eventLoop.makeCompletedFuture(.success(()))
|
||||
}
|
||||
}
|
52
Sources/App/Model/Table.swift
Normal file
52
Sources/App/Model/Table.swift
Normal file
@ -0,0 +1,52 @@
|
||||
import FluentSQLiteDriver
|
||||
import Vapor
|
||||
|
||||
/// A registered user
|
||||
class Table: Model {
|
||||
|
||||
enum Key: String {
|
||||
case id = "id"
|
||||
case name = "name"
|
||||
case isPublic = "public"
|
||||
case language = "language"
|
||||
|
||||
var key: FieldKey {
|
||||
.init(stringLiteral: rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the SQLite table
|
||||
static let schema = "table"
|
||||
|
||||
/// The unique identifier for this table.
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
/// The user's full name.
|
||||
@Field(key: Key.name.key)
|
||||
var name: String
|
||||
|
||||
/// The players sitting at the table
|
||||
@Children(for: \.$table)
|
||||
var players: [User]
|
||||
|
||||
@Field(key: Key.isPublic.key)
|
||||
var isPublic: Bool
|
||||
|
||||
@Enum(key: Key.language.key)
|
||||
var language: SupportedLanguage
|
||||
|
||||
required init() { }
|
||||
|
||||
/// Creates a new table.
|
||||
init(id: UUID? = nil, name: String, isPublic: Bool = true, language: SupportedLanguage = .english) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.isPublic = isPublic
|
||||
self.language = language
|
||||
}
|
||||
|
||||
var stringId: String {
|
||||
"\(id!)"
|
||||
}
|
||||
}
|
55
Sources/App/Model/User.swift
Normal file
55
Sources/App/Model/User.swift
Normal file
@ -0,0 +1,55 @@
|
||||
import FluentSQLiteDriver
|
||||
import Vapor
|
||||
|
||||
|
||||
|
||||
/// A registered user
|
||||
final class User: Model {
|
||||
|
||||
enum Key: String {
|
||||
case id = "id"
|
||||
case name = "name"
|
||||
case hash = "hash"
|
||||
case points = "points"
|
||||
case table = "table_id"
|
||||
|
||||
var key: FieldKey {
|
||||
.init(stringLiteral: rawValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the SQLite table
|
||||
static let schema = "user"
|
||||
|
||||
/// The unique identifier for this user.
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
/// The user's full name.
|
||||
@Field(key: Key.name.key)
|
||||
var name: String
|
||||
|
||||
/// The hash of the user's password
|
||||
@Field(key: Key.hash.key)
|
||||
var passwordHash: String
|
||||
|
||||
/// The user's total points
|
||||
@Field(key: Key.points.key)
|
||||
var points: Int
|
||||
|
||||
// Example of an optional parent relation.
|
||||
@OptionalParent(key: Key.table.key)
|
||||
var table: Table?
|
||||
|
||||
init() { }
|
||||
|
||||
/// Creates a new user.
|
||||
init(id: UUID? = nil, name: String, hash: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.passwordHash = hash
|
||||
self.points = 0
|
||||
}
|
||||
}
|
||||
|
||||
//extension User: Content { }
|
Reference in New Issue
Block a user