feat: Reorganizes files.
This commit is contained in:
96
Sources/App/Controllers/View/UsersViewController.swift
Normal file
96
Sources/App/Controllers/View/UsersViewController.swift
Normal file
@@ -0,0 +1,96 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
struct UserViewController: RouteCollection {
|
||||
|
||||
private let api = UserApiController()
|
||||
|
||||
func boot(routes: any RoutesBuilder) throws {
|
||||
let users = routes.protected.grouped("users")
|
||||
users.get(use: index(req:))
|
||||
users.post(use: create(req:))
|
||||
users.group(":userID") {
|
||||
$0.delete(use: delete(req:))
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> View {
|
||||
try await req.view.render(
|
||||
"users/index",
|
||||
UsersCTX(users: api.getSortedUsers(req: req))
|
||||
)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> View {
|
||||
_ = try await api.create(req: req)
|
||||
return try await req.view.render("users/table", ["users": api.getSortedUsers(req: req)])
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> View {
|
||||
_ = try await api.delete(req: req)
|
||||
return try await req.view.render("users/table", ["users": api.getSortedUsers(req: req)])
|
||||
}
|
||||
}
|
||||
|
||||
struct UserFormCTX: Content {
|
||||
let htmxForm: HtmxFormCTX<Context>
|
||||
|
||||
struct Context: Content {
|
||||
let showConfirmPassword: Bool
|
||||
let showEmailInput: Bool
|
||||
let buttonLabel: String
|
||||
}
|
||||
|
||||
static func signIn(next: String?) -> Self {
|
||||
.init(
|
||||
htmxForm: .init(
|
||||
formClass: "user-form",
|
||||
formId: "user-form",
|
||||
htmxTargetUrl: .post("/login\(next != nil ? "?next=\(next!)" : "")"),
|
||||
htmxTarget: "body",
|
||||
htmxPushUrl: true,
|
||||
htmxResetAfterRequest: true,
|
||||
htmxSwapOob: nil,
|
||||
htmxSwap: nil,
|
||||
context: .init(showConfirmPassword: false, showEmailInput: false, buttonLabel: "Sign In")
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
static func create() -> Self {
|
||||
.init(
|
||||
htmxForm: .init(
|
||||
formClass: "user-form",
|
||||
formId: "user-form",
|
||||
htmxTargetUrl: .post("/users"),
|
||||
htmxTarget: "#user-table",
|
||||
htmxPushUrl: false,
|
||||
htmxResetAfterRequest: true,
|
||||
htmxSwapOob: nil,
|
||||
htmxSwap: nil,
|
||||
context: .init(showConfirmPassword: true, showEmailInput: true, buttonLabel: "Create")
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private struct UsersCTX: Content {
|
||||
let users: [User.DTO]
|
||||
let form: UserFormCTX
|
||||
|
||||
init(users: [User.DTO], form: UserFormCTX? = nil) {
|
||||
self.users = users
|
||||
self.form = form ?? .create()
|
||||
}
|
||||
}
|
||||
|
||||
private extension UserApiController {
|
||||
|
||||
func getSortedUsers(req: Request) async throws -> [User.DTO] {
|
||||
try await index(req: req)
|
||||
.sorted { ($0.username ?? "") < ($1.username ?? "") }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user