feat: Working on route and id helpers for views.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import Dependencies
|
||||
import Elementary
|
||||
import Fluent
|
||||
import SharedModels
|
||||
import Vapor
|
||||
import VaporElementary
|
||||
@@ -14,12 +15,10 @@ struct PurchaseOrderViewController: RouteCollection {
|
||||
route.get(use: index)
|
||||
route.get("next", use: nextPage)
|
||||
route.post(use: create(req:))
|
||||
route.post("search", use: postSearch)
|
||||
route.get("search", use: getSearch)
|
||||
// route.post("search", use: postSearch)
|
||||
// route.get("search", use: getSearch)
|
||||
route.group("create") {
|
||||
$0.get(use: form)
|
||||
$0.get("vendor-branch-select", use: vendorBranchSelect(req:))
|
||||
$0.get("employee-select", use: employeeSelect(req:))
|
||||
}
|
||||
route.group(":id") {
|
||||
$0.get(use: get)
|
||||
@@ -49,18 +48,6 @@ struct PurchaseOrderViewController: RouteCollection {
|
||||
return await req.render { PurchaseOrderForm(shouldShow: true) }
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func vendorBranchSelect(req: Request) async throws -> HTMLResponse {
|
||||
let branches = try await vendorBranches.fetchAllWithDetail()
|
||||
return await req.render { PurchaseOrderForm.VendorSelect(vendorBranches: branches) }
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func employeeSelect(req: Request) async throws -> HTMLResponse {
|
||||
let employees = try await self.employees.fetchAll()
|
||||
return await req.render { PurchaseOrderForm.EmployeeSelect(employees: employees) }
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func get(req: Request) async throws -> HTMLResponse {
|
||||
let purchaseOrder = try await purchaseOrders.get(req.ensureIDPathComponent(as: Int.self))
|
||||
@@ -83,18 +70,23 @@ struct PurchaseOrderViewController: RouteCollection {
|
||||
|
||||
@Sendable
|
||||
func postSearch(req: Request) async throws -> HTMLResponse {
|
||||
let query = try req.content.decode([String: String].self)
|
||||
req.logger.info("query: \(query)")
|
||||
let context = try req.content.decode(SearchContext.self).toSearch()
|
||||
let purchaseOrders = try await purchaseOrders.search(context)
|
||||
req.logger.info("\(purchaseOrders)")
|
||||
return await req.render { PurchaseOrderTable.Rows(page: purchaseOrders) }
|
||||
let context = try req.content.decode(PurchaseOrderSearchContent.self)
|
||||
let results = try await purchaseOrders.search(context.toDatabaseQuery(), .init(page: 1, per: 25))
|
||||
return await req.render { PurchaseOrderTable(page: results, context: .search, searchContext: nil) }
|
||||
}
|
||||
|
||||
// Show the form to generate a search query.
|
||||
@Sendable
|
||||
func getSearch(req: Request) async throws -> HTMLResponse {
|
||||
let context = try req.query.decode(SearchQuery.self).toSearchContext()
|
||||
return await req.render { PurchaseOrderSearch(context: context) }
|
||||
// TODO: Need to handle updating the form.
|
||||
return await req.render {
|
||||
MainPage(displayNav: true, route: .purchaseOrders) {
|
||||
div(.class("container"), .id("purchase-order-content")) {
|
||||
PurchaseOrderSearch()
|
||||
PurchaseOrderTable(page: .init(items: [], metadata: .init(page: 0, per: 50, total: 0)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func mainPage<C: HTML>(
|
||||
@@ -103,9 +95,8 @@ struct PurchaseOrderViewController: RouteCollection {
|
||||
) async throws -> some SendableHTMLDocument where C: Sendable {
|
||||
let page = try await purchaseOrders.fetchPage(.init(page: page.page, per: page.limit))
|
||||
return MainPage(displayNav: true, route: .purchaseOrders) {
|
||||
div(.class("container")) {
|
||||
div(.class("container"), .id("purchase-order-content")) {
|
||||
html
|
||||
PurchaseOrderSearch()
|
||||
PurchaseOrderTable(page: page)
|
||||
}
|
||||
}
|
||||
@@ -142,32 +133,3 @@ private struct CreateContext: Content {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private struct SearchContext: Content {
|
||||
let context: String
|
||||
let search: String
|
||||
|
||||
func toSearch() throws -> PurchaseOrder.SearchContext {
|
||||
switch context {
|
||||
case "employee":
|
||||
return .employee(search)
|
||||
case "customer":
|
||||
return .customer(search)
|
||||
case "vendor":
|
||||
return .vendor(search)
|
||||
default:
|
||||
throw Abort(.badRequest, reason: "Invalid search context.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SearchQuery: Content {
|
||||
let context: String
|
||||
|
||||
func toSearchContext() throws -> PurchaseOrderSearchContext {
|
||||
guard let context = PurchaseOrderSearchContext(rawValue: context) else {
|
||||
throw Abort(.badRequest, reason: "Invalid context.")
|
||||
}
|
||||
return context
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user