Schafkopf-Server/Sources/App/Model/User.swift

74 lines
1.6 KiB
Swift
Raw Normal View History

2021-12-22 22:11:37 +01:00
import FluentSQLiteDriver
2022-10-11 11:51:52 +02:00
import FluentSQL
2021-12-22 22:11:37 +01:00
import Vapor
2022-10-11 11:51:52 +02:00
private extension FieldProperty {
convenience init(_ key: User.Key) {
self.init(key: key.key)
}
}
2021-12-22 22:11:37 +01:00
2022-10-11 11:51:52 +02:00
private extension OptionalParentProperty {
convenience init(_ key: User.Key) {
self.init(key: key.key)
}
}
2021-12-22 22:11:37 +01:00
/// 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"
2022-10-11 12:04:30 +02:00
case recoveryEmail = "email"
2021-12-22 22:11:37 +01:00
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.
2022-10-11 11:51:52 +02:00
@Field(.name)
2021-12-22 22:11:37 +01:00
var name: String
/// The hash of the user's password
2022-10-11 11:51:52 +02:00
@Field(.hash)
2021-12-22 22:11:37 +01:00
var passwordHash: String
2022-10-11 12:04:30 +02:00
@Field(.recoveryEmail)
var recoveryEmail: String?
2021-12-22 22:11:37 +01:00
/// The user's total points
2022-10-11 11:51:52 +02:00
@Field(.points)
2021-12-22 22:11:37 +01:00
var points: Int
2022-10-11 11:51:52 +02:00
/// The table to which this user currently belongs
@OptionalParent(.table)
2021-12-22 22:11:37 +01:00
var table: Table?
2022-10-11 12:04:15 +02:00
/// The optional password reset request associated with this user
@OptionalChild(for: \.$user)
var resetRequest: PasswordReset?
2021-12-22 22:11:37 +01:00
init() { }
/// Creates a new user.
2022-10-12 14:34:43 +02:00
init(id: UUID? = nil, name: String, hash: String, email: String? = nil) {
2021-12-22 22:11:37 +01:00
self.id = id
self.name = name
self.passwordHash = hash
self.points = 0
2022-10-12 14:34:43 +02:00
self.recoveryEmail = email
2021-12-22 22:11:37 +01:00
}
}