feat: Cleans up routes.

This commit is contained in:
2025-01-19 13:33:01 -05:00
parent 1c8748211c
commit b23dc6bf07
32 changed files with 958 additions and 1786 deletions

View File

@@ -38,37 +38,137 @@ public enum ApiRoute: Sendable {
}
public enum EmployeeApiRoute: Sendable {
case shared(SharedEmployeeRoute)
case create(Employee.Create)
case delete(id: Employee.ID)
case get(id: Employee.ID)
case index
case update(id: Employee.ID, updates: Employee.Update)
public static let router = Route(.case(Self.shared)) {
SharedEmployeeRoute.router
static let rootPath = "employees"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(Employee.Create.self))
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.delete(id:))) {
Path { rootPath; UUID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; UUID.parser() }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; UUID.parser() }
Method.put
Body(.json(Employee.Update.self))
}
}
}
public enum PurchaseOrderApiRoute: Sendable {
case shared(SharedPurchaseOrderRoute)
case create(PurchaseOrder.Create)
case delete(id: PurchaseOrder.ID)
case get(id: PurchaseOrder.ID)
case index
case page(page: Int, limit: Int)
public static let router = Route(.case(Self.shared)) {
SharedPurchaseOrderRoute.router
static let rootPath = "purchase-orders"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(PurchaseOrder.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Digits() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Digits() }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.page(page:limit:))) {
Path { rootPath; "next" }
Method.get
Query {
Field("page", default: 1) { Digits() }
Field("limit", default: 25) { Digits() }
}
}
}
}
// TODO: Add logout.
// TODO: Add login / logout.
public enum UserApiRoute: Sendable {
case shared(SharedUserRoute)
case create(User.Create)
case delete(id: User.ID)
case get(id: User.ID)
case index
case update(id: User.ID, updates: User.Update)
public static let router = Route(.case(Self.shared)) {
SharedUserRoute.router
static let rootPath = "users"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(User.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; User.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; User.ID.parser() }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; User.ID.parser() }
Method.patch
Body(.json(User.Update.self))
}
}
}
public enum VendorApiRoute: Sendable {
case index(withBranches: Bool?)
case shared(SharedVendorRoute)
case create(Vendor.Create)
case delete(id: Vendor.ID)
case get(id: Vendor.ID)
case update(id: Vendor.ID, updates: Vendor.Update)
static let rootPath = "vendors"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(Vendor.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.get
}
Route(.case(Self.index(withBranches:))) {
Path { rootPath }
Method.get
@@ -78,19 +178,32 @@ public enum ApiRoute: Sendable {
}
}
}
Route(.case(Self.shared)) {
SharedVendorRoute.router
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Vendor.ID.parser() }
Method.put
Body(.json(Vendor.Update.self))
}
}
}
public enum VendorBranchApiRoute: Sendable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
case get(id: VendorBranch.ID)
case index(for: Vendor.ID? = nil)
case shared(SharedVendorBranchRoute)
case update(id: VendorBranch.ID, updates: VendorBranch.Update)
public static let router = OneOf {
Route(.case(Self.create)) {
Path { "vendors"; "branches" }
Method.post
Body(.json(VendorBranch.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.get
@@ -102,9 +215,6 @@ public enum ApiRoute: Sendable {
Field("vendorID", default: nil) { Optionally { VendorBranch.ID.parser() } }
}
}
Route(.case(Self.shared)) {
SharedVendorBranchRoute.router
}
Route(.case(Self.update(id:updates:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.put

View File

@@ -1,165 +0,0 @@
import CasePathsCore
import Foundation
@preconcurrency import URLRouting
public enum SharedEmployeeRoute: Sendable {
case create(Employee.Create)
case delete(id: Employee.ID)
case get(id: Employee.ID)
case index
case update(id: Employee.ID, updates: Employee.Update)
static let rootPath = "employees"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(Employee.Create.self))
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.delete(id:))) {
Path { rootPath; UUID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; UUID.parser() }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; UUID.parser() }
Method.put
Body(.json(Employee.Update.self))
}
}
}
public enum SharedPurchaseOrderRoute: Sendable {
case create(PurchaseOrder.Create)
case delete(id: PurchaseOrder.ID)
case get(id: PurchaseOrder.ID)
case index
case page(page: Int, limit: Int)
static let rootPath = "purchase-orders"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(PurchaseOrder.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Digits() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Digits() }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.page(page:limit:))) {
Path { rootPath; "next" }
Method.get
Query {
Field("page", default: 1) { Digits() }
Field("limit", default: 25) { Digits() }
}
}
}
}
public enum SharedUserRoute: Sendable {
case create(User.Create)
case delete(id: User.ID)
case get(id: User.ID)
case index
case login(User.Login)
case update(id: User.ID, updates: User.Update)
static let rootPath = "users"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(User.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; User.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; User.ID.parser() }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.login)) {
Path { rootPath }
Method.post
Body(.json(User.Login.self))
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; User.ID.parser() }
// TODO: Use put or patch.
Method.post
Body(.json(User.Update.self))
}
}
}
public enum SharedVendorRoute: Sendable {
case create(Vendor.Create)
case delete(id: Vendor.ID)
case get(id: Vendor.ID)
case update(id: Vendor.ID, updates: Vendor.Update)
static let rootPath = "vendors"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body(.json(Vendor.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Vendor.ID.parser() }
Method.put
Body(.json(Vendor.Update.self))
}
}
}
public enum SharedVendorBranchRoute: Sendable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
public static let router = OneOf {
Route(.case(Self.create)) {
Path { "vendors"; "branches" }
Method.post
Body(.json(VendorBranch.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.delete
}
}
}

View File

@@ -5,71 +5,184 @@ import Foundation
public enum ViewRoute: Sendable {
case employee(EmployeeRoute)
case login
case login(LoginRoute)
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.login)) { LoginRoute.router }
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 {
}
public extension ViewRoute {
enum EmployeeRoute: Sendable {
case create(Employee.Create)
case delete(id: Employee.ID)
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) }
case get(id: Employee.ID)
case index
case select(context: SelectContext)
case update(id: Employee.ID, updates: Employee.Update)
static let rootPath = "employees"
public static let router = OneOf {
Route(.case(Self.form)) {
Path { rootPath; "create" }
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body {
FormData {
Field("firstName", .string)
Field("lastName", .string)
Field("active") { Optionally { Bool.parser() } }
}
.map(.memberwise(Employee.Create.init))
}
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.shared)) {
SharedEmployeeRoute.router
Route(.case(Self.delete(id:))) {
Path { rootPath; Employee.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Employee.ID.parser() }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Employee.ID.parser() }
Method.put
Body {
FormData {
Field("firstName") { Optionally { CharacterSet.alphanumerics.map(.string) } }
Field("lastName") { Optionally { CharacterSet.alphanumerics.map(.string) } }
Field("active") { Optionally { Bool.parser() } }
}
.map(.memberwise(Employee.Update.init))
}
}
Route(.case(Self.select(context:))) {
Path { rootPath; "select" }
Method.get
Query {
Field("context") { SelectContext.parser() }
}
}
}
}
}
public enum PurchaseOrderRoute: Sendable {
public extension ViewRoute {
enum LoginRoute: Sendable {
case index(next: String? = nil)
case post(Request)
static let rootPath = "login"
public static let router = OneOf {
Route(.case(Self.index)) {
Method.get
Path { rootPath }
Query {
Field("next", default: nil) {
Optionally { CharacterSet.urlPathAllowed.map(.string) }
}
}
}
Route(.case(Self.post)) {
Path { rootPath }
Method.post
Body {
FormData {
Field("username", .string)
Field("password", .string)
Field("next", default: nil) {
Optionally { CharacterSet.urlPathAllowed.map(.string) }
}
}
.map(.memberwise(Request.init))
}
}
}
public struct Request: Codable, Equatable, Sendable {
public let username: String
public let password: String
public let next: String?
}
}
}
public extension ViewRoute {
enum PurchaseOrderRoute: Sendable {
case create(PurchaseOrder.Create)
case delete(id: PurchaseOrder.ID)
case form
case get(id: PurchaseOrder.ID)
case index
case page(page: Int, limit: Int)
case search(Search)
case shared(SharedPurchaseOrderRoute)
static let rootPath = "purchase-orders"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body {
FormData {
Field("id") { Optionally { PurchaseOrder.ID.parser() } }
Field("workOrder") { Optionally { Int.parser() } }
Field("materials", .string)
Field("customer", .string)
Field("truckStock") { Optionally { Bool.parser() } }
Field("createdByID") { User.ID.parser() }
Field("createdForID") { Employee.ID.parser() }
Field("vendorBranchID") { VendorBranch.ID.parser() }
}
.map(.memberwise(PurchaseOrder.Create.init))
}
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Digits() }
Method.delete
}
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.get(id:))) {
Path { rootPath; Digits() }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.page(page:limit:))) {
Path { rootPath; "next" }
Method.get
Query {
Field("page", default: 1) { Digits() }
Field("limit", default: 25) { Digits() }
}
}
Route(.case(Self.search)) {
Search.router
}
Route(.case(Self.shared)) {
SharedPurchaseOrderRoute.router
}
}
public enum Search: Sendable {
@@ -121,98 +234,156 @@ public enum ViewRoute: Sendable {
}
}
}
// TODO: Move into respective view routes.
public enum SelectRoute: Sendable {
case employee(context: Context)
case vendorBranches(context: Context)
public extension ViewRoute {
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() }
}
}
}
enum SelectContext: String, Codable, Sendable, CaseIterable {
case purchaseOrderForm
case purchaseOrderSearch
}
public enum UserRoute: Sendable {
}
public extension ViewRoute {
enum UserRoute: Sendable {
case create(User.Create)
case delete(id: User.ID)
case form
case shared(SharedUserRoute)
case get(id: User.ID)
case index
case update(id: User.ID, updates: User.Update)
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.create)) {
Path { rootPath }
Method.post
Body(.json(User.Create.self))
}
Route(.case(Self.delete(id:))) {
Path { rootPath; User.ID.parser() }
Method.delete
}
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" }
Route(.case(Self.get(id:))) {
Path { rootPath; User.ID.parser() }
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" }
Route(.case(Self.index)) {
Path { rootPath }
Method.get
Query {
Field("vendorID") { Vendor.ID.parser() }
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; User.ID.parser() }
Method.patch
Body {
FormData {
Field("username") {
Optionally { CharacterSet.alphanumerics.map(.string) }
}
Field("email") {
Optionally {
// TODO: Not sure if this is correct.
Rest().map(.string)
}
}
}
.map(.memberwise(User.Update.init))
}
}
}
}
}
public extension ViewRoute {
enum VendorRoute: Sendable {
case create(Vendor.Create)
case delete(id: Vendor.ID)
case form
case get(id: Vendor.ID)
case index
case update(id: Vendor.ID, updates: Vendor.Update)
static let rootPath = "vendors"
public static let router = OneOf {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
Body {
FormData {
Field("name", .string)
}
.map(.memberwise(Vendor.Create.init))
}
}
Route(.case(Self.delete(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.delete
}
Route(.case(Self.get(id:))) {
Path { rootPath; Vendor.ID.parser() }
Method.get
}
Route(.case(Self.form)) {
Path { rootPath; "create" }
Method.get
}
Route(.case(Self.index)) {
Path { rootPath }
Method.get
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Vendor.ID.parser() }
Method.put
Body {
FormData {
Field("name", .string)
}
.map(.memberwise(Vendor.Update.init))
}
}
}
}
}
public extension ViewRoute {
enum VendorBranchRoute: Sendable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
case select(context: ViewRoute.SelectContext)
public static let router = OneOf {
Route(.case(Self.create)) {
Path { "vendors"; "branches" }
Method.post
Body {
FormData {
Field("name", .string)
Field("vendorID") { Vendor.ID.parser() }
}
.map(.memberwise(VendorBranch.Create.init))
}
}
Route(.case(Self.delete(id:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.delete
}
Route(.case(Self.select(context:))) {
Path { "vendors"; "branches"; "select" }
Method.get
Query {
Field("context") { SelectContext.parser() }
}
}
Route(.case(Self.shared)) {
SharedVendorBranchRoute.router
}
}
}
}

View File

@@ -34,9 +34,9 @@ public extension Vendor {
}
struct Update: Codable, Sendable {
public let name: String?
public let name: String
public init(name: String?) {
public init(name: String) {
self.name = name
}
}