Riyyi
1 month ago
17 changed files with 374 additions and 125 deletions
@ -0,0 +1,4 @@
|
||||
tr.htmx-swapping td { |
||||
opacity: 0; |
||||
transition: opacity 0.5s ease-out; |
||||
} |
@ -0,0 +1,10 @@
|
||||
* Website |
||||
|
||||
Website, written in Swift 6. |
||||
|
||||
* Libraries |
||||
|
||||
- [[https://github.com/vapor/vapor][Vapor]] HTTP web framework |
||||
- [[https://github.com/vapor/fluent.git][Fluent]] ORM |
||||
- [[https://github.com/sliemeobn/elementary][Elementary]] HTML templating |
||||
- [[https://github.com/bigskysoftware/htmx][htmx]] font-end JS |
@ -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) |
||||
} |
||||
} |
@ -0,0 +1,24 @@
|
||||
import Elementary |
||||
import ElementaryHTMX |
||||
|
||||
// ----------------------------------------- |
||||
// Elementary |
||||
|
||||
public extension HTMLAttribute { |
||||
static func empty() -> HTMLAttribute { |
||||
return HTMLAttribute(name: "", value: "") |
||||
} |
||||
} |
||||
|
||||
// ----------------------------------------- |
||||
// ElementaryHTMX |
||||
|
||||
public extension HTMLAttribute.hx { |
||||
static func swap(_ value: HTMLAttributeValue.HTMX.ModifiedSwapTarget, _ spec: String? = nil) -> HTMLAttribute { |
||||
if let spec { |
||||
.init(name: "hx-swap", value: "\(value.rawValue) \(spec)") |
||||
} else { |
||||
.init(name: "hx-swap", value: value.rawValue) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,35 @@
|
||||
import Elementary |
||||
import Fluent |
||||
|
||||
struct TodosFormComponent: HTML { |
||||
|
||||
var name: String |
||||
var target: String |
||||
var errors: [String: String] = [:] |
||||
|
||||
// ------------------------------------- |
||||
|
||||
var content: some HTML { |
||||
div(.id("cdiv_" + name)) { |
||||
form( |
||||
.id("\(name)-form"), |
||||
.hx.post("/\(target)"), .hx.target("#cdiv_" + name), .hx.swap(.outerHTML) |
||||
) { |
||||
div(.class("row")) { |
||||
div(.class("col-11")) { |
||||
input( |
||||
.class("form-control"), .type(.text), .id("\(name)-title"), |
||||
.name("title"), .placeholder("Title")) // .required |
||||
} |
||||
div(.class("col-1")) { |
||||
button(.class("btn btn-success"), .type(.submit)) { "Add" } |
||||
} |
||||
} |
||||
if let error = errors["title"] { |
||||
div(.class("form-text text-danger px-2")) { "\(error)." } |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,48 @@
|
||||
import Elementary |
||||
import ElementaryHTMX |
||||
import Fluent |
||||
|
||||
struct TodosTableComponent: HTML { |
||||
|
||||
var name: String |
||||
var todos: [TodoDTO] |
||||
var refresh: Bool = false |
||||
|
||||
// ------------------------------------- |
||||
|
||||
var content: some HTML { |
||||
div(.id("cdiv_" + name), refresh ? .hx.swapOOB(.outerHTML) : .empty()) { |
||||
table(.class("table")) { |
||||
thead { |
||||
tr { |
||||
th { "#" } |
||||
th { "ID" } |
||||
th { "Title" } |
||||
th { "Modifier" } |
||||
} |
||||
} |
||||
tbody( |
||||
.hx.confirm("Are you sure?"), .hx.target("closest tr"), |
||||
.hx.swap(.outerHTML, "swap:0.5s") |
||||
) { |
||||
for (index, todo) in todos.enumerated() { |
||||
tr { |
||||
td { "\(index)" } |
||||
td { todo.id?.uuidString ?? "" } |
||||
td { todo.title ?? "" } |
||||
td { |
||||
if let id = todo.id { |
||||
button( |
||||
.class("btn btn-danger"), |
||||
.hx.delete("/\(name)/\(id.uuidString)") |
||||
) { "Delete" } |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue