feat: Refactoring routes to use shared / base routes.

This commit is contained in:
2025-01-21 16:00:16 -05:00
parent 20e58114c0
commit 66074d66f4
15 changed files with 445 additions and 227 deletions

View File

@@ -4,222 +4,34 @@ import Foundation
public enum ApiRoute: Sendable, Equatable {
case employee(EmployeeApiRoute)
case purchaseOrder(PurchaseOrderApiRoute)
case user(UserApiRoute)
case vendor(VendorApiRoute)
case vendorBranch(VendorBranchApiRoute)
case employee(BaseRoute.EmployeeRoute)
case purchaseOrder(BaseRoute.PurchaseOrderRoute)
case user(BaseRoute.UserRoute)
case vendor(BaseRoute.VendorRoute)
case vendorBranch(BaseRoute.VendorBranchRoute)
static let rootPath = Path { "api"; "v1" }
public static let router = OneOf {
Route(.case(Self.employee)) {
rootPath
EmployeeApiRoute.router
BaseRoute.EmployeeRoute.router
}
Route(.case(Self.purchaseOrder)) {
rootPath
PurchaseOrderApiRoute.router
BaseRoute.PurchaseOrderRoute.router
}
Route(.case(Self.user)) {
rootPath
UserApiRoute.router
BaseRoute.UserRoute.router
}
Route(.case(Self.vendor)) {
rootPath
VendorApiRoute.router
BaseRoute.VendorRoute.router
}
Route(.case(Self.vendorBranch)) {
rootPath
VendorBranchApiRoute.router
}
}
public enum EmployeeApiRoute: Sendable, Equatable {
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 PurchaseOrderApiRoute: Sendable, Equatable {
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() }
}
}
}
}
// TODO: Add login / logout.
public enum UserApiRoute: Sendable, Equatable {
case create(User.Create)
case delete(id: User.ID)
case get(id: User.ID)
case index
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.update(id:updates:))) {
Path { rootPath; User.ID.parser() }
Method.patch
Body(.json(User.Update.self))
}
}
}
public enum VendorApiRoute: Sendable, Equatable {
case index(withBranches: Bool? = nil)
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
Query {
Optionally {
Field("branches", default: nil) {
Bool.parser()
}
}
}
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Vendor.ID.parser() }
Method.put
Body(.json(Vendor.Update.self))
}
}
}
public enum VendorBranchApiRoute: Sendable, Equatable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
case get(id: VendorBranch.ID)
case index(for: Vendor.ID? = nil)
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
}
Route(.case(Self.index(for:))) {
Path { "vendors"; "branches" }
Method.get
Query {
Optionally { Field("vendorID", default: nil) { VendorBranch.ID.parser() } }
}
}
Route(.case(Self.update(id:updates:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.put
Body(.json(VendorBranch.Update.self))
}
BaseRoute.VendorBranchRoute.router
}
}
}

View File

@@ -0,0 +1,294 @@
import CasePathsCore
import Foundation
@preconcurrency import URLRouting
public enum BaseRoute {}
public extension BaseRoute {
enum EmployeeRoute: Sendable, Equatable {
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
OneOf {
Body(.json(Employee.Create.self))
Body {
FormData {
Field("firstName", .string)
Field("lastName", .string)
Optionally { Field("active") { Bool.parser() } }
}
.map(.memberwise(Employee.Create.init))
}
}
}
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
OneOf {
Body(.json(Employee.Update.self))
Body {
FormData {
Optionally { Field("firstName") { CharacterSet.alphanumerics.map(.string) } }
Optionally { Field("lastName") { CharacterSet.alphanumerics.map(.string) } }
Optionally { Field("active") { Bool.parser() } }
}
.map(.memberwise(Employee.Update.init))
}
}
}
}
}
}
public extension BaseRoute {
enum PurchaseOrderRoute: Sendable, Equatable {
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
OneOf {
Body(.json(PurchaseOrder.Create.self))
Body {
FormData {
Optionally { Field("id") { PurchaseOrder.ID.parser() } }
Optionally { Field("workOrder") { Int.parser() } }
Field("materials", .string)
Field("customer", .string)
Optionally { Field("truckStock") { 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.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 extension BaseRoute {
enum UserRoute: Sendable, Equatable {
case create(User.Create)
case delete(id: User.ID)
case get(id: User.ID)
case index
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
OneOf {
Body(.json(User.Create.self))
Body {
FormData {
Field("username", .string)
Field("email", .string)
Field("password", .string)
Field("confirmPassword", .string)
}
.map(.memberwise(User.Create.init))
}
}
}
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
OneOf {
Body(.json(User.Update.self))
Body {
FormData {
Optionally { Field("username") {
CharacterSet.alphanumerics.map(.string)
}
}
Optionally { Field("email", .string) }
}
.map(.memberwise(User.Update.init))
}
}
}
}
}
}
public extension BaseRoute {
enum VendorRoute: Sendable, Equatable {
case index(withBranches: Bool? = nil)
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
OneOf {
Body(.json(Vendor.Create.self))
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.index(withBranches:))) {
Path { rootPath }
Method.get
Query {
Optionally {
Field("branches", default: nil) {
Bool.parser()
}
}
}
}
Route(.case(Self.update(id:updates:))) {
Path { rootPath; Vendor.ID.parser() }
Method.put
OneOf {
Body(.json(Vendor.Update.self))
Body {
FormData {
Field("name", .string)
}
.map(.memberwise(Vendor.Update.init))
}
}
}
}
}
}
public extension BaseRoute {
enum VendorBranchRoute: Sendable, Equatable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
case get(id: VendorBranch.ID)
case index(for: Vendor.ID? = nil)
case update(id: VendorBranch.ID, updates: VendorBranch.Update)
public static let router = OneOf {
Route(.case(Self.create)) {
Path { "vendors"; "branches" }
Method.post
OneOf {
Body(.json(VendorBranch.Create.self))
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.get(id:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.get
}
Route(.case(Self.index(for:))) {
Path { "vendors"; "branches" }
Method.get
Query {
Optionally { Field("vendorID", default: nil) { VendorBranch.ID.parser() } }
}
}
Route(.case(Self.update(id:updates:))) {
Path { "vendors"; "branches"; VendorBranch.ID.parser() }
Method.put
OneOf {
Body(.json(VendorBranch.Update.self))
Body {
FormData {
Field("name", .string)
}
.map(.memberwise(VendorBranch.Update.init))
}
}
}
}
}
}