feat: Adds view route parsing tests.

This commit is contained in:
2025-01-20 10:51:07 -05:00
parent 2de85ed758
commit affd9b5d81
14 changed files with 552 additions and 59 deletions

View File

@@ -31,7 +31,7 @@ public struct Employee: Codable, Equatable, Identifiable, Sendable {
}
public extension Employee {
struct Create: Codable, Sendable {
struct Create: Codable, Sendable, Equatable {
public let firstName: String
public let lastName: String
public let active: Bool?
@@ -47,7 +47,7 @@ public extension Employee {
}
}
struct Update: Codable, Sendable {
struct Update: Codable, Sendable, Equatable {
public let firstName: String?
public let lastName: String?
public let active: Bool?

View File

@@ -41,7 +41,7 @@ public struct PurchaseOrder: Codable, Equatable, Identifiable, Sendable {
public extension PurchaseOrder {
struct Create: Codable, Sendable {
struct Create: Codable, Sendable, Equatable {
public let id: Int?
public let workOrder: Int?
@@ -73,7 +73,7 @@ public extension PurchaseOrder {
}
}
struct CreateIntermediate: Codable, Sendable {
struct CreateIntermediate: Codable, Sendable, Equatable {
public let id: Int?
public let workOrder: Int?
@@ -115,7 +115,7 @@ public extension PurchaseOrder {
}
}
enum SearchContext: Sendable {
enum SearchContext: Sendable, Equatable {
case customer(String)
case vendor(VendorBranch.ID)
case employee(Employee.ID)

View File

@@ -2,7 +2,8 @@ import CasePathsCore
import Foundation
@preconcurrency import URLRouting
public enum ViewRoute: Sendable {
// swiftlint:disable file_length
public enum ViewRoute: Sendable, Equatable {
case employee(EmployeeRoute)
case login(LoginRoute)
@@ -24,7 +25,7 @@ public enum ViewRoute: Sendable {
public extension ViewRoute {
enum EmployeeRoute: Sendable {
enum EmployeeRoute: Sendable, Equatable {
case create(Employee.Create)
case delete(id: Employee.ID)
case form
@@ -43,15 +44,11 @@ public extension ViewRoute {
FormData {
Field("firstName", .string)
Field("lastName", .string)
Field("active") { Optionally { Bool.parser() } }
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; Employee.ID.parser() }
Method.delete
@@ -60,14 +57,22 @@ public extension ViewRoute {
Path { rootPath; Employee.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; 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() } }
Optionally { Field("firstName") { CharacterSet.alphanumerics.map(.string) } }
Optionally { Field("lastName") { CharacterSet.alphanumerics.map(.string) } }
Optionally { Field("active") { Bool.parser() } }
}
.map(.memberwise(Employee.Update.init))
}
@@ -85,7 +90,7 @@ public extension ViewRoute {
public extension ViewRoute {
enum LoginRoute: Sendable {
enum LoginRoute: Sendable, Equatable {
case index(next: String? = nil)
case post(Request)
@@ -96,8 +101,10 @@ public extension ViewRoute {
Method.get
Path { rootPath }
Query {
Field("next", default: nil) {
Optionally { CharacterSet.urlPathAllowed.map(.string) }
Optionally {
Field("next", default: nil) {
CharacterSet.urlPathAllowed.map(.string)
}
}
}
}
@@ -108,8 +115,10 @@ public extension ViewRoute {
FormData {
Field("username", .string)
Field("password", .string)
Field("next", default: nil) {
Optionally { CharacterSet.urlPathAllowed.map(.string) }
Optionally {
Field("next", default: nil) {
CharacterSet.urlPathAllowed.map(.string)
}
}
}
.map(.memberwise(Request.init))
@@ -121,12 +130,18 @@ public extension ViewRoute {
public let username: String
public let password: String
public let next: String?
public init(username: String, password: String, next: String? = nil) {
self.username = username
self.password = password
self.next = next
}
}
}
}
public extension ViewRoute {
enum PurchaseOrderRoute: Sendable {
enum PurchaseOrderRoute: Sendable, Equatable {
case create(PurchaseOrder.Create)
case delete(id: PurchaseOrder.ID)
case form
@@ -143,11 +158,11 @@ public extension ViewRoute {
Method.post
Body {
FormData {
Field("id") { Optionally { PurchaseOrder.ID.parser() } }
Field("workOrder") { Optionally { Int.parser() } }
Optionally { Field("id") { PurchaseOrder.ID.parser() } }
Optionally { Field("workOrder") { Int.parser() } }
Field("materials", .string)
Field("customer", .string)
Field("truckStock") { Optionally { Bool.parser() } }
Optionally { Field("truckStock") { Bool.parser() } }
Field("createdByID") { User.ID.parser() }
Field("createdForID") { Employee.ID.parser() }
Field("vendorBranchID") { VendorBranch.ID.parser() }
@@ -185,9 +200,9 @@ public extension ViewRoute {
}
}
public enum Search: Sendable {
public enum Search: Sendable, Equatable {
case index(context: Context? = nil, table: Bool? = nil)
case search(Request)
case request(Request)
static let rootPath = Path { "purchase-orders"; "search" }
@@ -196,24 +211,33 @@ public extension ViewRoute {
rootPath
Method.get
Query {
Field("context", default: .employee) { Optionally { Search.Context.parser() } }
Field("table", default: nil) { Optionally { Bool.parser() } }
Optionally { Field("context", default: .employee) { Search.Context.parser() } }
Optionally { Field("table", default: nil) { Bool.parser() } }
}
}
Route(.case(Search.search)) {
Route(.case(Search.request)) {
rootPath
Method.post
Body(.json(Search.Request.self))
Body {
FormData {
Field("context") { Context.parser() }
Optionally { Field("createdForID") { Employee.ID.parser() } }
Optionally { Field("customerSearch", .string) }
Optionally { Field("vendorBranchID") { VendorBranch.ID.parser() } }
}
.map(.memberwise(Request.init))
}
}
}
public enum Context: String, Codable, CaseIterable, Sendable {
public enum Context: String, Codable, CaseIterable, Equatable, Sendable {
case employee
case customer
case vendor
}
public struct Request: Codable, Sendable {
// TODO: Create a validation or potentially turn this into an enum with the valid states.
public struct Request: Codable, Equatable, Sendable {
public let context: Context
public let createdForID: Employee.ID?
public let customerSearch: String?
@@ -231,14 +255,13 @@ public extension ViewRoute {
self.vendorBranchID = vendorBranchID
}
}
}
}
}
public extension ViewRoute {
enum SelectContext: String, Codable, Sendable, CaseIterable {
enum SelectContext: String, Codable, Equatable, Sendable, CaseIterable {
case purchaseOrderForm
case purchaseOrderSearch
}
@@ -246,7 +269,7 @@ public extension ViewRoute {
}
public extension ViewRoute {
enum UserRoute: Sendable {
enum UserRoute: Sendable, Equatable {
case create(User.Create)
case delete(id: User.ID)
case form
@@ -260,7 +283,15 @@ public extension ViewRoute {
Route(.case(Self.create)) {
Path { rootPath }
Method.post
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() }
@@ -283,15 +314,11 @@ public extension ViewRoute {
Method.patch
Body {
FormData {
Field("username") {
Optionally { CharacterSet.alphanumerics.map(.string) }
Optionally { Field("username") {
CharacterSet.alphanumerics.map(.string)
}
Field("email") {
Optionally {
// TODO: Not sure if this is correct.
Rest().map(.string)
}
}
Optionally { Field("email", .string) }
}
.map(.memberwise(User.Update.init))
}
@@ -302,7 +329,7 @@ public extension ViewRoute {
public extension ViewRoute {
enum VendorRoute: Sendable {
enum VendorRoute: Sendable, Equatable {
case create(Vendor.Create)
case delete(id: Vendor.ID)
case form
@@ -356,7 +383,7 @@ public extension ViewRoute {
public extension ViewRoute {
enum VendorBranchRoute: Sendable {
enum VendorBranchRoute: Sendable, Equatable {
case create(VendorBranch.Create)
case delete(id: VendorBranch.ID)
case select(context: ViewRoute.SelectContext)
@@ -387,3 +414,5 @@ public extension ViewRoute {
}
}
}
// swiftlint:enable file_length

View File

@@ -26,7 +26,7 @@ public struct User: Codable, Equatable, Identifiable, Sendable {
public extension User {
struct Create: Codable, Sendable {
struct Create: Codable, Sendable, Equatable {
public let username: String
public let email: String
public let password: String
@@ -45,7 +45,7 @@ public extension User {
}
}
struct Login: Codable, Sendable {
struct Login: Codable, Sendable, Equatable {
public let username: String?
public let email: String?
public let password: String
@@ -80,6 +80,11 @@ public extension User {
struct Update: Codable, Equatable, Sendable {
public let username: String?
public let email: String?
public init(username: String?, email: String?) {
self.username = username
self.email = email
}
}
}

View File

@@ -25,7 +25,7 @@ public struct Vendor: Codable, Equatable, Identifiable, Sendable {
public extension Vendor {
struct Create: Codable, Sendable {
struct Create: Codable, Sendable, Equatable {
public let name: String
public init(name: String) {
@@ -33,7 +33,7 @@ public extension Vendor {
}
}
struct Update: Codable, Sendable {
struct Update: Codable, Sendable, Equatable {
public let name: String
public init(name: String) {

View File

@@ -24,7 +24,7 @@ public struct VendorBranch: Codable, Equatable, Identifiable, Sendable {
}
public extension VendorBranch {
struct Create: Codable, Sendable {
struct Create: Codable, Sendable, Equatable {
public let name: String
public let vendorID: Vendor.ID
@@ -56,7 +56,7 @@ public extension VendorBranch {
}
}
struct Update: Codable, Sendable {
struct Update: Codable, Sendable, Equatable {
public let name: String?
public init(name: String?) {