feat: Working purchase order table and form.
This commit is contained in:
@@ -10,12 +10,12 @@ struct EmployeeViewController: RouteCollection {
|
||||
let employees = routes.protected.grouped("employees")
|
||||
employees.get(use: index(req:))
|
||||
employees.get("form", use: employeeForm(req:))
|
||||
employees.post(use: postEmployeeForm(req:))
|
||||
employees.post(use: create(req:))
|
||||
employees.group(":employeeID") {
|
||||
$0.get(use: editEmployee(req:))
|
||||
$0.delete(use: deleteEmployee(req:))
|
||||
$0.put(use: updateEmployee(req:))
|
||||
$0.post("toggle-active", use: toggleActiveEmployee(req:))
|
||||
$0.get(use: edit(req:))
|
||||
$0.delete(use: delete(req:))
|
||||
$0.put(use: update(req:))
|
||||
$0.post("toggle-active", use: toggleActive(req:))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,14 +25,13 @@ struct EmployeeViewController: RouteCollection {
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func postEmployeeForm(req: Request) async throws -> View {
|
||||
func create(req: Request) async throws -> View {
|
||||
_ = try await api.createEmployee(req: req)
|
||||
let employees = try await api.getSortedEmployees(req: req)
|
||||
return try await req.view.render("employees/table", ["employees": employees])
|
||||
return try await req.view.render("employees/index", EmployeesCTX(oob: true, api: api, req: req))
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func toggleActiveEmployee(req: Request) async throws -> View {
|
||||
func toggleActive(req: Request) async throws -> View {
|
||||
guard let employee = try await Employee.find(req.parameters.get("employeeID"), on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
@@ -43,14 +42,14 @@ struct EmployeeViewController: RouteCollection {
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func deleteEmployee(req: Request) async throws -> View {
|
||||
func delete(req: Request) async throws -> View {
|
||||
_ = try await api.deleteEmployee(req: req)
|
||||
let employees = try await api.getSortedEmployees(req: req)
|
||||
return try await req.view.render("employees/table", ["employees": employees])
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func editEmployee(req: Request) async throws -> View {
|
||||
func edit(req: Request) async throws -> View {
|
||||
guard let employee = try await Employee.find(req.parameters.get("employeeID"), on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
@@ -58,7 +57,7 @@ struct EmployeeViewController: RouteCollection {
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func updateEmployee(req: Request) async throws -> View {
|
||||
func update(req: Request) async throws -> View {
|
||||
_ = try await api.updateEmployee(req: req)
|
||||
return try await req.view.render("employees/index", EmployeesCTX(oob: true, api: api, req: req))
|
||||
}
|
||||
@@ -88,12 +87,25 @@ private struct EmployeesCTX: Content {
|
||||
}
|
||||
|
||||
private struct EmployeeFormCTX: Content {
|
||||
let employee: Employee.DTO?
|
||||
let oob: Bool
|
||||
|
||||
let htmxForm: HtmxFormCTX<Context>
|
||||
|
||||
init(employee: Employee.DTO? = nil) {
|
||||
self.employee = employee
|
||||
self.oob = employee != nil
|
||||
self.htmxForm = .init(
|
||||
formClass: "employee-form",
|
||||
formId: "employee-form",
|
||||
htmxTargetUrl: employee?.id == nil ? .post("/employees") : .put("/employees/\(employee!.id!)"),
|
||||
htmxTarget: "#employee-table",
|
||||
htmxPushUrl: false,
|
||||
htmxResetAfterRequest: true,
|
||||
htmxSwapOob: nil,
|
||||
htmxSwap: employee == nil ? .outerHTML : nil,
|
||||
context: .init(employee: employee)
|
||||
)
|
||||
}
|
||||
|
||||
struct Context: Content {
|
||||
let employee: Employee.DTO?
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user