feat: Initial working api routes.
This commit is contained in:
131
Sources/App/Models/Employee.swift
Normal file
131
Sources/App/Models/Employee.swift
Normal file
@@ -0,0 +1,131 @@
|
||||
import Fluent
|
||||
import struct Foundation.UUID
|
||||
import Vapor
|
||||
|
||||
final class Employee: Model, @unchecked Sendable {
|
||||
|
||||
static let schema = "employee"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "first_name")
|
||||
var firstName: String
|
||||
|
||||
@Field(key: "last_name")
|
||||
var lastName: String
|
||||
|
||||
@Field(key: "is_active")
|
||||
var active: Bool
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
id: UUID? = nil,
|
||||
firstName: String,
|
||||
lastName: String,
|
||||
active: Bool
|
||||
) {
|
||||
self.id = id
|
||||
self.firstName = firstName
|
||||
self.lastName = lastName
|
||||
self.active = active
|
||||
}
|
||||
|
||||
func toDTO() -> DTO {
|
||||
.init(id: id, firstName: $firstName.value, lastName: $lastName.value, active: $active.value)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Update) {
|
||||
if let firstName = updates.firstName {
|
||||
self.firstName = firstName
|
||||
}
|
||||
if let lastName = updates.lastName {
|
||||
self.lastName = lastName
|
||||
}
|
||||
if let active = updates.active {
|
||||
self.active = active
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
extension Employee {
|
||||
|
||||
struct Create: Content {
|
||||
let firstName: String
|
||||
let lastName: String
|
||||
let active: Bool?
|
||||
|
||||
func toModel() -> Employee {
|
||||
.init(firstName: firstName, lastName: lastName, active: active ?? true)
|
||||
}
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
|
||||
var id: UUID?
|
||||
var firstName: String?
|
||||
var lastName: String?
|
||||
var active: Bool?
|
||||
|
||||
func toModel() -> Employee {
|
||||
let model = Employee()
|
||||
|
||||
model.id = id
|
||||
if let firstName {
|
||||
model.firstName = firstName
|
||||
}
|
||||
if let lastName {
|
||||
model.lastName = lastName
|
||||
}
|
||||
if let active {
|
||||
model.active = active
|
||||
}
|
||||
return model
|
||||
}
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
|
||||
let name = "CreateEmployee"
|
||||
|
||||
func prepare(on database: Database) async throws {
|
||||
try await database.schema(Employee.schema)
|
||||
.id()
|
||||
.field("first_name", .string, .required)
|
||||
.field("last_name", .string, .required)
|
||||
.field("is_active", .bool, .required, .sql(.default(true)))
|
||||
.unique(on: "first_name", "last_name")
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: Database) async throws {
|
||||
try await database.schema(Employee.schema).delete()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Update: Content {
|
||||
var firstName: String?
|
||||
var lastName: String?
|
||||
var active: Bool?
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Validations
|
||||
|
||||
extension Employee.Create: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("firstName", as: String.self, is: !.empty)
|
||||
validations.add("lastName", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
|
||||
extension Employee.Update: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("firstName", as: String?.self, is: .nil || !.empty, required: false)
|
||||
validations.add("lastName", as: String?.self, is: .nil || !.empty, required: false)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user