feat: Adds employee views.
This commit is contained in:
@@ -1,158 +1,65 @@
|
|||||||
// import Dependencies
|
import DatabaseClient
|
||||||
// import Fluent
|
import Dependencies
|
||||||
// import Leaf
|
import Elementary
|
||||||
// import Vapor
|
import SharedModels
|
||||||
//
|
import Vapor
|
||||||
// struct EmployeeViewController: RouteCollection {
|
import VaporElementary
|
||||||
//
|
|
||||||
// @Dependency(\.employees) var employees
|
struct EmployeeViewController: RouteCollection {
|
||||||
//
|
|
||||||
// func boot(routes: any RoutesBuilder) throws {
|
@Dependency(\.database.employees) var employees
|
||||||
// let protected = routes.protected.grouped("employees")
|
|
||||||
// protected.get(use: index(req:))
|
func boot(routes: any RoutesBuilder) throws {
|
||||||
// protected.get("form", use: employeeForm(req:))
|
let route = routes.protected.grouped("employees")
|
||||||
// protected.post(use: create(req:))
|
route.get(use: index)
|
||||||
// protected.group(":employeeID") {
|
route.get("create", use: form)
|
||||||
// $0.get(use: get(req:))
|
route.post(use: create)
|
||||||
// $0.get("edit", use: edit(req:))
|
route.group(":id") {
|
||||||
// $0.delete(use: delete(req:))
|
$0.get(use: get)
|
||||||
// $0.put(use: update(req:))
|
$0.put(use: update)
|
||||||
// $0.patch("toggle-active", use: toggleActive(req:))
|
}
|
||||||
// }
|
}
|
||||||
// }
|
|
||||||
//
|
@Sendable
|
||||||
// @Sendable
|
func index(req: Request) async throws -> HTMLResponse {
|
||||||
// func index(req: Request) async throws -> View {
|
try await req.render { try await mainPage(EmployeeForm()) }
|
||||||
// return try await renderIndex(req)
|
}
|
||||||
// }
|
|
||||||
//
|
@Sendable
|
||||||
// @Sendable
|
func form(req: Request) async throws -> HTMLResponse {
|
||||||
// private func renderIndex(
|
await req.render { EmployeeForm(shouldShow: true) }
|
||||||
// _ req: Request,
|
}
|
||||||
// _ employee: Employee.DTO? = nil,
|
|
||||||
// _ form: EmployeeFormCTX? = nil
|
@Sendable
|
||||||
// ) async throws -> View {
|
func create(req: Request) async throws -> HTMLResponse {
|
||||||
// return try await req.view.render(
|
let employee = try await employees.create(req.content.decode(Employee.Create.self))
|
||||||
// "employees/index",
|
return await req.render { EmployeeTable.Row(employee: employee) }
|
||||||
// EmployeesCTX(employee: employee, employees: employees.fetchAll(), form: form ?? .init())
|
}
|
||||||
// )
|
|
||||||
// }
|
@Sendable
|
||||||
//
|
func get(req: Request) async throws -> HTMLResponse {
|
||||||
// @Sendable
|
guard let employee = try await employees.get(req.ensureIDPathComponent()) else {
|
||||||
// func create(req: Request) async throws -> View {
|
throw Abort(.badRequest, reason: "Employee not found.")
|
||||||
// try Employee.Create.validate(content: req)
|
}
|
||||||
// let employee = try await employees.create(req.content.decode(Employee.Create.self))
|
guard req.isHtmxRequest else {
|
||||||
// return try await req.view.render("employees/table-row", employee)
|
return try await req.render { try await mainPage(EmployeeForm(employee: employee)) }
|
||||||
// }
|
}
|
||||||
//
|
return await req.render { EmployeeForm(employee: employee) }
|
||||||
// @Sendable
|
}
|
||||||
// func get(req: Request) async throws -> View {
|
|
||||||
// let employee = try await employees.get(req.ensureIDPathComponent(key: "employeeID"))
|
@Sendable
|
||||||
// // Check if we've rendered the page yet.
|
func update(req: Request) async throws -> HTMLResponse {
|
||||||
// guard req.isHtmxRequest else {
|
let employee = try await employees.update(req.ensureIDPathComponent(), req.content.decode(Employee.Update.self))
|
||||||
// return try await renderIndex(req, employee)
|
return await req.render { EmployeeTable.Row(employee: employee) }
|
||||||
// }
|
}
|
||||||
// return try await req.view.render("employees/detail", ["employee": employee])
|
|
||||||
// }
|
private func mainPage<C: HTML>(_ html: C) async throws -> some SendableHTMLDocument where C: Sendable {
|
||||||
//
|
let employees = try await self.employees.fetchAll()
|
||||||
// @Sendable
|
return MainPage(displayNav: true, route: .employees) {
|
||||||
// func toggleActive(req: Request) async throws -> View {
|
div(.class("container")) {
|
||||||
// guard let id = req.parameters.get("employeeID", as: Employee.IDValue.self) else {
|
html
|
||||||
// throw Abort(.badRequest, reason: "Employee id not supplied.")
|
EmployeeTable(employees: employees)
|
||||||
// }
|
}
|
||||||
// let employee = try await employees.toggleActive(id)
|
}
|
||||||
// return try await req.view.render("employees/table-row", employee)
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // TODO: I think we can just return a response and remove the table-row, here.
|
|
||||||
// @Sendable
|
|
||||||
// func delete(req: Request) async throws -> View {
|
|
||||||
// let id = try req.requireEmployeeID()
|
|
||||||
// _ = try await employees.delete(id)
|
|
||||||
// let employees = try await employees.fetchAll()
|
|
||||||
// return try await req.view.render("employees/table", ["employees": employees])
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Sendable
|
|
||||||
// func edit(req: Request) async throws -> View {
|
|
||||||
// guard let employee = try await employees.get(req.parameters.get("employeeID")) else {
|
|
||||||
// throw Abort(.notFound)
|
|
||||||
// }
|
|
||||||
// return try await req.view.render("employees/detail", EmployeeDetailCTX(editing: true, employee: employee))
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Sendable
|
|
||||||
// func update(req: Request) async throws -> View {
|
|
||||||
// let id = try req.requireEmployeeID()
|
|
||||||
// try Employee.Update.validate(content: req)
|
|
||||||
// let updates = try req.content.decode(Employee.Update.self)
|
|
||||||
// req.logger.info("Employee updates: \(updates)")
|
|
||||||
// let employee = try await employees.update(id, updates)
|
|
||||||
// req.logger.info("Done updating employee: \(employee)")
|
|
||||||
// return try await req.view.render("employees/table-row", employee)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Sendable
|
|
||||||
// func employeeForm(req: Request) async throws -> View {
|
|
||||||
// try await req.view.render("employees/form", EmployeeFormCTX())
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private extension Request {
|
|
||||||
// func requireEmployeeID() throws -> Employee.IDValue {
|
|
||||||
// guard let id = parameters.get("employeeID", as: Employee.IDValue.self) else {
|
|
||||||
// throw Abort(.badRequest, reason: "Employee id not supplied")
|
|
||||||
// }
|
|
||||||
// return id
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private struct EmployeeDetailCTX: Content {
|
|
||||||
// let editing: Bool
|
|
||||||
// let employee: Employee.DTO?
|
|
||||||
//
|
|
||||||
// init(editing: Bool = false, employee: Employee.DTO? = nil) {
|
|
||||||
// self.editing = editing
|
|
||||||
// self.employee = employee
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private struct EmployeesCTX: Content {
|
|
||||||
// let employee: Employee.DTO?
|
|
||||||
// let employees: [Employee.DTO]
|
|
||||||
// let form: EmployeeFormCTX
|
|
||||||
//
|
|
||||||
// init(
|
|
||||||
// employee: Employee.DTO? = nil,
|
|
||||||
// employees: [Employee.DTO],
|
|
||||||
// form: EmployeeFormCTX? = nil
|
|
||||||
// ) {
|
|
||||||
// self.employee = employee
|
|
||||||
// self.employees = employees
|
|
||||||
// self.form = form ?? .init()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// private struct EmployeeFormCTX: Content {
|
|
||||||
//
|
|
||||||
// let htmxForm: HtmxFormCTX<Context>
|
|
||||||
//
|
|
||||||
// init(employee: Employee.DTO? = 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?
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
63
Sources/App/Views/Employees/EmployeeForm.swift
Normal file
63
Sources/App/Views/Employees/EmployeeForm.swift
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import Elementary
|
||||||
|
import ElementaryHTMX
|
||||||
|
import SharedModels
|
||||||
|
|
||||||
|
struct EmployeeForm: HTML {
|
||||||
|
let employee: Employee?
|
||||||
|
let shouldShow: Bool
|
||||||
|
|
||||||
|
init(employee: Employee? = nil, shouldShow: Bool = false) {
|
||||||
|
self.employee = employee
|
||||||
|
self.shouldShow = shouldShow
|
||||||
|
}
|
||||||
|
|
||||||
|
init(employee: Employee) {
|
||||||
|
self.employee = employee
|
||||||
|
self.shouldShow = true
|
||||||
|
}
|
||||||
|
|
||||||
|
var content: some HTML {
|
||||||
|
Float(shouldDisplay: shouldShow, resetURL: "/employees") {
|
||||||
|
form(
|
||||||
|
employee == nil ? .hx.post(targetURL) : .hx.put(targetURL),
|
||||||
|
employee == nil ? .hx.target("#employee-table") : .hx.target("#employee_\(employee!.id)"),
|
||||||
|
employee == nil
|
||||||
|
? .hx.swap(.beforeEnd.transition(true).swap("0.5s"))
|
||||||
|
: .hx.swap(.outerHTML.transition(true).swap("0.5s")),
|
||||||
|
.custom(
|
||||||
|
name: "hx-on::after-request",
|
||||||
|
value: "if (event.detail.successful) toggleContent('float'); window.location.href='/employees';"
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
div(.class("row")) {
|
||||||
|
input(
|
||||||
|
.type(.text), .class("col-5"),
|
||||||
|
.name("firstName"), .value(employee?.firstName ?? ""),
|
||||||
|
.placeholder("First Name"), .required
|
||||||
|
)
|
||||||
|
div(.class("col-2")) {}
|
||||||
|
input(
|
||||||
|
.type(.text), .class("col-5"),
|
||||||
|
.name("lastName"), .value(employee?.lastName ?? ""),
|
||||||
|
.placeholder("Last Name"), .required
|
||||||
|
)
|
||||||
|
}
|
||||||
|
div(.class("btn-row")) {
|
||||||
|
button(.type(.submit), .class("btn-primary")) {
|
||||||
|
buttonLabel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private var buttonLabel: String {
|
||||||
|
guard employee != nil else { return "Create" }
|
||||||
|
return "Update"
|
||||||
|
}
|
||||||
|
|
||||||
|
private var targetURL: String {
|
||||||
|
guard let employee else { return "/employees" }
|
||||||
|
return "/employees/\(employee.id)"
|
||||||
|
}
|
||||||
|
}
|
||||||
51
Sources/App/Views/Employees/EmployeeTable.swift
Normal file
51
Sources/App/Views/Employees/EmployeeTable.swift
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
import Elementary
|
||||||
|
import ElementaryHTMX
|
||||||
|
import SharedModels
|
||||||
|
|
||||||
|
struct EmployeeTable: HTML {
|
||||||
|
let employees: [Employee]
|
||||||
|
|
||||||
|
var content: some HTML {
|
||||||
|
table {
|
||||||
|
thead {
|
||||||
|
tr {
|
||||||
|
th { "Name" }
|
||||||
|
th(.style("width: 100px;")) {
|
||||||
|
Button.add()
|
||||||
|
.attributes(
|
||||||
|
.style("padding: 0px 10px;"),
|
||||||
|
.hx.get("/employees/create"),
|
||||||
|
.hx.target("#float"),
|
||||||
|
.hx.swap(.outerHTML.transition(true).swap("0.5s"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tbody(.id("employee-table")) {
|
||||||
|
for employee in employees {
|
||||||
|
Row(employee: employee)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Row: HTML {
|
||||||
|
let employee: Employee
|
||||||
|
|
||||||
|
var content: some HTML {
|
||||||
|
tr(.id("employee_\(employee.id)")) {
|
||||||
|
td { "\(employee.firstName.capitalized) \(employee.lastName.capitalized)" }
|
||||||
|
td {
|
||||||
|
Button.detail()
|
||||||
|
.attributes(
|
||||||
|
.style("padding-left: 15px;"),
|
||||||
|
.hx.get("/employees/\(employee.id)"),
|
||||||
|
.hx.target("#float"),
|
||||||
|
.hx.pushURL(true),
|
||||||
|
.hx.swap(.outerHTML.transition(true).swap("0.5s"))
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ func routes(_ app: Application) throws {
|
|||||||
try app.register(collection: ApiController())
|
try app.register(collection: ApiController())
|
||||||
try app.register(collection: UserViewController())
|
try app.register(collection: UserViewController())
|
||||||
try app.register(collection: VendorViewController())
|
try app.register(collection: VendorViewController())
|
||||||
|
try app.register(collection: EmployeeViewController())
|
||||||
// try app.register(collection: ViewController())
|
// try app.register(collection: ViewController())
|
||||||
|
|
||||||
app.get { _ in
|
app.get { _ in
|
||||||
|
|||||||
Reference in New Issue
Block a user