Files
vapor-po/Sources/SharedModels/Routes/ViewRoute.swift

219 lines
5.3 KiB
Swift

import CasePathsCore
import Foundation
@preconcurrency import URLRouting
public enum ViewRoute: Sendable {
case employee(EmployeeRoute)
case login
case purchaseOrder(PurchaseOrderRoute)
case select(SelectRoute)
case user(UserRoute)
case vendor(VendorRoute)
case vendorBranch(VendorBranchRoute)
public static let router = OneOf {
Route(.case(Self.employee)) { EmployeeRoute.router }
Route(.case(Self.login)) {
Path { "login" }
Method.get
}
Route(.case(Self.purchaseOrder)) { PurchaseOrderRoute.router }
Route(.case(Self.select)) { SelectRoute.router }
Route(.case(Self.user)) { UserRoute.router }
Route(.case(Self.vendor)) { VendorRoute.router }
Route(.case(Self.vendorBranch)) { VendorBranchRoute.router }
}
public enum EmployeeRoute: Sendable {
case form
case shared(SharedEmployeeRoute)
public static func delete(id: Employee.ID) -> Self {
.shared(.delete(id: id))
}
public static func get(id: Employee.ID) -> Self {
.shared(.get(id: id))
}
public static var index: Self { .shared(.index) }
static let rootPath = "employees"
public static let router = OneOf {
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.shared)) {
SharedEmployeeRoute.router
}
}
}
public enum PurchaseOrderRoute: Sendable {
case form
case search(Search)
case shared(SharedPurchaseOrderRoute)
static let rootPath = "purchase-orders"
public static let router = OneOf {
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.search)) {
Search.router
}
Route(.case(Self.shared)) {
SharedPurchaseOrderRoute.router
}
}
public enum Search: Sendable {
case index(context: Context? = nil, table: Bool? = nil)
case search(Request)
static let rootPath = Path { "purchase-orders"; "search" }
static let router = OneOf {
Route(.case(Search.index(context:table:))) {
rootPath
Method.get
Query {
Field("context", default: .employee) { Optionally { Search.Context.parser() } }
Field("table", default: nil) { Optionally { Bool.parser() } }
}
}
Route(.case(Search.search)) {
rootPath
Method.post
Body(.json(Search.Request.self))
}
}
public enum Context: String, Codable, CaseIterable, Sendable {
case employee
case customer
case vendor
}
public struct Request: Codable, Sendable {
public let context: Context
public let createdForID: Employee.ID?
public let customerSearch: String?
public let vendorBranchID: VendorBranch.ID?
public init(
context: Context,
createdForID: Employee.ID? = nil,
customerSearch: String? = nil,
vendorBranchID: VendorBranch.ID? = nil
) {
self.context = context
self.createdForID = createdForID
self.customerSearch = customerSearch
self.vendorBranchID = vendorBranchID
}
}
}
}
// TODO: Move into respective view routes.
public enum SelectRoute: Sendable {
case employee(context: Context)
case vendorBranches(context: Context)
public enum Context: String, Codable, Sendable, CaseIterable {
case purchaseOrderForm
case purchaseOrderSearch
}
static let rootPath = "select"
public static let router = OneOf {
Route(.case(Self.employee(context:))) {
Path { rootPath; "employee" }
Method.get
Query {
Field("context") { Context.parser() }
}
}
Route(.case(Self.vendorBranches(context:))) {
Path { rootPath; "vendor-branches" }
Method.get
Query {
Field("context") { Context.parser() }
}
}
}
}
public enum UserRoute: Sendable {
case form
case shared(SharedUserRoute)
static let rootPath = "users"
public static func delete(id: User.ID) -> Self {
.shared(.delete(id: id))
}
public static var index: Self { .shared(.index) }
public static let router = OneOf {
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.shared)) {
SharedUserRoute.router
}
}
}
public enum VendorRoute: Sendable {
case form
case shared(SharedVendorRoute)
static let rootPath = "vendors"
public static let router = OneOf {
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.shared)) {
SharedVendorRoute.router
}
}
}
// TODO: Add Select
public enum VendorBranchRoute: Sendable {
case index(vendorID: Vendor.ID)
case shared(SharedVendorBranchRoute)
public static func delete(id: VendorBranch.ID) -> Self {
.shared(.delete(id: id))
}
public static let router = OneOf {
Route(.case(Self.index(vendorID:))) {
Path { "vendors"; "branches" }
Method.get
Query {
Field("vendorID") { Vendor.ID.parser() }
}
}
Route(.case(Self.shared)) {
SharedVendorBranchRoute.router
}
}
}
}