feat: Starting users view controller.
This commit is contained in:
@@ -6,20 +6,7 @@
|
||||
#extend("logo")
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<form class="login-form"
|
||||
hx-post="#(route)"
|
||||
hx-target="body"
|
||||
hx-push-url="true"
|
||||
>
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username" placeholder="Username" name="username" autocomplete="username" required autofocus>
|
||||
<br>
|
||||
<label for="password">Password</label>
|
||||
<input type="password" id="password" placeholder="Password" name="password" autocomplete="current-password" required>
|
||||
<br>
|
||||
<input type="submit" value="Sign In">
|
||||
</form>
|
||||
#extend("user-form")
|
||||
</div>
|
||||
#endexport
|
||||
#endextend
|
||||
|
||||
37
Resources/Views/user-form.leaf
Normal file
37
Resources/Views/user-form.leaf
Normal file
@@ -0,0 +1,37 @@
|
||||
<form class="user-form"
|
||||
id="user-form"
|
||||
hx-post="#(htmxTargetUrl)"
|
||||
hx-target="#(htmxTarget)"
|
||||
hx-push-url="#(htmxPushUrl)"
|
||||
>
|
||||
<label for="username">Username</label>
|
||||
<input type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
placeholder="Username"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
<br>
|
||||
<label for="password">Password</label>
|
||||
<input type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
placeholder="Password"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
<br>
|
||||
#if(showConfirmPassword):
|
||||
<label for="confirmPassword">Password</label>
|
||||
<input type="password"
|
||||
id="confirmPassword"
|
||||
name="confirmPassword"
|
||||
placeholder="Confirm Password"
|
||||
autofocus
|
||||
required
|
||||
>
|
||||
<br>
|
||||
#endif
|
||||
<input type="submit" value="#(buttonLabel)">
|
||||
</form>
|
||||
@@ -8,10 +8,7 @@ struct EmployeeViewController: RouteCollection {
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let employees = routes.protected.grouped("employees")
|
||||
|
||||
// MARK: Protected routes.
|
||||
|
||||
employees.get(use: employees(req:))
|
||||
employees.get(use: index(req:))
|
||||
employees.get("form", use: employeeForm(req:))
|
||||
employees.post(use: postEmployeeForm(req:))
|
||||
employees.group(":employeeID") {
|
||||
@@ -23,7 +20,7 @@ struct EmployeeViewController: RouteCollection {
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func employees(req: Request) async throws -> View {
|
||||
func index(req: Request) async throws -> View {
|
||||
return try await req.view.render("employees", EmployeesCTX(api: api, req: req))
|
||||
}
|
||||
|
||||
|
||||
36
Sources/App/Controllers/UsersViewController.swift
Normal file
36
Sources/App/Controllers/UsersViewController.swift
Normal file
@@ -0,0 +1,36 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
struct UserViewController: RouteCollection {
|
||||
|
||||
private let api = ApiController()
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let users = routes.protected.grouped("users")
|
||||
users.get(use: index(req:))
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> View {
|
||||
let users = try await api.usersIndex(req: req)
|
||||
return try await req.view.render("users", ["users": users])
|
||||
}
|
||||
}
|
||||
|
||||
struct UserFormCTX: Content {
|
||||
let htmxTargetUrl: String
|
||||
let htmxTarget: String
|
||||
let htmxPushUrl: String
|
||||
let showConfirmPassword: Bool
|
||||
let buttonLabel: String
|
||||
|
||||
static func signIn(route: String) -> Self {
|
||||
.init(
|
||||
htmxTargetUrl: route,
|
||||
htmxTarget: "body",
|
||||
htmxPushUrl: "true",
|
||||
showConfirmPassword: false,
|
||||
buttonLabel: "Sign In"
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ struct ViewController: RouteCollection {
|
||||
|
||||
private let api = ApiController()
|
||||
private let employees = EmployeeViewController()
|
||||
private let users = UserViewController()
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let protected = routes.protected
|
||||
@@ -20,15 +21,16 @@ struct ViewController: RouteCollection {
|
||||
|
||||
protected.get(use: home(req:))
|
||||
protected.post("logout", use: logout(req:))
|
||||
protected.get("users", use: users(req:))
|
||||
// protected.get("users", use: users(req:))
|
||||
try routes.register(collection: employees)
|
||||
try routes.register(collection: users)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func getLogin(req: Request) async throws -> View {
|
||||
req.logger.info("Query: \(req.url.query ?? "n/a")")
|
||||
req.logger.debug("Login Query: \(req.url.query ?? "n/a")")
|
||||
let params = try? req.query.decode(LoginParameter.self)
|
||||
return try await req.view.render("login", ["route": params?.next ?? "/"])
|
||||
return try await req.view.render("login", UserFormCTX.signIn(route: params?.next ?? "/"))
|
||||
}
|
||||
|
||||
@Sendable
|
||||
@@ -65,18 +67,12 @@ struct ViewController: RouteCollection {
|
||||
|
||||
switch route {
|
||||
case .users:
|
||||
return try await users(req: req)
|
||||
return try await users.index(req: req)
|
||||
case .employees:
|
||||
return try await employees.employees(req: req)
|
||||
return try await employees.index(req: req)
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func users(req: Request) async throws -> View {
|
||||
let users = try await api.usersIndex(req: req)
|
||||
return try await req.view.render("users", ["users": users])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private struct UserForm: Content {
|
||||
|
||||
Reference in New Issue
Block a user