Everywhere: Implement HTMX todo page
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import Fluent
|
||||
import Vapor
|
||||
|
||||
struct TodoAPIController: RouteCollection {
|
||||
func boot(routes: RoutesBuilder) throws {
|
||||
let todos = routes.grouped("todos")
|
||||
|
||||
todos.get(use: index)
|
||||
todos.post(use: create)
|
||||
todos.group(":id") { todo in
|
||||
todo.get(use: show)
|
||||
todo.put(use: update)
|
||||
todo.delete(use: delete)
|
||||
}
|
||||
// todos.delete(":id", use: delete)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [TodoDTO] {
|
||||
try await Todo.query(on: req.db).all().map { $0.toDTO() }
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func show(req: Request) async throws -> TodoDTO {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> TodoDTO {
|
||||
let todo = try req.content.decode(TodoDTO.self).toModel()
|
||||
|
||||
try await todo.save(on: req.db)
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func update(req: Request) async throws -> TodoDTO {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
|
||||
let updatedTodo = try req.content.decode(Todo.self)
|
||||
todo.title = updatedTodo.title
|
||||
try await todo.save(on: req.db)
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> HTTPStatus {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
}
|
||||
|
||||
try await todo.delete(on: req.db)
|
||||
|
||||
return .noContent
|
||||
}
|
||||
|
||||
// -------------------------------------
|
||||
|
||||
private func hexToUUID(hex: String) -> UUID? {
|
||||
var uuid: String = hex.replacingOccurrences(of: "-", with: "")
|
||||
|
||||
guard uuid.count == 32 else { return nil }
|
||||
|
||||
uuid.insert("-", at: uuid.index(uuid.startIndex, offsetBy: 8))
|
||||
uuid.insert("-", at: uuid.index(uuid.startIndex, offsetBy: 12 + 1))
|
||||
uuid.insert("-", at: uuid.index(uuid.startIndex, offsetBy: 16 + 2))
|
||||
uuid.insert("-", at: uuid.index(uuid.startIndex, offsetBy: 20 + 3))
|
||||
|
||||
return UUID(uuidString: uuid)
|
||||
}
|
||||
}
|
||||
@@ -1,60 +1,63 @@
|
||||
import Elementary
|
||||
import ElementaryHTMX
|
||||
import ElementaryHTMXSSE
|
||||
import ElementaryHTMXWS
|
||||
import Fluent
|
||||
import Vapor
|
||||
import VaporElementary
|
||||
|
||||
struct TodoController: RouteCollection {
|
||||
func boot(routes: RoutesBuilder) throws {
|
||||
let todos = routes.grouped("todos")
|
||||
routes.group("todos") { todos in
|
||||
todos.get(use: index)
|
||||
|
||||
todos.get(use: index)
|
||||
todos.post(use: create)
|
||||
todos.group(":id") { todo in
|
||||
todo.get(use: show)
|
||||
todo.put(use: update)
|
||||
todo.delete(use: delete)
|
||||
todos.post(use: create)
|
||||
|
||||
todos.group(":id") { todo in
|
||||
todo.delete(use: delete)
|
||||
}
|
||||
}
|
||||
// todos.delete(":todoID", use: delete)
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func index(req: Request) async throws -> [TodoDTO] {
|
||||
try await Todo.query(on: req.db).all().map { $0.toDTO() }
|
||||
func index(req: Request) async throws -> HTMLResponse {
|
||||
let todos = try await todos(db: req.db)
|
||||
|
||||
return HTMLResponse {
|
||||
MainLayout(title: "Todos") {
|
||||
TodosTableComponent(name: "todos", todos: todos)
|
||||
TodosFormComponent(name: "todos-form", target: "todos")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func show(req: Request) async throws -> TodoDTO {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
func create(req: Request) async throws -> HTMLResponse {
|
||||
do {
|
||||
try TodoDTO.validate(content: req)
|
||||
}
|
||||
catch let error as ValidationsError {
|
||||
return HTMLResponse {
|
||||
TodosFormComponent(name: "todos-form", target: "todos", errors: ["title": error.description])
|
||||
}
|
||||
}
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func create(req: Request) async throws -> TodoDTO {
|
||||
let todo = try req.content.decode(TodoDTO.self).toModel()
|
||||
|
||||
try await todo.save(on: req.db)
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
let todos = try await todos(db: req.db)
|
||||
|
||||
@Sendable
|
||||
func update(req: Request) async throws -> TodoDTO {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
return HTMLResponse {
|
||||
// Return the empty form
|
||||
TodosFormComponent(name: "todos-form", target: "todos")
|
||||
|
||||
// Also update the todos table
|
||||
TodosTableComponent(name: "todos", todos: todos, refresh: true) // TODO: Put component names inside variables
|
||||
}
|
||||
|
||||
let updatedTodo = try req.content.decode(Todo.self)
|
||||
todo.title = updatedTodo.title
|
||||
try await todo.save(on: req.db)
|
||||
|
||||
return todo.toDTO()
|
||||
}
|
||||
|
||||
@Sendable
|
||||
func delete(req: Request) async throws -> HTTPStatus {
|
||||
func delete(req: Request) async throws -> HTMLResponse {
|
||||
guard let uuid = hexToUUID(hex: req.parameters.get("id")!),
|
||||
let todo = try await Todo.find(uuid, on: req.db) else {
|
||||
throw Abort(.notFound)
|
||||
@@ -62,11 +65,16 @@ struct TodoController: RouteCollection {
|
||||
|
||||
try await todo.delete(on: req.db)
|
||||
|
||||
return .noContent
|
||||
return HTMLResponse {} // TODO: Return 204 No Content
|
||||
}
|
||||
|
||||
// -------------------------------------
|
||||
|
||||
@Sendable
|
||||
private func todos(db: any Database) async throws -> [TodoDTO] {
|
||||
try await Todo.query(on: db).all().map { $0.toDTO() }
|
||||
}
|
||||
|
||||
private func hexToUUID(hex: String) -> UUID? {
|
||||
var uuid: String = hex.replacingOccurrences(of: "-", with: "")
|
||||
|
||||
@@ -79,4 +87,5 @@ struct TodoController: RouteCollection {
|
||||
|
||||
return UUID(uuidString: uuid)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user