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)
|
||||
}
|
||||
}
|
||||
150
Sources/App/Models/PurchaseOrder.swift
Normal file
150
Sources/App/Models/PurchaseOrder.swift
Normal file
@@ -0,0 +1,150 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
final class PurchaseOrder: Model, Content, @unchecked Sendable {
|
||||
static let schema = "purchase_order"
|
||||
|
||||
@ID(custom: "id", generatedBy: .database)
|
||||
var id: Int?
|
||||
|
||||
@Field(key: "work_order")
|
||||
var workOrder: Int?
|
||||
|
||||
@Field(key: "materials")
|
||||
var materials: String
|
||||
|
||||
@Field(key: "customer")
|
||||
var customer: String
|
||||
|
||||
@Field(key: "truck_stock")
|
||||
var truckStock: Bool
|
||||
|
||||
@Parent(key: "created_by_id")
|
||||
var createdBy: User
|
||||
|
||||
@Parent(key: "created_for_id")
|
||||
var createdFor: Employee
|
||||
|
||||
@Parent(key: "vendor_branch_id")
|
||||
var vendorBranch: VendorBranch
|
||||
|
||||
@Timestamp(key: "created_at", on: .create)
|
||||
var createdAt: Date?
|
||||
|
||||
@Timestamp(key: "updated_at", on: .update)
|
||||
var updatedAt: Date?
|
||||
|
||||
init() {}
|
||||
|
||||
init(
|
||||
id: Int? = nil,
|
||||
workOrder: Int? = nil,
|
||||
materials: String,
|
||||
customer: String,
|
||||
truckStock: Bool,
|
||||
createdByID: User.IDValue,
|
||||
createdForID: Employee.IDValue,
|
||||
vendorBranchID: VendorBranch.IDValue,
|
||||
createdAt: Date? = nil,
|
||||
updatedAt: Date? = nil
|
||||
) {
|
||||
self.id = id
|
||||
self.workOrder = workOrder
|
||||
self.materials = materials
|
||||
self.customer = customer
|
||||
self.truckStock = truckStock
|
||||
$createdBy.id = createdByID
|
||||
$createdFor.id = createdForID
|
||||
$vendorBranch.id = vendorBranchID
|
||||
self.createdAt = createdAt
|
||||
self.updatedAt = updatedAt
|
||||
}
|
||||
|
||||
func toDTO() -> DTO {
|
||||
.init(
|
||||
id: id,
|
||||
workOrder: workOrder,
|
||||
materials: materials,
|
||||
customer: customer,
|
||||
truckStock: truckStock,
|
||||
createdBy: $createdBy.value?.toDTO(),
|
||||
createdFor: $createdFor.value?.toDTO(),
|
||||
vendorBranch: $vendorBranch.value,
|
||||
createdAt: createdAt,
|
||||
updatedAt: updatedAt
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension PurchaseOrder {
|
||||
|
||||
struct Create: Content {
|
||||
let workOrder: Int?
|
||||
let materials: String
|
||||
let customer: String
|
||||
let truckStock: Bool?
|
||||
let createdForID: Employee.IDValue
|
||||
let vendorBranchID: VendorBranch.IDValue
|
||||
|
||||
func toModel(createdByID: User.IDValue) -> PurchaseOrder {
|
||||
.init(
|
||||
id: nil,
|
||||
workOrder: workOrder,
|
||||
materials: materials,
|
||||
customer: customer,
|
||||
truckStock: truckStock ?? false,
|
||||
createdByID: createdByID,
|
||||
createdForID: createdForID,
|
||||
vendorBranchID: vendorBranchID,
|
||||
createdAt: nil,
|
||||
updatedAt: nil
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
let id: Int?
|
||||
let workOrder: Int?
|
||||
let materials: String
|
||||
let customer: String
|
||||
let truckStock: Bool
|
||||
let createdBy: User.DTO?
|
||||
let createdFor: Employee.DTO?
|
||||
let vendorBranch: VendorBranch?
|
||||
let createdAt: Date?
|
||||
let updatedAt: Date?
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
|
||||
let name = "CreatePurchaseOrder"
|
||||
|
||||
func prepare(on database: any Database) async throws {
|
||||
try await database.schema(PurchaseOrder.schema)
|
||||
.field("id", .int, .identifier(auto: true))
|
||||
.field("work_order", .int)
|
||||
.field("customer", .string, .required)
|
||||
.field("materials", .string, .required)
|
||||
.field("truck_stock", .bool, .required)
|
||||
.field("created_by_id", .uuid, .required, .references(User.schema, "id"))
|
||||
.field("created_for_id", .uuid, .required, .references(Employee.schema, "id"))
|
||||
.field("vendor_branch_id", .uuid, .required, .references(VendorBranch.schema, "id"))
|
||||
.field("created_at", .datetime)
|
||||
.field("updated_at", .datetime)
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: any Database) async throws {
|
||||
try await database.schema(PurchaseOrder.schema).delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension PurchaseOrder.Create: Validatable {
|
||||
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("materials", as: String.self, is: !.empty)
|
||||
validations.add("customer", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import Fluent
|
||||
import struct Foundation.UUID
|
||||
|
||||
/// Property wrappers interact poorly with `Sendable` checking, causing a warning for the `@ID` property
|
||||
/// It is recommended you write your model with sendability checking on and then suppress the warning
|
||||
/// afterwards with `@unchecked Sendable`.
|
||||
final class Todo: Model, @unchecked Sendable {
|
||||
static let schema = "todos"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "title")
|
||||
var title: String
|
||||
|
||||
init() { }
|
||||
|
||||
init(id: UUID? = nil, title: String) {
|
||||
self.id = id
|
||||
self.title = title
|
||||
}
|
||||
|
||||
func toDTO() -> TodoDTO {
|
||||
.init(
|
||||
id: self.id,
|
||||
title: self.$title.value
|
||||
)
|
||||
}
|
||||
}
|
||||
92
Sources/App/Models/User.swift
Normal file
92
Sources/App/Models/User.swift
Normal file
@@ -0,0 +1,92 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
final class User: Model, @unchecked Sendable {
|
||||
static let schema = "user"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "username")
|
||||
var username: String
|
||||
|
||||
@Field(key: "email")
|
||||
var email: String
|
||||
|
||||
@Field(key: "password_hash")
|
||||
var passwordHash: String
|
||||
|
||||
init() {}
|
||||
|
||||
init(id: UUID? = nil, username: String, email: String, passwordHash: String) {
|
||||
self.id = id
|
||||
self.username = username
|
||||
self.email = email
|
||||
self.passwordHash = passwordHash
|
||||
}
|
||||
|
||||
func toDTO() -> DTO {
|
||||
.init(id: id, username: $username.value, email: $email.value)
|
||||
}
|
||||
|
||||
func generateToken() throws -> UserToken {
|
||||
try .init(
|
||||
value: [UInt8].random(count: 16).base64,
|
||||
userID: requireID()
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension User {
|
||||
|
||||
struct Create: Content {
|
||||
var username: String
|
||||
var email: String
|
||||
var password: String
|
||||
var confirmPassword: String
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
let id: UUID?
|
||||
let username: String?
|
||||
let email: String?
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateUser"
|
||||
|
||||
func prepare(on database: any Database) async throws {
|
||||
try await database.schema(User.schema)
|
||||
.id()
|
||||
.field("username", .string, .required)
|
||||
.field("email", .string, .required)
|
||||
.field("password_hash", .string, .required)
|
||||
.unique(on: "email", "username")
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: any Database) async throws {
|
||||
try await database.schema(User.schema).delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension User: ModelAuthenticatable {
|
||||
static let usernameKey = \User.$email
|
||||
static let passwordHashKey = \User.$passwordHash
|
||||
|
||||
func verify(password: String) throws -> Bool {
|
||||
try Bcrypt.verify(password, created: passwordHash)
|
||||
}
|
||||
}
|
||||
|
||||
extension User: ModelSessionAuthenticatable {}
|
||||
|
||||
extension User.Create: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("username", as: String.self, is: !.empty)
|
||||
validations.add("email", as: String.self, is: .email)
|
||||
validations.add("password", as: String.self, is: .count(8...))
|
||||
}
|
||||
}
|
||||
51
Sources/App/Models/UserToken.swift
Normal file
51
Sources/App/Models/UserToken.swift
Normal file
@@ -0,0 +1,51 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
final class UserToken: Model, Content, @unchecked Sendable {
|
||||
|
||||
static let schema = "user_token"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "value")
|
||||
var value: String
|
||||
|
||||
@Parent(key: "user_id")
|
||||
var user: User
|
||||
|
||||
init() {}
|
||||
|
||||
init(id: UUID? = nil, value: String, userID: User.IDValue) {
|
||||
self.id = id
|
||||
self.value = value
|
||||
$user.id = userID
|
||||
}
|
||||
}
|
||||
|
||||
extension UserToken {
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateUserToken"
|
||||
|
||||
func prepare(on database: any Database) async throws {
|
||||
try await database.schema(UserToken.schema)
|
||||
.id()
|
||||
.field("value", .string, .required)
|
||||
.field("user_id", .uuid, .required, .references(User.schema, "id"))
|
||||
.unique(on: "value")
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: any Database) async throws {
|
||||
try await database.schema(UserToken.schema).delete()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UserToken: ModelTokenAuthenticatable {
|
||||
static let valueKey = \UserToken.$value
|
||||
static let userKey = \UserToken.$user
|
||||
|
||||
var isValid: Bool { true }
|
||||
}
|
||||
102
Sources/App/Models/Vendor.swift
Normal file
102
Sources/App/Models/Vendor.swift
Normal file
@@ -0,0 +1,102 @@
|
||||
import Fluent
|
||||
import struct Foundation.UUID
|
||||
import Vapor
|
||||
|
||||
// The primary database model.
|
||||
final class Vendor: Model, @unchecked Sendable {
|
||||
|
||||
static let schema = "vendor"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "name")
|
||||
var name: String
|
||||
|
||||
@Children(for: \.$vendor)
|
||||
var branches: [VendorBranch]
|
||||
|
||||
init() {}
|
||||
|
||||
init(id: UUID? = nil, name: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
}
|
||||
|
||||
func toDTO(includeBranches: Bool? = nil) -> DTO {
|
||||
.init(
|
||||
id: id,
|
||||
name: $name.value,
|
||||
branches: ($branches.value != nil && $branches.value!.count > 0)
|
||||
? $branches.value!.map { $0.toDTO() }
|
||||
: (includeBranches == true) ? [] : nil
|
||||
)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Update) {
|
||||
name = updates.name
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helpers.
|
||||
|
||||
extension Vendor {
|
||||
struct Create: Content {
|
||||
var name: String
|
||||
|
||||
func toModel() -> Vendor {
|
||||
.init(name: name)
|
||||
}
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
|
||||
var id: UUID?
|
||||
var name: String?
|
||||
var branches: [VendorBranch.DTO]?
|
||||
|
||||
func toModel() -> Vendor {
|
||||
let model = Vendor()
|
||||
model.id = id
|
||||
if let name {
|
||||
model.name = name
|
||||
}
|
||||
return model
|
||||
}
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateVendor"
|
||||
|
||||
func prepare(on database: Database) async throws {
|
||||
try await database.schema(Vendor.schema)
|
||||
.id()
|
||||
.field("name", .string, .required)
|
||||
.unique(on: "name")
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: Database) async throws {
|
||||
try await database.schema(Vendor.schema).delete()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Update: Content {
|
||||
var name: String
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Validations
|
||||
|
||||
extension Vendor.Create: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
|
||||
extension Vendor.Update: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
102
Sources/App/Models/VendorBranch.swift
Normal file
102
Sources/App/Models/VendorBranch.swift
Normal file
@@ -0,0 +1,102 @@
|
||||
import Fluent
|
||||
import struct Foundation.UUID
|
||||
import Vapor
|
||||
|
||||
final class VendorBranch: Model, @unchecked Sendable {
|
||||
|
||||
static let schema = "vendor_branch"
|
||||
|
||||
@ID(key: .id)
|
||||
var id: UUID?
|
||||
|
||||
@Field(key: "name")
|
||||
var name: String
|
||||
|
||||
@Parent(key: "vendor_id")
|
||||
var vendor: Vendor
|
||||
|
||||
init() {}
|
||||
|
||||
init(id: UUID? = nil, name: String, vendorId: Vendor.IDValue) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
$vendor.id = vendorId
|
||||
}
|
||||
|
||||
func toDTO() -> DTO {
|
||||
.init(id: id, name: $name.value, vendorId: $vendor.id)
|
||||
}
|
||||
|
||||
func applyUpdates(_ updates: Update) {
|
||||
name = updates.name
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
extension VendorBranch {
|
||||
struct Create: Content {
|
||||
var name: String
|
||||
|
||||
func toModel() -> VendorBranch {
|
||||
let model = VendorBranch()
|
||||
model.name = name
|
||||
return model
|
||||
}
|
||||
}
|
||||
|
||||
struct DTO: Content {
|
||||
var id: UUID?
|
||||
var name: String?
|
||||
var vendorId: Vendor.IDValue?
|
||||
|
||||
func toModel() -> VendorBranch {
|
||||
let model = VendorBranch()
|
||||
|
||||
model.id = id
|
||||
if let name {
|
||||
model.name = name
|
||||
}
|
||||
if let vendorId {
|
||||
model.$vendor.id = vendorId
|
||||
}
|
||||
return model
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Migrate: AsyncMigration {
|
||||
let name = "CreateVendorBranch"
|
||||
|
||||
func prepare(on database: Database) async throws {
|
||||
try await database.schema(VendorBranch.schema)
|
||||
.id()
|
||||
.field("name", .string, .required)
|
||||
.field("vendor_id", .uuid, .required, .references("vendor", "id"))
|
||||
.create()
|
||||
}
|
||||
|
||||
func revert(on database: Database) async throws {
|
||||
try await database.schema(VendorBranch.schema).delete()
|
||||
}
|
||||
}
|
||||
|
||||
struct Update: Content {
|
||||
var name: String
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Validations
|
||||
|
||||
extension VendorBranch.Create: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
|
||||
extension VendorBranch.Update: Validatable {
|
||||
static func validations(_ validations: inout Validations) {
|
||||
validations.add("name", as: String.self, is: !.empty)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user