feat: Refactoring route declarations.

This commit is contained in:
2025-01-19 10:52:15 -05:00
parent d27a19863a
commit 1c8748211c
20 changed files with 954 additions and 644 deletions

View File

@@ -0,0 +1,165 @@
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
}
}
}